[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Adding row to dataset results in an error

Posted on 2011-05-10
4
Medium Priority
?
438 Views
Last Modified: 2012-05-11
I am trying to add a datarow to a datatable I have created but I get:

Column 'TitleID' does not belong to table .

The way my code is currently setup, it creates a "junk" row for testing in the SelectedGridNeedData sub. It adds without an issue. The error results in the TitlesGrid_RowDrop sub.

I also tried to reference the columnName of column(0) in the beginning of TitlesGrid_RowDrop with no luck.

PLEASE HELP!
Imports System.Data

Partial Class Sales_SalesPresentationGenerator
    Inherits System.Web.UI.Page
    Dim ds As New DataSet
    Dim dt As New DataTable
    Dim TitleName As DataColumn = New DataColumn("Title", Type.GetType("System.String"))
    Dim TitleID As DataColumn = New DataColumn("TitleID", Type.GetType("System.Int32"))
    Dim Territory As DataColumn = New DataColumn("Territory", Type.GetType("System.String"))
    Dim TerritoryID As DataColumn = New DataColumn("TerritoryID", Type.GetType("System.Int32"))
    Dim Medium As DataColumn = New DataColumn("Medium", Type.GetType("System.String"))
    Dim MediumID As DataColumn = New DataColumn("MediumID", Type.GetType("System.Int32"))

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub SelectedGridNeedData(sender As Object, e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles SelectedGrid.NeedDataSource

        dt = New DataTable()
        dt.Columns.Add(TitleID)
        dt.Columns.Add(TitleName)
        dt.Columns.Add(TerritoryID)
        dt.Columns.Add(Territory)
        dt.Columns.Add(MediumID)
        dt.Columns.Add(Medium)

        Dim temp As DataRow
        temp = dt.NewRow

        temp("Title") = "Test"
        temp("TitleID") = 1
        temp("Territory") = "USA"
        temp("TerritoryID") = 2
        temp("Medium") = "DVD"
        temp("MediumID") = 3
        dt.Rows.Add(temp)

        ds.Tables.Add(dt)
        SelectedGrid.DataSource = dt
    End Sub

    'Add to selected
    Protected Sub TitlesGrid_RowDrop(sender As Object, e As Telerik.Web.UI.GridDragDropEventArgs) Handles TitlesGrid.RowDrop
        'e.DraggedItems.Count
        For Each r In e.DraggedItems

            Dim temp As DataRow
            temp = dt.NewRow()

            temp.Item("TitleID") = CType(CType(r.Item("TitleID"), TableCell).Text, Integer)
            temp.Item("Title") = CType(r.Item("Title"), TableCell).Text
            temp.Item("Medium") = CType(r.Item("Medium"), TableCell).Text
            temp.Item("MediumID") = CType(CType(r.Item("MediumID"), TableCell).Text, Integer)
            temp.Item("Territory") = CType(r.Item("Territory"), TableCell).Text
            temp.Item("TerritoryID") = CType(CType(r.Item("TerritoryID"), TableCell).Text, Integer)
            dt.Rows.Add(temp)
        Next
        SelectedGrid.Rebind()
    End Sub

    'Remove from Selected
    Protected Sub SelectedRowDrop(sender As Object, e As Telerik.Web.UI.GridDragDropEventArgs) Handles SelectedGrid.RowDrop

    End Sub
End Class

Open in new window

0
Comment
Question by:echobridge
  • 2
  • 2
4 Comments
 
LVL 11

Accepted Solution

by:
jasonduan earned 2000 total points
ID: 35732614
SelectedGridNeedData() is not called when TitlesGrid_RowDrop() occurs, therefore, no columns were added to "dt".
Remember: whenever postback occurs, the page has to go through its whole lifecycle again, and all class variables are reinitialized.
try the following code: (moved line 20~26 to Page_Load()).
Imports System.Data

Partial Class Sales_SalesPresentationGenerator
    Inherits System.Web.UI.Page
    Dim ds As New DataSet
    Dim dt As New DataTable
    Dim TitleName As DataColumn = New DataColumn("Title", Type.GetType("System.String"))
    Dim TitleID As DataColumn = New DataColumn("TitleID", Type.GetType("System.Int32"))
    Dim Territory As DataColumn = New DataColumn("Territory", Type.GetType("System.String"))
    Dim TerritoryID As DataColumn = New DataColumn("TerritoryID", Type.GetType("System.Int32"))
    Dim Medium As DataColumn = New DataColumn("Medium", Type.GetType("System.String"))
    Dim MediumID As DataColumn = New DataColumn("MediumID", Type.GetType("System.Int32"))

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        dt = New DataTable()
        dt.Columns.Add(TitleID)
        dt.Columns.Add(TitleName)
        dt.Columns.Add(TerritoryID)
        dt.Columns.Add(Territory)
        dt.Columns.Add(MediumID)
        dt.Columns.Add(Medium)
    End Sub

    Protected Sub SelectedGridNeedData(sender As Object, e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles SelectedGrid.NeedDataSource
       
        Dim temp As DataRow
        temp = dt.NewRow

        temp("Title") = "Test"
        temp("TitleID") = 1
        temp("Territory") = "USA"
        temp("TerritoryID") = 2
        temp("Medium") = "DVD"
        temp("MediumID") = 3
        dt.Rows.Add(temp)

        ds.Tables.Add(dt)
        SelectedGrid.DataSource = dt
    End Sub

    'Add to selected
    Protected Sub TitlesGrid_RowDrop(sender As Object, e As Telerik.Web.UI.GridDragDropEventArgs) Handles TitlesGrid.RowDrop
        'e.DraggedItems.Count
        For Each r In e.DraggedItems

            Dim temp As DataRow
            temp = dt.NewRow()

            temp.Item("TitleID") = CType(CType(r.Item("TitleID"), TableCell).Text, Integer)
            temp.Item("Title") = CType(r.Item("Title"), TableCell).Text
            temp.Item("Medium") = CType(r.Item("Medium"), TableCell).Text
            temp.Item("MediumID") = CType(CType(r.Item("MediumID"), TableCell).Text, Integer)
            temp.Item("Territory") = CType(r.Item("Territory"), TableCell).Text
            temp.Item("TerritoryID") = CType(CType(r.Item("TerritoryID"), TableCell).Text, Integer)
            dt.Rows.Add(temp)
        Next
        SelectedGrid.Rebind()
    End Sub

    'Remove from Selected
    Protected Sub SelectedRowDrop(sender As Object, e As Telerik.Web.UI.GridDragDropEventArgs) Handles SelectedGrid.RowDrop

    End Sub
End Class

Open in new window

0
 
LVL 1

Author Comment

by:echobridge
ID: 35732684
Great! That fixed my "Column 'TitleID' does not belong to table" issue!

Unfortunately, my rows are still not adding to my grid. The code runs without error, but when TitlesGrid_RowDrop is called, nothing is actually added.

I attached my new code (removed junk row). Any input on the new issue?
Imports System.Data

Partial Class Sales_SalesPresentationGenerator
    Inherits System.Web.UI.Page
    Dim ds As New DataSet
    Dim dt As New DataTable
    Dim TitleName As DataColumn = New DataColumn("Title", Type.GetType("System.String"))
    Dim TitleID As DataColumn = New DataColumn("TitleID", Type.GetType("System.Int32"))
    Dim Territory As DataColumn = New DataColumn("Territory", Type.GetType("System.String"))
    Dim TerritoryID As DataColumn = New DataColumn("TerritoryID", Type.GetType("System.Int32"))
    Dim Medium As DataColumn = New DataColumn("Medium", Type.GetType("System.String"))
    Dim MediumID As DataColumn = New DataColumn("MediumID", Type.GetType("System.Int32"))

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        dt = New DataTable()
        dt.Columns.Add(TitleID)
        dt.Columns.Add(TitleName)
        dt.Columns.Add(TerritoryID)
        dt.Columns.Add(Territory)
        dt.Columns.Add(MediumID)
        dt.Columns.Add(Medium)
    End Sub

    Protected Sub SelectedGridNeedData(sender As Object, e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles SelectedGrid.NeedDataSource
        SelectedGrid.DataSource = dt
    End Sub

    'Add to selected
    Protected Sub TitlesGrid_RowDrop(sender As Object, e As Telerik.Web.UI.GridDragDropEventArgs) Handles TitlesGrid.RowDrop
        'e.DraggedItems.Count
        For Each r In e.DraggedItems

            Dim temp As DataRow
            temp = dt.NewRow()

            temp.Item("TitleID") = CType(CType(r.Item("TitleID"), TableCell).Text, Integer)
            temp.Item("Title") = CType(r.Item("Title"), TableCell).Text
            temp.Item("Medium") = CType(r.Item("Medium"), TableCell).Text
            temp.Item("MediumID") = CType(CType(r.Item("MediumID"), TableCell).Text, Integer)
            temp.Item("Territory") = CType(r.Item("Territory"), TableCell).Text
            temp.Item("TerritoryID") = CType(CType(r.Item("TerritoryID"), TableCell).Text, Integer)
            dt.Rows.Add(temp)
        Next
        SelectedGrid.Rebind()
    End Sub

    'Remove from Selected
    Protected Sub SelectedRowDrop(sender As Object, e As Telerik.Web.UI.GridDragDropEventArgs) Handles SelectedGrid.RowDrop

    End Sub
End Class

Open in new window

0
 
LVL 11

Assisted Solution

by:jasonduan
jasonduan earned 2000 total points
ID: 35732787
In order to do that, you can "persist" the "dt" between postbacks.


remove line #5: Dim dt As New DataTable

add the following: (this is C# code, please convert it to VB.net)

private DataTable dt  
{

   get { return ViewState["dt"] as DataTable; }
   set { ViewState["dt"] = value; }
}

Modify Page_Load() to add dt columns when page is first visited.

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
     If Not this.IsPostBack
       Begin

 
        dt = New DataTable()
        dt.Columns.Add(TitleID)
        dt.Columns.Add(TitleName)
        dt.Columns.Add(TerritoryID)
        dt.Columns.Add(Territory)
        dt.Columns.Add(MediumID)
        dt.Columns.Add(Medium)

      End
End Sub
0
 
LVL 1

Author Comment

by:echobridge
ID: 35732932
WOO! The data flow is working (the control is not, but that will just take some tooling around).

Really appreciate the help!
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Creating a Cordova application which allow user to save to/load from his Dropbox account the application database.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question