Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I just want to avoid duplicate data in my datatable.
The error points at
VB
dt.Rows.Add(row)


What is wrong in my code?

SQL
Dim dtSH As DataTable = wfms.formflow.getshopheadbycostcentreid(CostCtr)
 
For i As Integer = 0 To dtSH.Rows.Count - 1

   Dim row As DataRow = dt.NewRow()
   row = dt.NewRow()

    If dt.Rows.Count <> 0 Then

'loop each dt.rows'
      For countrow As Integer = 0 To dt.Rows.Count - 1 

'to determine whether new row equals data in dt'
        If dt.Rows(countrow)("EMPID_T").ToString <> row("EMPID_T").ToString Then

               row("EMPID_T") = dtSH.Rows(i)("EMPID_T")
               row("NAME_T") = dtSH.Rows(i)("NAME_T")
               dt.Rows.Add(row)

         End If

       Next

Else
                  row = dt.NewRow()
                  row("EMPID_T") = dtSH.Rows(i)("EMPID_T")
                  row("NAME_T") = dtSH.Rows(i)("NAME_T")
                  dt.Rows.Add(row)
End If


Next


/// update:

All I want to do is actually retrieve the distinct data and bind into dt. Is there any method to select distinct data into datatable using vb code without using SQL?
Posted
Updated 27-Jul-12 16:54pm
v2
Comments
[no name] 27-Jul-12 23:09pm    
Well you could always use LINQ... it looks like SQL and gets translated to SQL but is not SQL.
Kenneth Haugland 27-Jul-12 23:14pm    
This could just as well be a standard vb program without any database, just a datatable...

I must have been on somthing...

Your code want as bad as I thought
VB
Dim dtNewRow As DataRow   
dtNewRow = resultsDataTable.NewRow()   
dtNewRow.Item("id") = 0   
dtNewRow.Item("name") = "Joe Blogs"  
resultsDataTable.Rows.Add(dtNewRow) 


But Why do you have to For loops that goes through the row, isnt one enough? Cant the complete copy look like this:


VB
Dim dtSH As DataTable = wfms.formflow.getshopheadbycostcentreid(CostCtr)
 
            For i As Integer = 0 To dtSH.Rows.Count - 1
                  Dim row As DataRow = dt.NewRow()
                  row("EMPID_T") = dtSH.Rows(i)("EMPID_T")
                  row("NAME_T") = dtSH.Rows(i)("NAME_T")
                  dt.Rows.Add(row)
            next 


And shouldnt that be all?
 
Share this answer
 
v3
Comments
snamyna 27-Jul-12 22:54pm    
Aftr debugging, by removing dt.Rows.Add(row), the only data that in my datatable is the first data because it enters else structure in If dt.Rows.Count <> 0 Then.
Every data that enter If dt.Rows.Count <> 0 is not displayed.
I have updated my question clearly of what result actually I am looking for.
snamyna 27-Jul-12 23:10pm    
The for loops is used because one input inserted, it may result in several rows.
Using looping in vb.code will decrease the system performance.
One way that comes to my mind is by filtering the datatable with distinct data.

Example:
Data in dt datatable:

//Add new row
ID 1
Name John

//Add new row
ID 2
Name Mary

//Add new row
ID 1
Name John

if bind dt to Gridview1 it becomes:

1 John
2 Mary
1 John

Implement this method to distinct the datatable:

VB
Dim dttbl As DataTable = dt.DefaultView.ToTable(True)

    GridView1.DataSource = dttbl
    GridView1.DataBind()


SO the result becomes:

1 John
2 Mary
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900