Click here to Skip to main content
15,912,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have added a record to the underlying db and after I add the record i do a datagridview.Refresh(); and i dont see the newly added record.

If i stop and start the application its there. What am I doing or not doing?
Notes : button1 and datagridview is in different Forms. I made datagridview's Modifiers public.
This project is Windows application.


C#
public class CustomerService
{
    public List<Customers> ShowAll()
    {
        List<Customers> customers = new List<Customers>();
        SqlConnection conn = new SqlConnection("data source=.; database=custer; user id=sa; password=*****");
        SqlCommand cmd = new SqlCommand(" select * from Customers ", conn.Open());
        SqlDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            Customer customer = new Customer ()
            {
                CustomerID = dr.GetInt32(0),
                CustomerName = dr.GetString(1),
                CustomerSurname = dr.GetString(2),
            };
            customers.Add(customer);
        }
        conn.Close();
        return customers;
    }
  }

    private void button1_Click(object sender, EventArgs e)
    {
        CustomerService service = new CustomerService();
        if (txtCustomerName.Text != "" || txtCustomerSurname.Text != "")
        {
            musteriservice.MusteriEkle(txtCustomerName.Text, txtCustomerSurname.Text); //this rows is other method .I am using for adding new customer 
            MessageBox.Show("Customer Added");
            Form1.dataGridView1.DataSource = service.ShowAll();
            Form1.dataGridView1.Refresh();
        }
        else
        {
            //……………
        }
    }    }
Posted
Updated 13-Aug-12 21:01pm
v8
Comments
Kenneth Haugland 13-Aug-12 10:22am    
MAybe it reads the SQL data before the commit has executed?
y.baris 14-Aug-12 3:00am    
if problem is that what you told
how can I solve it ?
pradiprenushe 14-Aug-12 3:54am    
dr.Close();
conn.Close();

Close datareader & look what happens.
y.baris 14-Aug-12 4:09am    
I wrote dr.Close();
but problem still not solved
pradiprenushe 14-Aug-12 4:18am    
if(txtCustomerName.Text != "" || txtCustomerSurname.Text != "")
{
}
else
{}
Form1.dataGridView1.DataSource = service.ShowAll();


Change to this & see behaviour

1 solution

If you are binding the data in ASP.net make use of dataGridView1.databind() method.
Let us know are you binding in asp.net or a windows application?
 
Share this answer
 
Comments
y.baris 14-Aug-12 2:53am    
this project is Windows application.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900