Hi all,
I am adding rows to a gridview at button click... User can add as many records to the gridview .... Now i want to add all the rows data to database in one go..
How can i do this please help
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Prabhu RajaPosted Oct 1, 2011, 8:17 AM
SqlCommand cmd ;
SqlConnection oConn = new SqlConnection(" Your Connection String ");
oConn.Open();
cmd.Connection = oConn;
cmd.CommandType = CommandType.Text;
int n=0 ; //get how many rows are added at end of insert
foreach (DataRow dr in GridView1.Rows)
{
cmd = new SqlCommand("Insert Into Tablename values (@Value1, @Value2)"); //Insert Query
//Give Your Column name or use as dr.rows.cell[index]
cmd.parameter["@Value1"].value = dr["GridViewColumnName"];
cmd.parameter["@Value2"].value = dr["GridViewColumnName"];
n += cmd.ExecuteNonQuery();
cmd.parameter.clear();
}
oConn.Close();
---------------------------------------------------------------------------------------------
Thank You
Mayur GujrathiPosted Oct 1, 2011, 7:23 AM
Dim hdpdtcid As New TextBox
For Each dg As GridViewRow In grdDematChk.Rows
dgqty = dg.FindControl("TextBox1")
hdpdtcid = dg.FindControl("TextBox2")
//write insert statement here you can get text as dgqty .Text
End If
Next
Satish BhatPosted Oct 1, 2011, 5:48 AM