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

Selectdata ID Transactionid Qty Price Status

checkbox 1 1245 2 50 New
checkbox 2 1245 1 25 New
checkbox 3 4568 1 10 New
checkbox 4 2345 1 25 New
checkbox 5 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.

in the above update query how to get ID which is unique for Transaction id 1245


what is the mistake in my above code.


in the database ouput i get as follows

ID Transactionid Qty Price Status

1 1245 2 50 New
2 1245 1 25 New

What I have tried:

In gridview record as follows

Selectdata ID Transactionid Qty Price Status

checkbox 1 1245 2 50 New
checkbox 2 1245 1 25 New
checkbox 3 4568 1 10 New
checkbox 4 2345 1 25 New
checkbox 5 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.

in the above update query how to get ID which is unique for Transaction id 1245


what is the mistake in my above code.


in the database ouput i get as follows

ID Transactionid Qty Price Status

1 1245 2 50 New
2 1245 1 25 New
3 4568 1 10 New
4 2345 1 25 New
5 4568 1 10 New
Posted
Updated 18-Jun-18 23:00pm
Comments
Richard MacCutchan 19-Jun-18 5:08am    
I already told you what the issue was and all you have done is repost the exact same question. You are getting close to being thrown off this site for abuse. I suggest you go away and do some serious study of C#, SQL, and programming in general.

1 solution

Please dont open new Questions for every step, i already told you what to do, a little google would have easily solved your problem.
You need to UPDATE [...] WHERE ID=[1,2,3,4,5,e.g.] 
NOT         UPDATE [...] WHERE transactee_id=[1245,4568,e.g.]

If you are having Problems getting your ID from GridView then nobody except yourself can help, because we don't know where your data is from or how it is stored.
If you have your ID shown in your gridview you might be able to get it from there
 
Share this answer
 
v3

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