Search code examples
c#sqltelerikradgridupdatecommand

SQL update command executed but no change to the database


I am quite new to the ASP.NET technology and I stumbled across peculiar problem with my app. I am trying to update a boolean database column to set the value to True (1) whenever a user clicked a button on the RadGridView data rows. The button seems to be working fine as there were no errors or exceptions however the database column did not get updated at all.

Here's a snippet of my code:

ASPX :

<telerik:GridButtonColumn ButtonType="PushButton" ConfirmTextFields="TrxId" 
                    ConfirmTextFormatString="Are you sure you want to release Order No. {0}?" 
                    CommandName="btnRelease" Text="Release Order">
                </telerik:GridButtonColumn>

C# :

protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName == "btnRelease")
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ZEUS"].ConnectionString);

            con.Open();
            SqlCommand command = new SqlCommand("UPDATE [TransactsData] SET [IsReleased] = 1, [TimeReleased] = GETDATE() WHERE [TrxId] = @TrxId", con);
            command.Parameters.AddWithValue("@TrxId", SqlDbType.BigInt);
            command.ExecuteNonQuery();
            con.Close();
        } 
    }

thanks in advance.


Solution

  • Try this:

    protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName == "btnRelease")
        {
            GridDataItem item = (GridDataItem)e.Item; 
    
            // Replace "TrxId" with the reference to the item containing your TrxId
            string TrxId = item["TrxId"].Text;
    
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ZEUS"].ConnectionString);
    
            con.Open();
            SqlCommand command = new SqlCommand("UPDATE [TransactsData] SET [IsReleased] = 1, [TimeReleased] = GETDATE() WHERE [TrxId] = @TrxId", con);
            command.Parameters.AddWithValue("@TrxId", TrxId);
            command.ExecuteNonQuery();
            con.Close();
        } 
    }
    

    Btw: I am supposing you have something like this in your RadGrid:

    <telerik:GridBoundColumn DataField="TrxId" HeaderText="Transaction ID" UniqueName="TrxId">