Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow g1 in GridView1.Rows)
{
string connStr = ConfigurationManager.ConnectionStrings["demoConStr"].ConnectionString;
SqlConnection con = new SqlConnection(connStr);
com = new SqlCommand("Insert into prodata (ID,sl,cpoints,spec,actual,remark,status,fil) Values ('" + txtsystemid.Text + "','" + g1.Cells[0].Text + "','" + g1.Cells[1].Text + "','" + ((TextBox)g1.Cells[2].FindControl("txtspecbind")).Text + "','" + ((TextBox)g1.Cells[3].FindControl("txtactbind")).Text + "','" + ((TextBox)g1.Cells[4].FindControl("txtrembind")).Text + "','" + ((DropDownList)g1.Cells[5].FindControl("checkbind")).Text + "','" + 0 + "' )", con);
con.Open();
com.ExecuteNonQuery();
con.Close();
}
lblm.Text = "Records inserted successfully";
}

This code for Gridview Data Entry..
I need Update Query... Please solve my problem..
Posted
Comments
Nandakishore G N 19-Feb-13 3:57am    
you would have used gridview_rowcommand..

1 solution

This may be a solution:

Bind your data to gridview so one of not nullable field you can find if record from database or will be added to database so you can decide what you should do!


DataTable dt=your table result

for (int i=0;i<(record count able to be inserted);i++)
{

DataRow dr=dt.NewRow();
dt.Rows.Add(dr);
}


protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow g1 in GridView1.Rows)
{
string connStr = ConfigurationManager.ConnectionStrings["demoConStr"].ConnectionString;
SqlConnection con = new SqlConnection(connStr);

if (((TextBox)g1.findControl ("txtId").Text=="") //txtId is control which binds not nullable field on your data
{
com = new SqlCommand("Insert into prodata (ID,sl,cpoints,spec,actual,remark,status,fil) Values ('" + txtsystemid.Text + "','" + g1.Cells[0].Text + "','" + g1.Cells[1].Text + "','" + ((TextBox)g1.Cells[2].FindControl("txtspecbind")).Text + "','" + ((TextBox)g1.Cells[3].FindControl("txtactbind")).Text + "','" + ((TextBox)g1.Cells[4].FindControl("txtrembind")).Text + "','" + ((DropDownList)g1.Cells[5].FindControl("checkbind")).Text + "','" + 0 + "' )", con);
con.Open();
com.ExecuteNonQuery();
con.Close();
}
else
{

//your update query
}

}
lblm.Text = "Records inserted successfully";

}
 
Share this answer
 
Comments
sundar-indo 19-Feb-13 3:51am    
i need insert place update.. where condition .. SQL Query
HilmiAydin 19-Feb-13 3:54am    
How you bind the gridview?
sundar-indo 19-Feb-13 5:13am    
by textbox inside of Gridview.. I will call it by ID..
After this i will edit some data and then update..

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