2

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.

Lucas
  • 3,376
  • 6
  • 31
  • 46
learner
  • 73
  • 1
  • 8
  • 1
    Have you run the application in debug mode? Are you sure that the code ran? Try creating a breakpoint at the line: if (e.commandName.... and step through to make sure it ran. – twoleggedhorse Jul 23 '13 at 08:05
  • im not sure about telerik but i know a standard asp: button would need a runat="server" in there – Dev N00B Jul 23 '13 at 08:18
  • 1
    I don't get this: command.Parameters.AddWithValue("@TrxId", SqlDbType.BigInt) The second parameter should be the actual value if @TrxId but you're passing a data type. Reference: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparametercollection.addwithvalue.aspx – ᗩИᎠЯƎᗩ Jul 23 '13 at 08:18
  • @Anreaoid, thanks for the insight, this might be the issue here, however I have tried several other methods to "grab" the selected TrxId value of which row that the user actually clicked... – learner Jul 23 '13 at 08:44
  • 1
    @DevN00B for gridcolumn controls runat=server declaration is not needed :) – learner Jul 23 '13 at 09:06
  • @twoleggedhorse yes I did put breakpoints at almost everyline in the method and it went without a hitch... – learner Jul 23 '13 at 09:08

2 Answers2

3

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"> 
ᗩИᎠЯƎᗩ
  • 2,122
  • 5
  • 29
  • 41
1

Try like this. i think you missed the value of @TrxId

var TrxId = 10;//The value of @TrxId parameter
command.Parameters.Add(new SqlParameter("@TrxId",TrxId));

Or like this

var TrxId = 10;//The value of @TrxId parameter
command.Parameters.AddWithValue("@TrxId",TrxId);
Nithesh Narayanan
  • 11,481
  • 34
  • 98
  • 138
  • Hi Nithesh, I've tested that it works fine as expected, however this will not satisfy the requirements of the app i'm building. I have to somehow pass the value of the currently clicked row TrxId to the @TrxId parameter. I've used datakey method but still produces no result. I appreciate your effort tho.. (i cant vote up without reps +1 ) – learner Jul 23 '13 at 09:01