gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-27 22:57:19 24 4
gpt4 key购买 nike

我有一个 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com