Pooja kulkarni

Pooja kulkarni

  • NA
  • 13
  • 1.4k

Saving Gridview Data To Database

Oct 27 2018 3:02 AM
I am having one of the form which givs a sorted data in front of me , that sorted data with help of gridview and some text box ,  I want to save it in my another database ; on "Save" button click
here is my code 
 
protected void lbInsert_Click(object sender, EventArgs e)
{
SqlDataSource2.InsertParameters["1"].DefaultValue =
((TextBox)GridView2.FooterRow.FindControl("txtMeasure")).Text;
SqlDataSource2.InsertParameters["2"].DefaultValue = ((TextBox)GridView2.FooterRow.FindControl("txtMeasure1")).Text;
SqlDataSource2.InsertParameters["3"].DefaultValue = ((TextBox)GridView2.FooterRow.FindControl("txtMeasure2")).Text;
SqlDataSource2.InsertParameters["4"].DefaultValue = ((TextBox)GridView2.FooterRow.FindControl("txtMeasure3")).Text;
SqlDataSource2.InsertParameters["5"].DefaultValue = ((TextBox)GridView2.FooterRow.FindControl("txtMeasure4")).Text;
SqlDataSource2.InsertParameters["TMLCALCULATION"].DefaultValue = ((TextBox)GridView2.FooterRow.FindControl("txtMeasure5")).Text;
SqlDataSource2.Insert();
}

protected void upload_Click(object sender, EventArgs e)
{
string query1 = "INSERT INTO tbl_Pre_Y1_Bus1 (CRITICAL,SPECIFICATION,[1],[2],[3],[4],[5],TMLCAL) VALUES (@CRITICAL,@SPECIFICATION,@[1],@[2],@[3],@[4],@[5],@TMLCAL)";

foreach (GridViewRow row in GridView2.Rows)
{
using (SqlCommand cmd = new SqlCommand(query1))
{
cmd.Connection = con;
cmd.CommandType = CommandType.Text;

cmd.Parameters.AddWithValue("@CRITICAL", Convert.ToString(row.Cells[0].Text));
cmd.Parameters.AddWithValue("@SPECIFICATION", Convert.ToString(row.Cells[1].Text));
cmd.Parameters.AddWithValue("@[1]", Convert.ToString(row.Cells[2].Text));
cmd.Parameters.AddWithValue("@[2]", Convert.ToString(row.Cells[3].Text));
cmd.Parameters.AddWithValue("@[3]", Convert.ToString(row.Cells[4].Text));
cmd.Parameters.AddWithValue("@[4]", Convert.ToString(row.Cells[5].Text));
cmd.Parameters.AddWithValue("@[5]", Convert.ToString(row.Cells[6].Text));
cmd.Parameters.AddWithValue("@TMLCAL", Convert.ToString(row.Cells[7].Text));

con.Open();  // fetching an error
cmd.ExecuteNonQuery();
con.Close();
Page.Response.Redirect(Page.Request.Url.ToString(), true);
}

}


string query = "INSERT INTO tbl_Pre_Y1_Bus1 (PartNo,ChallanDate,ChallanNo,Date_sup,date_insp,Accepted,Rejected,TMl_remark,sup_remark) VALUES (@PartNo,@ChallanDate,@ChallanNo,@Date_sup,@date_insp,@Accepted,@Rejected,@TMl_remark,@sup_remark)";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;

cmd.Parameters.AddWithValue("@PartNo", PartNo.Text);
cmd.Parameters.AddWithValue("@ChallanDate", ChallanDate.Text);
cmd.Parameters.AddWithValue("@ChallanNo", ChallanNo.Text);
cmd.Parameters.AddWithValue("@Date_sup", Date_sup.Text);
cmd.Parameters.AddWithValue("@date_insp", date_insp.Text);
cmd.Parameters.AddWithValue("@Accepted", Accepted.Text);
cmd.Parameters.AddWithValue("@Rejected", Rejected.Text);
cmd.Parameters.AddWithValue("@TMl_remark", TMl_remark.Text);
cmd.Parameters.AddWithValue("@sup_remark", sup_remark.Text);

con.Open();  // fetching an error
cmd.ExecuteNonQuery();
con.Close();
Page.Response.Redirect(Page.Request.Url.ToString(), true);
}

Answers (1)