[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How could I get get the value of datarow

Posted on 2009-04-30
3
Medium Priority
?
318 Views
Last Modified: 2012-06-22
I has the following situation. kindly go through it first.
In my BAL I have the following function
Public Function Display() As DataRow
        If dt.Columns.Count = 0 Then
            dt.Columns.Add("productname", System.Type.GetType("System.String"))
            dt.Columns.Add("partnumber", System.Type.GetType("System.String"))
            dt.Columns.Add("description", System.Type.GetType("System.String"))
            dt.Columns.Add("quantity", System.Type.GetType("System.Decimal"))
            dt.Columns.Add("price", System.Type.GetType("System.Decimal"))
        End If
        Dim dr As DataRow = dt.NewRow
        dr("productname") = _productname
        dr("partnumber") = _partnumber
        dr("description") = _description
        dr("quantity") = _quantity
        dr("price") = _cost
        'dt.Rows.Add(dr)
        Return dr
    End Function

In my UI I am accessing the above function as follows :
Private Sub DisplayCart()

        Dim CartItem As CartItem
        Dim CartEntry As DictionaryEntry
        Dim dt1 As New DataTable
        Dim dr1 As DataRow
        For Each CartEntry In Cart
            CartItem = CType(CartEntry.Value, CartItem)
            dr1 = dt1.NewRow()
            dr1 = CartItem.Display
            dt1.Rows.Add(dr1)
        Next
        Dim i As Integer
        For i = 0 To dt1.Rows.Count - 1
            Dim prodname As String = dt1.Rows(i).Item(1)
        Next
        GridView1.DataSource = dt1
        GridView1.DataBind()

It is giving me exception.
"This row already belongs to another table."

How to solve this problem. Because I need the data in UI as datatable because I need to set it as datasource in a gridview.
0
Comment
Question by:akulavi
  • 2
3 Comments
 
LVL 20

Accepted Solution

by:
ElrondCT earned 2000 total points
ID: 24270550
A DataRow is always tied to a specific DataTable. In your Display function, you are creating dr as a DataRow belonging to the dt DataTable. But you're then passing that row back as the result of the function, and trying to put it into DataTable dt1.

If you want the DataRow to be part of dt1, when you can pass a reference to dt1 in the function call, and the row will be connected to that when created:


Public Function Display(ByRef dt as DataTable) As DataRow
        If dt.Columns.Count = 0 Then
            dt.Columns.Add("productname", System.Type.GetType("System.String"))
            dt.Columns.Add("partnumber", System.Type.GetType("System.String"))
            dt.Columns.Add("description", System.Type.GetType("System.String"))
            dt.Columns.Add("quantity", System.Type.GetType("System.Decimal"))
            dt.Columns.Add("price", System.Type.GetType("System.Decimal"))
        End If
        Dim dr As DataRow = dt.NewRow
        dr("productname") = _productname
        dr("partnumber") = _partnumber
        dr("description") = _description
        dr("quantity") = _quantity
        dr("price") = _cost
        'dt.Rows.Add(dr)
        Return dr
    End Function
 
In my UI I am accessing the above function as follows :
Private Sub DisplayCart()
 
        Dim CartItem As CartItem
        Dim CartEntry As DictionaryEntry
        Dim dt1 As New DataTable
        Dim dr1 As DataRow
        For Each CartEntry In Cart
            CartItem = CType(CartEntry.Value, CartItem)
            dr1 = dt1.NewRow(dt1)
            dr1 = CartItem.Display
            dt1.Rows.Add(dr1)
        Next
        Dim i As Integer
        For i = 0 To dt1.Rows.Count - 1
            Dim prodname As String = dt1.Rows(i).Item(1)
        Next
        GridView1.DataSource = dt1
        GridView1.DataBind()

Open in new window

0
 
LVL 20

Expert Comment

by:ElrondCT
ID: 24270656
Sorry, I accidentally posted the message before I cleaned up the code snippet. Please ignore the previous code snippet, and use the following instead:

I've moved the Add into Display(), because I'm not sure that passing the row back and then adding it is going to work properly. If you pass a reference to the DataTable, you can add the row in the Sub with no problems.


Public Sub Display(ByRef dt as DataTable)
        If dt.Columns.Count = 0 Then
            dt.Columns.Add("productname", System.Type.GetType("System.String"))
            dt.Columns.Add("partnumber", System.Type.GetType("System.String"))
            dt.Columns.Add("description", System.Type.GetType("System.String"))
            dt.Columns.Add("quantity", System.Type.GetType("System.Decimal"))
            dt.Columns.Add("price", System.Type.GetType("System.Decimal"))
        End If
        Dim dr As DataRow = dt.NewRow
        dr("productname") = _productname
        dr("partnumber") = _partnumber
        dr("description") = _description
        dr("quantity") = _quantity
        dr("price") = _cost
        dt.Rows.Add(dr)
        Return 
    End Function
 
Private Sub DisplayCart()
 
        Dim CartItem As CartItem
        Dim CartEntry As DictionaryEntry
        Dim dt1 As New DataTable
        Dim dr1 As DataRow
        For Each CartEntry In Cart
            CartItem = CType(CartEntry.Value, CartItem)
            CartItem.Display(dt1)
        Next
        Dim i As Integer
        For i = 0 To dt1.Rows.Count - 1
            Dim prodname As String = dt1.Rows(i).Item(1)
        Next
        GridView1.DataSource = dt1
        GridView1.DataBind()

Open in new window

0
 

Author Closing Comment

by:akulavi
ID: 31576376
Thank you very much. Your solution has worked completely. Only "dt1" will be in line 29 in lieu of line 28. It should look like this.

           dr1 = dt1.NewRow            

           dr1 = CartItem.Display (dt1)


Anyway ... thanks...
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month18 days, 14 hours left to enroll

834 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