Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In gridview record as follows

Selectdata ID Qty Price Status

checkbox 1245 2 50 New
checkbox 1245 1 25 New
checkbox 4568 1 10 New
checkbox 2345 1 25 New
checkbox 4568 1 10 New

From the above gridview i am updating i am selecting first two rows from the gridview and upate in the database

String strConnString = ConfigurationManager.ConnectionStrings["ConnectionStrings"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
con.Open();
foreach (GridViewRow row in grdRpt.Rows)
{
CheckBox cb = (CheckBox)row.FindControl("chkselecdata");
if (cb.Checked == true)
{
int key = Convert.ToInt32(grdRpt.DataKeys[row.RowIndex].Value);
SqlCommand cmd = new SqlCommand("UPDATE [transact].[transaction_item] SET Status = 'Ready' Where transactee_id=" + key.ToString(), con);
cmd.ExecuteNonQuery();
}
}
con.Close();

i am updating gridview selected row to database using c#

in the run mode i am selecting first row in the gridview for the id 1245 but in the database 1245 for the two rows updated in the database.

i am not selecting second row in the gridview
in the run mode i am selecting first row only in the gridview to update in the database. but in the database two rows 1245 are updating.

what is the mistake in my above code.


in the database ouput i get as follows

ID Qty Price Status

1245 2 50 Ready
1245 1 25 Ready

i am selecting only one first row in the gridview. but in database two rows updated for that selectd id 1245

what is the mistake in above code.

What I have tried:

In gridview record as follows

Selectdata ID Qty Price Status

checkbox 1245 2 50 New
checkbox 1245 1 25 New
checkbox 4568 1 10 New
checkbox 2345 1 25 New
checkbox 4568 1 10 New

From the above gridview i am updating i am selecting first two rows from the gridview and upate in the database

String strConnString = ConfigurationManager.ConnectionStrings["ConnectionStrings"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
con.Open();
foreach (GridViewRow row in grdRpt.Rows)
{
CheckBox cb = (CheckBox)row.FindControl("chkselecdata");
if (cb.Checked == true)
{
int key = Convert.ToInt32(grdRpt.DataKeys[row.RowIndex].Value);
SqlCommand cmd = new SqlCommand("UPDATE [transact].[transaction_item] SET Status = 'Ready' Where transactee_id=" + key.ToString(), con);
cmd.ExecuteNonQuery();
}
}
con.Close();

i am updating gridview selected row to database using c#

in the run mode i am selecting first row in the gridview for the id 1245 but in the database 1245 for the two rows updated in the database.

i am not selecting second row in the gridview
in the run mode i am selecting first row only in the gridview to update in the database. but in the database two rows 1245 are updating.

what is the mistake in my above code.


in the database ouput i get as follows

ID Qty Price Status

1245 2 50 Ready
1245 1 25 Ready

i am selecting only one first row in the gridview. but in database two rows updated for that selectd id 1245.


what is the mistake in above code.
Posted
Updated 18-Jun-18 21:28pm

There is no mistake. Your code is doing exactly what you tell it to do: UPDATE all records with id 1245.
 
Share this answer
 
It seems like your code is working fine.
Your Problem is that you can't uniquely identify your row, so every row with the transactee_id of 1245 is updated, to get this working as you expect you need a unique identifier (ID) that only describes one row.

"UPDATE [transact].[transaction_item] SET Status = 'Ready' Where transactee_id=" + key.ToString()


Another thing to mention is how you bring together your UPDATE string and your parameter.
The way you do it can cause multiple accidental errors or even injection!
The best way would be to add your 'key' as a Parameter (cmd.Parameters.AddWithValue();)
 
Share this answer
 

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