ExecuteReader : 使用 gridView 时尚未初始化 CommandText 属性



我有一个网格视图,我想使用ExecuteReader函数填充它。当我运行我的程序时,它给出以下错误"执行阅读器:命令文本属性尚未初始化"。

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>

.class

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);

最新更新