Click here to Skip to main content
15,909,324 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Actually I have Updated Dynamic Textbox values from Gridview to database but according to 'rowindex' which is correct.

But Now I want to Update the values according to 'ID' which is in GridView. 'ID' is fetching from the database.

So There is a little problem in my code.

What should I write in my code for the 'ID'.

I have used this query which is correct. But in place of 'rowindex', I want to use 'ID'. The update query and some part of the code is given below -
C#
int count = GridView2.Rows.Count;
            for (int i = 0; i < count; i++)
            {
                TextBox box3 = (TextBox)GridView2.Rows[rowIndex].Cells[3].FindControl("TextBox3");
                TextBox box4 = (TextBox)GridView2.Rows[rowIndex].Cells[4].FindControl("TextBox4");
                sc.Add(box3.Text + "," + box4.Text);
                rowIndex++;
                conn.Open();
                string sqlStatement = "update SampleTable set Intime2='" + box3.Text + "',Outtime2='" + box4.Text + "' where ID='" + rowIndex + "'";
               
                SqlCommand cmd = new SqlCommand(sqlStatement, conn);
                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
                conn.Close();
            }
Posted
Updated 15-Dec-11 23:17pm
v2

hi,


for (int i = 0; i < count; i++)
{
TextBox box3 = (TextBox)GridView2.Rows[i].Cells[3].FindControl("TextBox3");
TextBox box4 = (TextBox)GridView2.Rows[i].Cells[4].FindControl("TextBox4");
sc.Add(box3.Text + "," + box4.Text);
rowIndex++;
conn.Open();
string sqlStatement = "update SampleTable set Intime2='" + box3.Text + "',Outtime2='" + box4.Text + "' where ID='" + i+ "'";
SqlCommand cmd = new SqlCommand(sqlStatement, conn);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
conn.Close();
} .


or else bind the id to a label and fetch that and pass it to your query

Hope this helps
 
Share this answer
 
Use the article been stated down on the following link:

Gridview Insert, Update, Edit and Delete with dynamic data from database[^]
 
Share this answer
 
easy solution is u bind the id to gridview then u easily updated the perticular row.

that id passed throgh sql .
on perticular selected row u get id then used it.
 
Share this answer
 
another simple approach is you can use grid view's Row Editing event.
 
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