c# - 执行阅读器 : CommandText property has not been initialized when using gridView

标签 c# html css asp.net

我有一个 gridView,我想使用 ExecuteReader 函数填充它。当我运行我的程序时,出现以下错误“ExecuteReader:CommandText 属性尚未初始化”。

aspx.cs

    private void LoadGrid()
    {
    //creates a list of items to be displayed 
        var stockItems = new List<StockUpdate>();

    //creates the SQL command to retrieve the data from the table

        SqlCommand command = new SqlCommand();

        command.CommandText = dsrFilteredBranches.InsertCommand;
        command.Connection = connection;

        connection.Open();

        SqlDataReader rd = command.ExecuteReader();

    //populates the table
        if (rd.HasRows)
        {
            while (rd.Read())
            {
                stockItems.Add(new StockUpdate
                {
                    BranchCode = rd["BranchCode"].ToString(),
                    StockTakeID = Convert.ToInt32(rd["StockTakeID"].ToString()),
                    Name = rd["Name"].ToString(),
                    Description = rd["Description"].ToString(),
                    StocktakeDate = rd["StocktakeDate"].ToString(),
                    Quantity = Convert.ToInt32(rd["Quantity"].ToString())     
                });
            }
        }

        connection.Close();
        dgvStockTake.DataSource = stockItems;
        dgvStockTake.DataBind();

    }

aspx

<asp:GridView ID="dgvStockTake" runat="server" AutoGenerateColumns="False" CssClass="auto-style8" DataKeyNames="BranchCode">

        <%-- creates the editable columns for the gridView --%>        

        <Columns>

        <asp:TemplateField HeaderText="BranchCode">
            <ItemTemplate>
                <asp:Label runat="server" ID="lblBranchCode" Text='<%# Bind("BranchCode") %>'>
                </asp:Label>
            </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="ID">
            <ItemTemplate>
                <asp:Label runat="server" ID="lblID" Text='<%# Bind("StockTakeID") %>'>
                </asp:Label>
            </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Name">
            <ItemTemplate>
                <asp:Label runat="server" ID="lblName" Text='<%# Bind("Name") %>'>
                </asp:Label>
            </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Descrption">
            <ItemTemplate>
                <asp:Label runat="server" ID="lblDescription" Text='<%# Bind("Description") %>'>
                </asp:Label>
            </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="StockTakeDate">
            <ItemTemplate>
                <asp:Label runat="server" ID="lblStocktakeDate" Text='<%# Bind("StocktakeDate") %>'>
                </asp:Label>
            </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Quantity">
            <ItemTemplate>
                <asp:Textbox runat="server" ID="txtQuantity" CssClass="backColor" Text='<%# Bind("Quantity") %>'>
                </asp:Textbox>
            </ItemTemplate>
        </asp:TemplateField>

        </Columns>

        </asp:GridView>

public class StockUpdate
{
    public string BranchCode { get; set; }
    public int StockTakeID { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public string StocktakeDate { get; set; }
    public int Quantity { get; set; }
}

我尝试使用 SQLDataSource 作为我的 gridView 的数据源,但是当我这样做时,我的整个 gridView 及其 HTML 代码都消失了。这就是我使用 DataReader 的原因。

当我用我的实际 SQL 命令替换“dsrFilteredBranches.InsertCommand”时,它给我一个错误,指出标量变量@BranchCode 尚未初始化,我也无法解决该问题。

我已经为此苦苦挣扎了几个小时,我们将不胜感激。

最佳答案

您的 SqlCommand 具有您需要在执行之前设置的参数。你可以这样做:

command.CommandText = dsrFilteredBranches.InsertCommand;

SqlParameter yourParameter = new SqlParameter
{
    ParameterName = "@YourParameter",
    Value = YourParameterValue
};

command.Parameters.Add(yourParameter);

关于c# - 执行阅读器 : CommandText property has not been initialized when using gridView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59193292/

相关文章:

css - 我如何将滑雪板产品与名称和价格联系起来?

c# - this[参数] 语法的用途是什么?

JavaScript 在渲染前更改 div

javascript - 防止 jQuery timepicker 插件自动打开

html - 在 HTML 表格行中使用流畅的 Bootstrap 网格

html - 溢出 x : auto element 内的 100% 宽度

javascript - 通过 jquery 打印隐藏数据

c# - 如何从 Jenkins 的多个测试项目中获取 dotCover 覆盖率报告

c# - 表格未显示在 Visual Studio 2012 中

C# 循环回到代码的前一部分