Jedi Ablaza
Jedi Ablaza

Reputation: 171

null return in CommandArgument

There is no data return in my CommandArgument and Row Command code in my .cs

Here is my .cs code

if(e.CommandName == "ApproveRow")
{
    int index = Convert.ToInt32(e.CommandArgument);

    //int index;
    //bool check = int.TryParse(e.CommandName.ToString(), out index);

    GridViewRow row = GridView1.Rows[index];
    string ids = row.Cells[2].Text;

    Utility u = new Utility();
    string conn = u.connect();
    SqlConnection connUser = new SqlConnection(conn);
    SqlCommand read = connUser.CreateCommand();

    string update = "UPDATE MosefTransaction SET TransStatus = 'Approved' where TransactionID = '" + ids + "'";

    connUser.Open();
    read.CommandText = update;
    //read.Parameters.AddWithValue("@TransactionID", ids);
    read.Parameters.Clear();
    read.ExecuteNonQuery();
}

and here is my aspx code:

<asp:TemplateField HeaderText="Transaction Number" HeaderStyle-ForeColor="White">
    <ItemTemplate>
        <asp:Label ID ="lblmosID" runat="server" Text='<%#Bind ("TransactionID") %>'></asp:Label>
    </ItemTemplate>
    <ItemStyle Width="30px" Font-Size="15px" Font-Names="Calibri" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Date" HeaderStyle-ForeColor="White">
    <ItemTemplate>
        <asp:Label ID ="lblDate" runat="server" Text='<%#Bind ("DateFiled") %>'></asp:Label>
    </ItemTemplate>
    <ItemStyle Width="130px" Font-Names="Calibri" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" HeaderStyle-ForeColor="White">
    <ItemTemplate>
        <asp:Label ID ="lblName" runat="server" Text='<%#Bind ("ReqName") %>'></asp:Label>
    </ItemTemplate>
    <ItemStyle Font-Names="Calibri" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Company" HeaderStyle-ForeColor="White">
    <ItemTemplate>
        <asp:Label ID ="lblComp" runat="server" Text='<%#Bind ("ReqCompany") %>'></asp:Label>
    </ItemTemplate>
    <ItemStyle Font-Names="Calibri" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Branch" HeaderStyle-ForeColor="White">
    <ItemTemplate>
        <asp:Label ID ="lblBranch" runat="server" Text='<%#Bind ("ReqBranch") %>'></asp:Label>
    </ItemTemplate>
    <ItemStyle Font-Names ="Calibri" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Business Unit" HeaderStyle-ForeColor="White">
    <ItemTemplate>
        <asp:Label ID ="lblBU" runat="server" Text='<%#Bind ("ReqBU") %>'></asp:Label>
    </ItemTemplate>
    <ItemStyle Font-Names="Calibri" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Department" HeaderStyle-ForeColor="White">
    <ItemTemplate>
        <asp:Label ID ="lblDept" runat="server" Text='<%#Bind ("ReqDept") %>'></asp:Label>
    </ItemTemplate>
    <ItemStyle Font-Names="Calibri" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Section" HeaderStyle-ForeColor="White">
    <ItemTemplate>
        <asp:Label ID ="lblsection" runat="server" Text='<%#Bind ("ReqSection") %>'></asp:Label>
    </ItemTemplate>
    <ItemStyle Font-Names="Calibri" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Status" HeaderStyle-ForeColor="White">
    <ItemTemplate>
        <asp:Label ID ="lblStatus" runat="server" Text='<%#Bind ("TransStatus") %>'></asp:Label>
    </ItemTemplate>
    <ItemStyle Font-Names="Calibri" />
</asp:TemplateField>


<asp:ButtonField ButtonType="Button" CommandName="ApproveRow" HeaderText="Approve" Text="Approve" ControlStyle-CssClass="btn btn-primary" HeaderStyle-ForeColor="White" HeaderStyle-Font-Names="Calibri" ItemStyle-Font-Names="Calibri"/>

Where is my error? I tried using BoundField and it works, but I need to use the Bind for my batch approve it checkbox. Thanks!

Upvotes: 0

Views: 750

Answers (2)

Daniel Ch. Bloch
Daniel Ch. Bloch

Reputation: 49

First of all, you do not attach any variable to your command read.ExecuteNonQuery();

Second, ExecuteNonQuery returns the number of rows and nothing more.

See https://msdn.microsoft.com/de-de/library/system.data.sqlclient.sqlcommand.executenonquery(v=vs.110).aspx

Upvotes: 2

Tim Schmelter
Tim Schmelter

Reputation: 460138

You can't use Cell.Text if you use TemplateFields and real controls because it's "".

You can use GridViewRow.FindControl:

Label lblmosID = (Label) row.FindControl("lblmosID");
string ids = lblmosID.Text;

But you should really use sql-parameters instead of string concatenation:

string update = @"UPDATE MosefTransaction 
                  SET TransStatus = 'Approved' 
                  Where TransactionID = @TransactionID";
using(var updateCommand = new SqlCommand(update, connUser))
{
    // presuming it's an int
    updateCommand.Parameters.Add("@TransactionID", SqlDbType.Int).Value = int.Parse(lblmosID.Text);
    connUser.Open();
    int affected = updateCommand.ExecuteNonQuery();
}

Upvotes: 2

Related Questions