I am encountering an issue with populating a gridView using the ExecuteReader function in my program. Every time I run it, I receive an error message stating "ExecuteReader : CommandText property has not been initialized".
aspx.cs
private void LoadGrid()
{
//creating a list of items to display
var stockItems = new List<StockUpdate>();
//setting up the SQL command to retrieve data from the table
SqlCommand command = new SqlCommand();
command.CommandText = dsrFilteredBranches.InsertCommand;
command.Connection = connection;
connection.Open();
SqlDataReader rd = command.ExecuteReader();
//filling the table with retrieved data
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">
<%-- creating 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; }
}
I attempted to use a SQLDataSource as the gridView's data source but it caused the entire gridView and its HTML code to disappear. This is why I resort to using the DataReader.
Replacing "dsrFilteredBranches.InsertCommand" with my actual SQL command results in an error mentioning that the scalar variable @BranchCode has not been initialized, and I have yet to resolve this problem.
I have spent hours trying to fix this issue and any assistance would be greatly appreciated.