Hello I have a table with a few textboxes and when a user clicks the button it gets added to a gridview. It worked fine with Shared dtValues As New DataTable() but I ran into other issues so I changed it to Public …. On the button press is where I get the error Column ‘Qty’ does not belong to the table. Any suggestions?
aspx
<asp:Table ID="tblPrice" runat="server" GridLines ="Both">
<asp:TableRow>
<asp:TableCell>QTY.</asp:TableCell>
<asp:TableCell>MATERIAL</asp:TableCell>
<asp:TableCell>COST</asp:TableCell>
<asp:TableCell>PRICE</asp:TableCell>
<asp:TableCell>TOTAL</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>
<asp:TextBox ID="txtQty" runat="server" Width="40" Text = "1"></asp:TextBox>
</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="txtMaterials" runat="server"></asp:TextBox>
</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="txtCost" runat="server" Width="125" Text = "0" ></asp:TextBox>
<asp:MaskedEditExtender ID="meeCost1" runat="server" Mask="99,999,999.99" DisplayMoney="Left" TargetControlID="txtCost" MaskType="Number">
</asp:MaskedEditExtender>
</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="txtPrice" AutoPostBack="true" runat="server" Width="125" Text = "0" OnTextChanged = "PriceChange"></asp:TextBox>
<asp:MaskedEditExtender ID="meePrice1" runat="server" Mask="99,999,999.99" DisplayMoney="Left" TargetControlID="txtPrice" MaskType="Number">
</asp:MaskedEditExtender>
</asp:TableCell>
<asp:TableCell>
<asp:Label ID="lblTotal" runat="server" Text="$0.00"></asp:Label>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>
<asp:Button ID="btnAddPurchase" runat="server" Text="Add Purchase" OnClick="btnAddPurchase_Click" />
</asp:TableCell>
</asp:TableRow>
</asp:Table>
<br />
<asp:GridView ID="gvPurchases" runat="server" AutoGenerateColumns="False"
AutoGenerateDeleteButton="True" AutoGenerateEditButton="True"
AutoGenerateSelectButton="True" >
</asp:GridView>
code Behind
Public Class _Default
Inherits System.Web.UI.Page
Public dtValues As New DataTable()
Protected Sub Page_Load(sender As Object, e As EventArgs)
If Not IsPostBack Then
CreateDataTable()
End If
End Sub
Protected Sub CreateDataTable()
dtValues.Columns.Add(New DataColumn("Qty"))
dtValues.Columns.Add(New DataColumn("Materials"))
dtValues.Columns.Add(New DataColumn("Cost"))
dtValues.Columns.Add(New DataColumn("Price"))
dtValues.Columns.Add(New DataColumn("Total"))
End Sub
Protected Sub btnAddPurchase_Click(sender As Object, e As EventArgs)
Dim drValues As DataRow = dtValues.NewRow()
drValues("Qty") = txtQty.Text
drValues("Materials") = txtMaterials.Text
drValues("Cost") = txtCost.Text
drValues("Price") = txtPrice.Text
dtValues.Rows.Add(drValues)
gvPurchases.DataSource = dtValues
gvPurchases.DataBind()
txtQty.Text = "1"
txtMaterials.Text = ""
txtCost.Text = "0"
txtPrice.Text = "0"
lblTotal.Text = "$0.00"
End Sub
It is happening because the table is created only on initial page load and when it is posted back via button click, table is again initialized but with no columns since IsPostBack is true in Page_Load event. So, by removing the IsPostBack condition, table object is instantiated with columns for every page request.