public partial class goruntule : Form
    {
        SqlDataAdapter da = null;
        DataSet ds = null;

        SqlCommandBuilder cmdBuilder;
        DataSet changes;

        public goruntule()
        {
            InitializeComponent();
        }

        private void goruntule_Load(object sender, EventArgs e)
        {
            label1.Text = PERSONEL_TAKIP.main_form.goruntulenecek_tablo;
            DataTable dtusers = new DataTable();
            da = new SqlDataAdapter("SELECT * FROM " + PERSONEL_TAKIP.main_form.goruntulenecek_tablo, PERSONEL_TAKIP.main_form.myConnection);
            da.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0];
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                cmdBuilder = new SqlCommandBuilder(da);
                changes = ds.GetChanges();
                if (changes != null)
                {
                    da.Update(changes);
                }
                MessageBox.Show("Changes Done");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message +ex.Source);
            }
            
        }

If i use Datatable instead of Dataset up on my code GridView works fine. But i need to use dataset because Dataset can update my database (sql server 2008) with this method : da.Update(changes). I don't know if it can update my database, i just read it on many sites. Anyway why my code does not works ? Visual Studio does not give me an error or something. when program works the datagridview1 is blank when i use dataset like the code i send you. But if i use datatable, datagridview1 shows me the results from my sql command.

I would be happy if you can help me...

Thank you!

Recommended Answers

All 2 Replies

Take a look at here on this example:

DataSet ds;
                    SqlDataAdapter adap;
                    private void LoadData()
                    {
                             if(ds != null)
                                       ds.Clear();
                             adap = new SqlDataAdapter("select id,title, description from testtable", con);
                             ds = new DataSet();
                             adap.Fill(ds);
                             dataGrid1.DataSource = ds.Tables[0];
                    } 
                    //This click will update one of the field in the database using adapter update() method on
                    dataset.
                    private void btnUpdate_Click(object sender, System.EventArgs e)
                    {
                             SqlCommandBuilder com = new SqlCommandBuilder(adap);
                             foreach(DataRow dr in ds.Tables[0].Rows)
                                       dr["title"] = txtTitle.Text; //chanking thw column - example code, you update it your way
                             adap.Update(ds);
                    }

Mitja Bonca your code does not worked for me. Can you suggest me something else ? Or you need any other specific information about my program ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.