Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to update a record in a Gridview
    primarykey
    data
    text
    <p>When I try to update a record in a <code>GridView</code>, the following code doesn't update the record, but it doesn't throw any errors either.</p> <pre><code> protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { string index = GridView1.Rows[e.RowIndex].Cells[1].Text; //DwgRegID SqlConnection con = new SqlConnection("Data Source=MEHDI-PC\\SQLEXPRESS;Initial Catalog=PIMS;Integrated Security=True"); using (SqlCommand cmd = new SqlCommand()) { string sql = "UPDATE dbo.Dwg_Register SET Ref = @Ref, Dwg_Ref = @Dwg_Ref,Title = @Title, Dwg_Received_Date = @Dwg_Received_Date, Rev = @Rev, Trade = @Trade, type1=@type1,Produced_Date=@Produced_Date,Produced_By=@Produced_By,Submittal_Ref=@Submittal_Ref,Issued_To=@Issued_To,Date_Issued = Date_Issued, Purpose = @Purpose, status1=@status1,Action_Date=@Action_Date where DwgRegID=N'" + index + "'"; cmd.Connection = con; con.Open(); cmd.CommandText = sql; cmd.Parameters.Add(new SqlParameter("@Ref", txtRef.Text)); cmd.Parameters.Add(new SqlParameter("@Dwg_Ref", txtDwgRef.Text)); cmd.Parameters.Add(new SqlParameter("@Title", txtTitle.Text)); cmd.Parameters.Add(new SqlParameter("@Dwg_Received_Date", txtDwgReceivedDate.Text == "" ? DBNull.Value : (object)txtDwgReceivedDate.Text)); cmd.Parameters.Add(new SqlParameter("@Rev", txtRev.Text)); cmd.Parameters.Add(new SqlParameter("@Trade", ddlTrade.Text)); cmd.Parameters.Add(new SqlParameter("@type1", ddlType.Text)); cmd.Parameters.Add(new SqlParameter("@Produced_Date", txtProducedDate.Text == "" ? DBNull.Value : (object)txtProducedDate.Text)); cmd.Parameters.Add(new SqlParameter("@Produced_By", ddlProducedBy.Text)); cmd.Parameters.Add(new SqlParameter("@Submittal_Ref", txtSubmittalRef.Text)); cmd.Parameters.Add(new SqlParameter("@Issued_To", ddlIssuedTo.Text)); cmd.Parameters.Add(new SqlParameter("@Date_Issued", txtDateIssued.Text == "" ? DBNull.Value : (object)txtDateIssued.Text)); cmd.Parameters.Add(new SqlParameter("@Purpose", ddlPurpose.Text)); cmd.Parameters.Add(new SqlParameter("@status1", ddlStatus.Text)); cmd.Parameters.Add(new SqlParameter("@Action_Date", txtActionDate.Text == "" ? DBNull.Value : (object)txtActionDate.Text)); //cmd.Parameters.Add(new SqlParameter("@DwgRegID", index)); cmd.ExecuteNonQuery(); if (con.State == ConnectionState.Open) { con.Close(); } GridView1.DataBind(); } SqlConnection con1 = new SqlConnection("Data Source= MEHDI-PC\\SQLExpress; Initial Catalog=PIMS; Integrated Security=True"); string sql1 = "SELECT DwgRegID,Ref,Dwg_Ref,Title,Dwg_Received_Date,Rev,Trade,type1,Produced_Date,Produced_By,Submittal_Ref,Issued_To,Date_Issued,Purpose,status1,Action_Date from dbo.Dwg_Register"; con1.Open(); SqlDataAdapter da = new SqlDataAdapter(sql1,con1); DataSet ds1 = new DataSet(); da.Fill(ds1); GridView1.DataSource = ds1; GridView1.DataBind(); if (con1.State == ConnectionState.Open) { con1.Close(); } GridView1.EditIndex = -1; GridView1.DataBind(); MultiView1.SetActiveView(ViewGrid); lblUpdate.Text = "Record updated sucessfully."; } </code></pre> <p>When I run the following SQL query in SSMS: </p> <pre><code>UPDATE dbo.Dwg_Register SET Ref=4 from dbo.Dwg_Register where DwgRegID=23 </code></pre> <p>this command does store value 4 into dbo.Dwg_Register. But this command is not storing values into sql table from the GridView. </p> <p>My EditRow function code is as following:</p> <pre><code> protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { MultiView1.SetActiveView(ViewGrid); GridView1.EditIndex = e.NewEditIndex; GridView1.DataBind(); </code></pre> <p>}</p> <p>This <code>RowEditing</code> function changes <code>RowState</code> in the <code>GridView</code> to edit-mode. But when I edit a cell value in the <code>Gridview</code> and press update button to store the new value to Sql table, Update function doesnt do it. This code neither throws any errors. Please help me identify where I might be going wrong. Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload