【问题标题】:ExecuteReader : CommandText property has not been initialized when using gridViewExecuteReader : 使用 gridView 时 CommandText 属性尚未初始化
【发布时间】:2019-12-05 10:38:36
【问题描述】:

我有一个要使用 ExecuteReader 函数填充的 gridView。当我运行我的程序时,它会给出以下错误“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 尚未初始化,我也无法解决该问题。

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

【问题讨论】:

  • 您在dsrFilteredBranches.InsertCommand 中有什么价值?插入的实际 SQL 命令是什么?
  • 我的命令是SELECT [StockTakeID],Name, Description, StocktakeDate, Quantity FROM man.rptStocktakeActualtest WHERE (BranchCode = @BranchCode) AND (DATEPART(YEAR,[StocktakeDate])=@Year) AND (DATEPART(MONTH,[StocktakeDate])=@Month) ORDER BY Name, StocktakeDate DESC
  • 您的查询中有参数。使用此查询时,您需要在 SQLCommand 对象中添加参数及其值。阅读this,了解如何在 C# 中使用参数化查询
  • @ChetanRanpariya 谢谢我去看看。

标签: c# html css asp.net


【解决方案1】:

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

command.CommandText = dsrFilteredBranches.InsertCommand;

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

command.Parameters.Add(yourParameter);

【讨论】:

  • 非常感谢它的工作!但现在我的 gridView 不再显示,所以我会看看。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多