首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CommandArgument中的空返回

CommandArgument中的空返回
EN

Stack Overflow用户
提问于 2016-07-05 10:07:13
回答 2查看 593关注 0票数 0

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

这是我的.cs代码

代码语言:javascript
复制
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代码:

代码语言:javascript
复制
<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作为我的批处理批准它复选框。谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-07-05 10:11:43

如果使用Cell.Text和真正的控件,就不能使用TemplateFields,因为它是""

您可以使用GridViewRow.FindControl

代码语言:javascript
复制
Label lblmosID = (Label) row.FindControl("lblmosID");
string ids = lblmosID.Text;

但是,您应该真正使用sql-参数,而不是字符串连接:

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2016-07-05 10:15:40

首先,您没有将任何变量附加到命令read.ExecuteNonQuery();

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

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38200639

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档