Here Is My Code..With this In Datagridview Only The First Modified Record Only Updating..Remaining Records are not updating. But I need All The Modified Records Should Be Update pls help me.
private void btnUpdate(object sender, EventArgs e)
{
using (OleDbConnection connect = new OleDbConnection(connectionString))
{
using (OleDbCommand command = new OleDbCommand())
{
command.Connection = connect;
command.CommandText = "update customer set Name1=@name,Address=@address where Name1=@name";
command.Parameters.Add(new OleDbParameter("name", OleDbType.VarChar));
command.Parameters.Add(new OleDbParameter("address", OleDbType.VarChar));
connect.Open();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (!row.IsNewRow)
{
command.Parameters["name"].Value = row.Cells[0].Value;
command.Parameters["address"].Value = row.Cells[1].Value;
command.ExecuteNonQuery();
}
}
}
}
}
Loading

CHAITANYA KIRAN KASANIPosted Oct 1, 2012, 7:22 AM
Shivanand ArurPosted Sep 30, 2012, 5:12 AM
"update customer set Name1=@name,Address=@address where Name1=@name";"
What this will do is, it will update the name and the address of the customer according to the specific name of the Customer.
For example...
table name: tblCustomer
Columns:
CustID CustName CustAddress
01 Shiv Mumbai
02 Anand Delhi
03 Chaitanya Kolkata
Now If I write a query which you have written in the Code....
"update customer set Name1=@name,Address=@address where Name1=@name";
Suppose in the @name parameter i get the value as 'Anand', in the @address parameter i get the value as 'Pune' and again for the @name parameter in the where clause i get the value as 'Anand'.... So logically your query will look something like this...
"Update Customer Set Name1='Anand', Address='Pune' where Name1='Anand';"
It means that you are updating a record where the Name1 is 'Anand', hence it is just recording only one record... If you wanna update all the records, then simply put this Update Query as mentioned below...
" Update Customer Set Name='@name', Address='@address' " what this will do is that it will update all the members with the name and the address value that you have passed in the parameter.....
General knowledge - Usually you should always update a record based on the ID (Primary key) of the table.... it is much safe since it will affect only a specific record.... If you pass a name or an address in the where clause, it will affect all the records which have the same name and the address in the database table..
Please let me know, if this solution is pro[er.... and is possible please explain the functionality if the answered post is incorrect....
Thank you!!!