简体   繁体   English

CommandArgument数据绑定问题

[英]CommandArgument DataBinding Issue

I have a sql dataset gridview table which displays values from the database. 我有一个sql数据集gridview表,该表显示数据库中的值。 I am trying to point my commandargument toward the compName variable within my SQL dataset (as in the code below) but I keep getting the following error: 我试图将我的命令参数指向我的SQL数据集中的compName变量(如下面的代码所示),但是我不断遇到以下错误:

"DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'compName'." “ DataBinding:'System.Data.DataRowView'不包含名称为'compName'的属性。”

My C# code: 我的C#代码:

protected void searchTheDB()
{
    string s = "SELECT compName As 'Company/Organization Name', btcAddr As 'Bitcoin Address', Premium_User as 'Premium User'," + 
    "upvote as 'Upvotes',downvote As 'Downvotes' FROM clientDataTable WHERE compName LIKE '%" + searchBox.Text + "%'";

    try
    {
        SqlConnection forSearch = new SqlConnection(connectionString);
        SqlDataAdapter search = new SqlDataAdapter(s, forSearch);
        DataSet dB = new DataSet();
        search.Fill(dB);
        searchGridView.DataSource = dB;
        searchGridView.DataBind();
        searchBox.Text = String.Empty;
    }
    catch (SqlException exp)
    {
        throw new InvalidOperationException("Sorry, the website is experiencing difficulties, please try again, error: ", exp);
    }
}

asp.net code: asp.net代码:

<asp:GridView ID="searchGridView" runat="server" CellPadding="10" ForeColor="#333333" GridLines="None" Height="161px" Width="935px" CellSpacing="5" HorizontalAlign="Justify" BorderStyle="Solid" OnRowCommand="searchGridView_RowCommand">
                     <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                     <Columns>
                         <asp:TemplateField>
                              <ItemTemplate>
                              <asp:Button ID="DownButton" runat="server" CommandName="Upvote" CommandArgument='<%#Eval("compName")%>' Text="Upvote"> </asp:Button>
                              <asp:Button ID="UpButton" runat="server" CommandName="Downvote" CommandArgument='<%#Eval("compName")%>' Text="Downvote"> </asp:Button>
                              </ItemTemplate>
                         </asp:TemplateField>
                     </Columns>

                     <EditRowStyle BackColor="#999999" />
                     <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                     <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                     <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                     <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                     <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                     <SortedAscendingCellStyle BackColor="#E9E7E2" />
                     <SortedAscendingHeaderStyle BackColor="#506C8C" />
                     <SortedDescendingCellStyle BackColor="#FFFDF8" />
                     <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
                 </asp:GridView>

Thanks a lot for your help! 非常感谢你的帮助!

Note how your query looks like: 注意查询的外观:

SELECT compName As 'Company/Organization Name'

There is no column named compName in resulting set, only Company/Organization Name . 结果集中没有名为compName列,只有Company/Organization Name And therefore the data set object knows nothing about compName . 因此,数据集对象对compName So theoretically your data binding should look like this: 因此,理论上您的数据绑定应如下所示:

CommandArgument='<%#Eval("Company/Organization Name")%>'

In practice however I am not completely sure such a column name will work fine, because of space (less concerned) and forward slash (more concerned). 但是实际上,由于空间(较少关注)和正斜杠(较关注),我不确定该列名是否可以正常工作。 So I would consider simplifying column aliases, or even getting rid of them altogether - you are not going to use them for anything other than data binding anyway, right? 因此,我将考虑简化列别名,甚至完全摆脱它们-您将不会将它们用于除数据绑定之外的任何其他功能,对吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM