I have a datatable that has all the data in it but when my VB.net program runs it and I make a temptable, the temptable has no info. WHat am I doing wrong?
Public Sub HTSCode()
Dim TempTable As New DataTable
Dim DV As DataView
TempTable = RatesDataSet.HTS
DV = TempTable.DefaultView
DV.Sort = "HTS Code NA"
TempTable = DV.ToTable
For Each Row As DataRow In TempTable.Rows
'doesnt get to this point cause there are no rows.
Next
End Sub
I am attaching pictures 1 of my datatable before I run it so there is info there and the second is when running it shows it empty. I am now even getting the data directly from the table and not a copy of it or temptable anymore.
The answer is your problem lies elsewhere. Using this sample to simulate your code, I can't ever get an object with no rows
Public Class ds
Public ReadOnly Property HTS As DataTable
Get
Dim dt As New DataTable()
dt.Columns.AddRange(
{
New DataColumn("HTS Code", GetType(Integer)),
New DataColumn("HTS Code NA", GetType(Integer))
})
For i = 0 To 4
Dim row = dt.NewRow()
row("HTS Code") = i
row("HTS Code NA") = 10 - i
dt.Rows.Add(row)
Next
Return dt
End Get
End Property
End Class
Dim RatesDataSet = New ds
Dim TempTable As DataTable
Dim DV As DataView
TempTable = RatesDataSet.HTS
Console.WriteLine(TempTable.Rows.Count)
DV = TempTable.DefaultView
Console.WriteLine(DV.Count)
DV.Sort = "HTS Code NA"
Console.WriteLine(DV.Count)
TempTable = DV.ToTable
Console.WriteLine(TempTable.Rows.Count)
Console.WriteLine("HTS Code NA:")
For Each Row As DataRow In TempTable.Rows
Console.WriteLine(Row("HTS Code NA"))
Next
Output
5
5
5
5
HTS Code NA:
6
7
8
9
10
I think it was my dumbness I never linked the table to that form. As soon as I did that the table was filled and works. Sorry and thanks
Related
How we can get a data table raw data into a Single type Array variable?
Like;
Dim price_range1() As Single = {10.4, 9.6, 6.8, 5.6, 4.4}
Dim price_range2() As Single = {5.2, 4.8, 3.4, 2.8, 2.2}
Dim price_range3() As Single = {2.6, 2.4, 1.7, 1.4, 1.1}
I'm already getting all data into the DataGrid. But I need to get those raw data as a variable.
DataGridView Table
Like that kind of variables
Imports MySql.Data.MySqlClient
Public Class showitems
Public commmand As New MySqlCommand
Public adapter As New MySqlDataAdapter
Public datatable As New DataTable
Private Sub showitems_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim var As New ArrayList
If Db_conn() = True Then
Dim sql As String
sql = "SELECT id, range1,range2,range3,range4,range5 FROM `pioltprice` WHERE id = 5 OR id = 6 OR id = 7 "
command.CommandText = sql
command.Connection = conn
adapter.SelectCommand = command
adapter.Fill(datatable)
DataGridView1.DataSource = datatable
Else
MsgBox("Error occurred")
End If
End Sub
End Class
Use MySql DataTable, MySql DataAdapter
You will need to take the values and include them in your array.
If you want to reuse the DataTable that you're already filling, then you could do the following after adapter.Fill(datatable):
Get the Row by its index
Get the ItemArray of the selected DataRow
Skip the first cell in the ItemArray (which is the Id)
Convert the values to a Single
Dim price_range1() As Single
If (datatable.Rows().Count > 0) Then
price_range1 = datatable.Rows().Item(0).ItemArray().Skip(1).Select(Function(cell) Single.Parse(cell)).ToArray()
End If
Dim price_range2() As Single
If (datatable.Rows().Count > 1) Then
price_range2 = datatable.Rows().Item(1).ItemArray().Skip(1).Select(Function(cell) Single.Parse(cell)).ToArray()
End If
Dim price_range3() As Single
If (datatable.Rows().Count > 2) Then
price_range3 = datatable.Rows().Item(2).ItemArray().Skip(1).Select(Function(cell) Single.Parse(cell)).ToArray()
End If
Live Demo: https://dotnetfiddle.net/pdEAkz
i am trying to create tree view and some error i can not fix it
Sub CREATENODE()
Dim TRN As New TreeNode
Dim DT As New DataTable
DT.Clear()
DT = ACCOUNTTableAdapter.TREE_ACCOUNT()
For I As Integer = 0 To DT.Rows.Count - 1
If DT.Rows(I)(9).ToString() = "00000000-0000-0000-0000-000000000000" Then
TRN = New TreeNode(DT.Rows(I)(3).ToString() + " " + DT.Rows(I)(4).ToString())
TRN.Tag = DT.Rows(I)(1).ToString()
If DT.Rows(I)(7).ToString() <> "0" Then
TRN.ImageIndex = 0
TRN.SelectedImageIndex = 0
Else
TRN.ImageIndex = 1
TRN.SelectedImageIndex = 1
End If
TreeView1.Nodes.Add(TRN)
End If
Next
''For Each NODE As TreeNode In TreeView1.Nodes
'' CHELD(NODE)
'Next
End Sub
This is nonsense
Dim TRN As New TreeNode
Dim DT As New DataTable
DT.Clear()
DT = ACCOUNTTableAdapter.TREE_ACCOUNT()
You create a New TreeNode and then inside the loop you overwrite it with another New one. Just put the Dim inside the loop after the if.
Dim TRN = New TreeNode($"{row(3)} {row(4)}")
You create a brand new DataTable. Then you clear it when it can't possibly have anything in it. Then you throw it away and assign a different DataTable to it.
Just do
Dim DT = ACCOUNTTableAdapter.TREE_ACCOUNT()
I have simplified your code by using a For Each loop. Also, I used and interpolated string indicated by the $ preceding the string. Variables can be inserted in place surrounded by braces { }.
As far as the actual problem, you need to create an instance of your table adapter with the New keyword. Then call the appropriate method. A simple application will just use .GetData.
Private Sub CREATENODE()
Dim DT = (New ACCOUNTTableAdapter).TREE_ACCOUNT() 'See what intellisense offers. It may be just .GetData
For Each row As DataRow In DT.Rows
If row(9).ToString() = "00000000-0000-0000-0000-000000000000" Then
Dim TRN = New TreeNode($"{row(3)} {row(4)}")
TRN.Tag = row(1)
If row(7).ToString() <> "0" Then
TRN.ImageIndex = 0
TRN.SelectedImageIndex = 0
Else
TRN.ImageIndex = 1
TRN.SelectedImageIndex = 1
End If
TreeView1.Nodes.Add(TRN)
End If
Next
End Sub
Hint: Why not put the entire row in the Tag property. You will have access to all the fields by casting the Tag back to a DataRow.
I try to flip dataset to display column as rows by using this code but it does not work :
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button.Click
Dim ds2 As New DataSet
Dim dt2 As New DataTable
Dim com1 As String = "select col1,col2,col3 from table1"
ds2 = FlipDataSet(ds2)
Dim dp As New SqlDataAdapter(com1, conn)
dp.Fill(dt2)
DGV_lev1.DataSource = dt2.DefaultView
End Sub
and use this function to flip dataset :
Private Function FlipDataSet(old_DataSet As DataSet) As DataSet
Dim ds As New DataSet()
For Each dt As DataTable In old_DataSet.Tables
Dim table As New DataTable()
For i As Integer = 0 To dt.Rows.Count
table.Columns.Add(Convert.ToString(i))
table.Columns(0).ColumnName = "Fields"
If i = 0 Then
Continue For
Else
table.Columns(i).ColumnName = "Customer " & i
End If
Next
Dim r As DataRow
For k As Integer = 0 To dt.Columns.Count - 1
r = table.NewRow()
r(0) = dt.Columns(k).ToString()
For j As Integer = 1 To dt.Rows.Count
r(j) = dt.Rows(j - 1)(k)
Next
table.Rows.Add(r)
Next
ds.Tables.Add(table)
Next
Return ds
End Function
to make datagirdview display from this :
to this :
can anyone help me
thank you
Try this, it worked in a quick test I did:
Private Function Transpose(ByVal table As DataTable) As DataTable
Dim flippedTable As New DataTable
'creates as many columns as rows in source table
flippedTable.Columns.AddRange(
table.Select.Select(
Function(dr) New DataColumn("col" & table.Rows.IndexOf(dr), GetType(Object))
).ToArray)
'iterates columns in source table
For Each dc As DataColumn In table.Columns
'get array of values of column in each row and add as new row in target table
flippedTable.Rows.Add(table.Select.Select(Function(dr) dr(dc)).ToArray)
Next
Return flippedTable
End Function
I need your help for my code vb
In fact, i created an new datatable and i want to copy the headers from another datatable
here is my code
Dim name(de.Tables(0).Columns.Count) As String
Dim p As Integer = 0
For Each column As DataColumn In de.Tables(0).Columns
name(p) = column.ColumnName
p += 1
Next
Dim m As Integer = 0
For m = 0 To de.Tables(0).Columns.Count - 1
dt.Columns(m).ColumnName = name(p)
Next
If you only want to "copy" the schema of a DataTable(so the columns and constraints) without it's content(DataRows) you can use DataTable.Clone:
Dim clonedTable As DataTable = originalTable.Clone()
If you also want to copy the DataRows you have to use DataTable.Copy.
Try this one
Dim dt As New DataTable()
Dim name(de.Tables(0).Columns.Count) As String
For Each column As DataColumn In de.Tables(0).Columns
dt.Columns.Add(New DataColumn(column.ColumnName))
Next
I'm trying to define a datarow to hold values and then add it to data set
indgv: datagridview with values in it
dsdetails: a dataset with a table named details
If indgv.Rows.Count > 0 Then
Dim dr As DataRow
dr = dsdetails.Tables("details").NewRow
For Each row As DataGridViewRow In indgv.Rows
dr("mat") = row.Cells("icode").Value
dr("dateoftrans") = Me.DateTimePicker1.Value
dr("numoftrans") = transnum.Text
dr("type") = 1
dr("doc") = doctyp.SelectedValue
dr("amount") = row.Cells("iamo").Value
dsdetails.Tables("details").Rows.Add(dr)
Next
adpdetails.Update(dsdetails, "details")
End If
running this causes the following error
Object reference not set to an instance of an object.
how to rephrase the declaration with 'New' to avoid the problen
BTW : when using new as the following
Dim dr As New DataRow = dsdetails.Tables("details").NewRow
it shows design time error
Type 'dsdetails.Tables' is not defined.
Try this code:
If indgv.Rows.Count > 0 Then
Dim tbl As DataTable = dsdetails.Tables("details")
Dim dr As DataRow
For Each row As DataGridViewRow In indgv.Rows
dr = tbl.NewRow 'Create a new row inside the loop!
dr("mat") = row.Cells("icode").Value
dr("dateoftrans") = Me.DateTimePicker1.Value
dr("numoftrans") = transnum.Text
dr("type") = 1
dr("doc") = doctyp.SelectedValue
dr("amount") = row.Cells("iamo").Value
tbl.Rows.Add(dr)
Next
adpdetails.Update(tbl)
End If
If all you need is to copy rows from one table to another, DataTable class has a Copy method you may want to use. It works like this:
Dim dtCopy As New DataTable()
dtCopy = dt.Copy()
If you have a datagridview control bound to a table, you could also use this form:
Dim dtCopy As New DataTable()
dtCopy = DirectCast(dataGridViewX1.DataSource, DataTable).Copy()
The Copy method will copy the datatable structure and the data.
If you want to copy the structure only without the data you could use Clone method.