【问题标题】:null return in CommandArgument在 CommandArgument 中返回 null
【发布时间】:2016-11-07 03:01:07
【问题描述】:

在我的CommandArgument 和我的 .cs 中的行命令代码中没有数据返回

这是我的 .cs 代码

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();
}

这是我的 aspx 代码:

<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"/>

我的错误在哪里?我尝试使用 BoundField 并且它可以工作,但我需要使用 Bind for my batch 批准它复选框。谢谢!

【问题讨论】:

    标签: c# asp.net gridview commandargument


    【解决方案1】:

    如果您使用 TemplateFields 和实际控件,则不能使用 Cell.Text,因为它是 ""

    你可以使用GridViewRow.FindControl:

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

    但是你真的应该使用 sql-parameters 而不是字符串连接:

    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();
    }
    

    【讨论】:

      【解决方案2】:

      首先,您不要在命令中附加任何变量 read.ExecuteNonQuery();

      其次,ExecuteNonQuery 返回行数,仅此而已。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-12-30
        • 1970-01-01
        • 1970-01-01
        • 2020-01-11
        • 2021-02-22
        • 2015-12-18
        • 2017-02-23
        • 2019-08-03
        相关资源
        最近更新 更多