I would like to get some help here in my code, what am I trying to do is to populate my drop down list which shows the available quantity (stored in database) but when I try to find the control in the row I get a null reference exception it says row was null.
Here is the template field that has my drop down list
<asp:TemplateField HeaderText="Quantity" SortExpression="Quantity">
<EditItemTemplate>
<asp:DropDownList ID="ddlQuantity" runat="server">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("Quantity") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
and my code behind
protected void Cart_GV_RowEditing(object sender, GridViewEditEventArgs e) {
Cart_GV.EditIndex = e.NewEditIndex;
GridViewRow row = (sender as Control).Parent.Parent as GridViewRow;
DropDownList ddlQuantity = row.FindControl("ddlQuantity") as DropDownList;
string cartitemid = Cart_GV.DataKeys[e.NewEditIndex].Value.ToString();
conn.Open();
SqlCommand checkQuantity = new SqlCommand("SELECT OnHandQTY FROM Inventory INNER JOIN Cart_Item ON Inventory.ProductID = Cart_Item.ProductID WHERE CartItemID = '" + cartitemid + "';", conn);
int availalbleQuantity = (int)checkQuantity.ExecuteScalar();
conn.Close();
for (int i = 1; i <= availalbleQuantity; i++) {
ListItem item = new ListItem(i.ToString(), i.ToString());
ddlQuantity.Items.Add(item);
}
ViewUserCart();
}
Please Try this one
protected void Cart_GV_RowEditing(object sender, GridViewEditEventArgs e) {
Cart_GV.EditIndex = e.NewEditIndex;
GridViewRow row = (sender as Control).Parent.Parent as GridViewRow;
DropDownList ddlQuantity = row.FindControl("ddlQuantity") as DropDownList;
string cartitemid = Cart_GV.DataKeys[e.NewEditIndex].Value.ToString();
conn.Open();
SqlCommand cmd = new SqlCommand("SELECT OnHandQTY FROM Inventory INNER JOIN Cart_Item ON Inventory.ProductID = Cart_Item.ProductID WHERE CartItemID = '" + cartitemid + "';", conn);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
con.Close();
ddlQuantity.DataSource = dt;
ddlQuantity.DataTextField = "OnHandQTY";
ddlQuantity.DataValueField = "OnHandQTY";
ddlQuantity.DataBind();
ViewUserCart();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.