Click here to Skip to main content
15,912,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone.
when I try to update a record in a Gridview, following code doesnt throw any error but doesnt update the record either. my code is:

C#
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{


    string index = e.RowIndex.ToString();

    lblSet.Text = index;

    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, Type=@Type,Produced_Date=@Produced_Date,Produced_By=@Produced_By,Submittal_Ref=@Submittal_Ref,Issued_To=@Issued_To,Date_Issued = Date_Issued, Purpose = @Purpose, Status=@Status,Action_Date=@Action_Date from dbo.Dwg_Register where DwgRegID=@DwgRegID";

        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("@Type", ddlType.Text));// == "" ? DBNull.Value : (object)txtDateReceived.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("@Status", ddlStatus.Text));
        cmd.Parameters.Add(new SqlParameter("@Action_Date", txtActionDate.Text == "" ? DBNull.Value : (object)txtActionDate.Text));
        cmd.Parameters.Add(new SqlParameter("@DwgRegID", int.Parse(lblSet.Text)));



        cmd.ExecuteNonQuery();

        if (con != null)
        {
            con.Close();
        }





        lblUpdate.Text = "Record updated sucessfully.";


        MultiView1.SetActiveView(ViewGrid);


        GridView1.EditIndex = -1;
        GridView1.DataBind();
    }
}


Please help me identify my mistake.
Thanks a lot.
Posted
Comments
[no name] 11-Jul-13 12:33pm    
Check your return values. Get the SQL and execute it in management studio. Make sure that your 'where' clause is valid. Make sure that you are actually looking at the correct database when verifying that the update did not happen.
[no name] 11-Jul-13 12:39pm    
when I run
UPDATE dbo.Dwg_Register SET Ref = @Ref, Dwg_Ref = @Dwg_Ref,Title = @Title, Dwg_Received_Date = @Dwg_Received_Date, Rev = @Rev, Trade = @Trade, Type=@Type,Produced_Date=@Produced_Date,Produced_By=@Produced_By,Submittal_Ref=@Submittal_Ref,Issued_To=@Issued_To,Date_Issued = Date_Issued, Purpose = @Purpose, Status=@Status,Action_Date=@Action_Date from dbo.Dwg_Register where DwgRegID=@DwgRegID;

in SSMS, it throws this following error:
`Must declare the scalar variable "@Ref".`
[no name] 11-Jul-13 12:42pm    
Of course it does. You need to get the actual SQL that you are trying to execute from your code. NOT the SQL in your code that has not been converted to the real SQL.
joshrduncan2012 11-Jul-13 13:35pm    
I think the OP should change ".Parameters.Add" to ".Parameters.AddWithValue".
[no name] 11-Jul-13 13:55pm    
I do too.

 
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