Column 'xxxxxx' does not belong to table when Adding a empty row to a datagrid

ptown

Newcomer
Joined
Nov 28, 2007
Messages
1
Hello, I have form that allows me to enter multiple items and perform a batch insert into the DB. I want to add a button, Add Row_Click to add aditional rows without losing the data already in the grid, But I am running into problem
using the Add Row Click and receive the error: Column 'CustomerID' does not belong to table. Please tell me what I am doing wrong?

Visual Basic:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim DT As New DataTable
        If (Not Page.IsPostBack) OrElse (Not (TypeOf Session("DT") Is DataTable)) Then

            'Create Empty Datatable
            'DT = CType(gvParts.DataSource, DataTable)
            DT = New DataTable("AddExtraRow")
            'DT.Columns.Add("LN", Type.GetType("System.String"))
            DT.Columns.Add("CustomerID", Type.GetType("System.String"))
            DT.Columns.Add("BarcodeAttached", Type.GetType("System.String"))
            DT.Columns.Add("Barcode", Type.GetType("System.String"))
            DT.Columns.Add("RevisionPart", Type.GetType("System.String"))
            DT.Columns.Add("RevisionDescription", Type.GetType("System.String"))
            DT.Columns.Add("ComponentLoaction", Type.GetType("System.String"))
            DT.Columns.Add("LocationID", Type.GetType("System.String"))
            DT.Columns.Add("Part_SerialNumber", Type.GetType("System.String"))
            DT.Columns.Add("Part_number", Type.GetType("System.String"))
            DT.Columns.Add("UPC", Type.GetType("System.String"))
            DT.Columns.Add("unit_price", Type.GetType("System.String"))
            DT.Columns.Add("PropertyTypeID", Type.GetType("System.String"))
            DT.Columns.Add("ComponentClassification", Type.GetType("System.String"))
            DT.Columns.Add("RecvCondition", Type.GetType("System.String"))
            DT.Columns.Add("Received_Qty", Type.GetType("System.String"))
            DT.Columns.Add("UOI", Type.GetType("System.String"))
            DT.Columns.Add("ConditionCodeID", Type.GetType("System.String"))
            DT.Columns.Add("Time_stamp_Received", Type.GetType("System.String"))
            DT.Columns.Add("Remarks", Type.GetType("System.String"))

            'Store the table in the Session variable
            Session("DT") = DT

            ' Bind the DataGrid to an empty table 
            'gvParts.DataSource = DT
           ' gvParts.DataBind()

        Else
            ' Retrieve the table from the session variable
            DT = CType(Session("DT"), DataTable)
        End If

    End Sub

    Protected Sub AddRow_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddRow.Click

        If (IsNumeric(RowCount.Text)) Then
            Dim i As Integer
            For i = 1 To CInt(RowCount.Text)
                Dim DT As New DataTable
                Dim DR As DataRow = DT.NewRow()
                DR("CustomerID") = RowCount.Text
                DT.Rows.Add(DR)

                'Bind & Show New row(s) in grid
                gvParts.DataSource = DT
                gvParts.DataBind()
            Next
        End If

    End Sub
****************************************************
Reference to the form below is created
*************************

Code:
Receiving Form Example
*****************************
RecivingID______     # of Line Items______ MkGrid

 LineNO | Part# | PartSerial# | PartName | Qty | Remarks
---------------------------------------------------------------------------
1.
2.
3.
4
5
Add Extra Rows Click____

Place a "Make grid" button next to the # of Line Items. When the user has filled in the number and clicked the button, put something like the following in the button click method.......

Create a new datatable and populate a column with the # of Line Items:
Visual Basic:
Protected Sub MakeGrid_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click
Dim dt As New Data.DataTable
        dt.Columns.Add(New Data.DataColumn("LineNO"))
        For i As Integer = 1 To tbItems.Text
            Dim dr As Data.DataRow
            dr = dt.NewRow
            dr("LineNO") = i
            dt.Rows.Add(dr)
        Next
        'Bind the DataTable to the DataGrid
        gvParts.DataSource = dt
        gvParts.DataBind()
The gridview will need to have six columns. The first one will just be a bound column to the LineNO column of the data table. The following Five columns will be template columns with text boxes :
Note I left out the other fields Template fields to save space.
[highlight=asp]
<asp:GridView ID="gvParts" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="PartNo" />
<asp:TemplateField HeaderText="Part #">
<ItemTemplate>
<asp:TextBox ID="part_number" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
[/highlight]
Then when they submit their form, I want to iterate through the rows of the gridview, getting the textbox values and inputting into database

Note I left out the other fields Template fields to save space.

Visual Basic:
Protected Sub AddReceiving_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddReceiving.Click

        'Add new received components to the receiving table
        Dim tblReceiving As New partdb.tblReceivingDataTable()
        For Each gvr As GridViewRow In gvParts.Rows
            If gvr.RowType = DataControlRowType.DataRow Then
                
                'Add a new ProductsRow to the Receiving DataTable
                Dim part_number As TextBox = CType(gvr. 


                'Add a new receiving row to the to the ReceivingDataTable
                Dim newReceiving As partdb.tblReceivingRow = tblReceiving.NewtblReceivingRow()

                ' Assign the values from the web page
                newReceiving.Part_number = part_number.Text

                ' Add any "default" values
                newReceiving.Archive = False
                newReceiving.ProcessFlag = False
                newReceiving.time_stamp_entry = Date.Now
                tblReceiving.AddtblReceivingRow(newReceiving)                
            End If
        Next

     End Sub
 
Last edited by a moderator:
In the Form_Load you are declaring the DT variable - this will fall out of scope when the Form_Load method exits. You are then declaring a new variable (that just happens to share the DT name) in the AddRow_Click method.

You would probably need to store DT in a session variable or similar so it would survive between postbacks.
 
Back
Top