Click here to Skip to main content
11,715,043 members (71,408 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: .NET3.5 C#
i have following code with table customer having id and name field .......when i insert new record i want to change datagrid simultaneously
so i performed this but on load() i am getting blank data grid view



 private void load()
        {
            string sql = "select * from customer";
            SqlDataAdapter myAdapter = new SqlDataAdapter(sql, con);
 
            DataSet myDataSet = new DataSet("customer");
            myAdapter.Fill(myDataSet);
 
            dataGridView1.DataSource = myDataSet;
        }
 
private void button4_Click(object sender, EventArgs e)
       {
 
           if (txtname.Text != "" & txtage.Text != "")
           {
               con.Open();
 
              
               //simple
               com.CommandText = "insert into customer(customerName,customerCode) values ('" + txtname.Text + "','" + txtage.Text + "')";
               com.ExecuteNonQuery();
               con.Close();
               MessageBox.Show("record inserted");
               load();
 
           }
           else MessageBox.Show("data entry error");
 
       }
here button will add record ....................record is getting insert but datagrid view on load function gibes blank
plz tell wat i have missed

regards
Posted 8-Jan-13 19:25pm

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

datagrid view on load function gibes blank
You point to dataset instead of table to grid. Try:
DataTable myDataTable = new DataTable("customer");
myAdapter.Fill(myDataTable); 
dataGridView1.DataSource = myDataTable;
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 520
1 Richard MacCutchan 275
2 Maciej Los 259
3 ManasKumarM 155
4 Frankie-C 125
0 Sergey Alexandrovich Kryukov 775
1 CHill60 415
2 Richard MacCutchan 405
3 Maciej Los 389
4 OriginalGriff 320


Advertise | Privacy | Mobile
Web03 | 2.8.150901.1 | Last Updated 9 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100