Assign value for each label name - vb.net

Can someone teach me how to assign a value for each label with the same name?.
For example, I have a 10 label with the name label1 until label10. On each label,i want to show different value from sql. This is my code.
Dim sqlCmd As New SqlCommand("select distinct top 15 Machine_no from table5 ", conn)
Dim sqlDa As New SqlDataAdapter(sqlCmd)
sqlDa.Fill(dt)
If dt.Rows.Count > 0 Then
For i As Integer = 0 To dt.Rows.Count
lblMachine1.text = dt.Rows(0)("Machine_no").ToString)
lblMachine2.Text = dt.Rows(1)("Machine_no").ToString
lblMachine3.Text = dt.Rows(2)("Machine_no").ToString
lblMachine4.Text = dt.Rows(3)("Machine_no").ToString
lblMachine5.Text = dt.Rows(4)("Machine_no").ToString
lblMachine6.Text = dt.Rows(5)("Machine_no").ToString
lblMachine7.Text = dt.Rows(6)("Machine_no").ToString
lblMachine8.Text = dt.Rows(7)("Machine_no").ToString
lblMachine9.Text = dt.Rows(8)("Machine_no").ToString
lblMachine10.Text = dt.Rows(9)("Machine_no").ToString
lblMachine11.Text = dt.Rows(10)("Machine_no").ToString
lblMachine12.Text = dt.Rows(11)("Machine_no").ToString
lblMachine13.Text = dt.Rows(12)("Machine_no").ToString
lblMachine14.Text = dt.Rows(13)("Machine_no").ToString
lblMachine15.Text = dt.Rows(14)("Machine_no").ToString
Next
End If
The problem is, when no data in rows 5 for example, the system will give an error.

Welcome to the site!
Before we address your problem, I would like to point out that:
"The problem is, when no data in rows 5 for example, the system will
give an error."
is too broad to really identify your issue.
It would be very helpful to also include the error message you are receiving.
Regarding your real problem, your code can be shortened with the use of DirectCast. You can try this one:
If dt.Rows.Count > 0 Then
For i = 0 To dt.Rows.Count - 1
DirectCast(Controls.Find("lblMachine" & i + 1, True)(0), Label).Text = dt.Rows(i).Item("MachineNo").ToString
Next
End If
Using For Loop in your code is a good start but you didn't integrate it at all. In the code above, you can see how i is being used.
Also dt is a DataTable right? DataTables are index-based so it's starting with zero(0). That's why you can notice in the the For Loop statement the dt.Rows.Count - 1.
Hope it helps.

https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/for-each-next-statement
Also try searching for:
Vb.net for each control loop
You will more than likely have to make a direct object reference using a List(Of Label)
Here is my take on it. Untested.
Dim machList As List(Of Label) = New List(Of Label)
For Each label As Label In GroupBox1.Controls
machList.Add(label)
Next label
For row As Integer = 0 To dt.Rows.Count
If dt.Row(row) <> Nothing Then
machList.Index(row).Text = dt.Rows(row)
End If
Next row

You are using i as a loop variable but you are not using to reference your labels.
You can try to put your labels in an array and then match them up to a row by index like this:
Dim labels = {lblMachine1, lblMachine2, lblMachine3, lblMachine4,...}
Dim sqlCmd As New SqlCommand("select distinct top 15 Machine_no from table5 ", conn)
Dim sqlDa As New SqlDataAdapter(sqlCmd)
sqlDa.Fill(dt)
If dt.Rows.Count > 0 Then
For i As Integer = 0 To dt.Rows.Count -1
dim row= dt.rows(i)
labels(i).text = row("Machine_no")
Next
End If
You can also use FindControl for windows formas #Allen points out. But when using for Web you need to implement a recursive version because the control you are looking for is usually nested within some other control.
private FindControlRecursive(control parent, string id) as control
if parent.id=id then return parent
for each child in parent.controls
control result = FindControlRecursive(child, id)
if not result is nothing then return result
next
end function
Then in your code:
Dim sqlCmd As New SqlCommand("select distinct top 15 Machine_no from table5 ", conn)
Dim sqlDa As New SqlDataAdapter(sqlCmd)
sqlDa.Fill(dt)
If dt.Rows.Count > 0 Then
For i As Integer = 0 To dt.Rows.Count -1
fincontrolrecursive("lblMachine" + i).text = dt.rows(i)("Machine_no")
Next
End If

Related

Temp table has no information but original table does

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

value of type integer cannot be converted to datatable vn.net

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.

Trying to add a list of suppliers in an array and then i am adding it to listbox and combobox using `.addrange` but i am receiving error

I retrieved list of suppliers from database and saved it in an array TOTAL_SUPPLIERS_ARRAY NOW i am trying it to add in the listbox or combobox but it shows an error on runtime saying "VALUE CAN NOT BE NULL" but if i try to add it with an loop it works properly why is it not working with .addrange ?
Sub GET_SUPPLIERS_DETAILS()
Dim CON As New MySqlConnection("server=localhost; username=root; password=Masoom1; database=airtech_db;")
Dim cmd As New MySqlCommand("Select * from `Suppliers`;", CON)
Dim da As New MySqlDataAdapter("Select * from `Suppliers`;", CON)
Dim ds As New DataSet
Dim dr As MySqlDataReader
Dim TOTAL_SUPPLIERS As Integer
CON.Open()
da.Fill(ds)
dr = cmd.ExecuteReader
TOTAL_SUPPLIERS = ds.Tables(0).Rows.Count
Dim TOTAL_SUPPLIERS_ARRAY(TOTAL_SUPPLIERS) As String, ARRAYINDEX As Integer
Do While dr.Read() = True
TOTAL_SUPPLIERS_ARRAY(ARRAYINDEX) = dr("Supplier_Name").ToString()
ARRAYINDEX += 1
Loop
CON.Close()
Dim cbCell As New DataGridViewComboBoxCell
For k = 0 To DataGridView1.Rows.Count - 1
cbCell = DataGridView1.Rows(k).Cells("Supplier_Name")
For iIndex = 0 To UBound(TOTAL_SUPPLIERS_ARRAY) - 1
cbCell.Items.Add(TOTAL_SUPPLIERS_ARRAY(iIndex))
Next
Next
ListBox1.Items.AddRange(TOTAL_SUPPLIERS_ARRAY)
ComboBox1.Items.AddRange(TOTAL_SUPPLIERS_ARRAY)
For I As Integer = 0 To UBound(TOTAL_SUPPLIERS_ARRAY) - 1
TextBox1.Text += TOTAL_SUPPLIERS_ARRAY(I) & " - "
Next I
End Sub
This part works only as i added it through a loop
For I As Integer = 0 To UBound(TOTAL_SUPPLIERS_ARRAY) - 1
TextBox1.Text += TOTAL_SUPPLIERS_ARRAY(I) & " - "
Next I
listbox.addrange command and combobox.addrange command not working
Here you have an issue TOTAL_SUPPLIERS_ARRAY(TOTAL_SUPPLIERS). Array declared in VB - array(upper-bound). Upper bound is 1 less than count. So, you should do TOTAL_SUPPLIERS_ARRAY(TOTAL_SUPPLIERS - 1).
But why struggle? Just use list
Dim myList as New List(of String)
While dr.Read()
myList.Add(dr("Supplier_Name").ToString())
Loop
And then, if you still need array, you can use LINQ - myList.ToArray
ListBox1.DataSource = myList.ToArray()

letting user type and add value to an already bound combobox

I have one combobox in my app that gets populated with a dozen or so items from SQL SERVER. I am currently trying to allow user to be able to type in a new value into the combobox and save it to a table in SQL SERVER. I'm looking for something along the lines of a DropDownStyle of DropDown and DropDownList. Basically I'm hoping to let the user type in the combobox, however if the value is not there, i want to be able to give them an option of saving it (probably on lost focus). I'm wondering what would be a good way of doing something like that.
On lost focus should I basically go through each item that is currently in the drop down and check it against the value entered?
EDIT:
Sql="SELECT IDvalue, TextName from TblReference"
Dim objconn As New SqlConnection
objconn = New SqlConnection(conn)
objconn.Open()
Dim da As New SqlDataAdapter(sql, objconn)
Dim ds As New DataSet
da.Fill(ds, "Name")
If ds.Tables("Name").Rows.Count > 0 Then
Dim dr As DataRow = ds.Tables("Name ").NewRow
ds.Tables("Name").Rows.InsertAt(dr, 0)
With c
.DataSource = ds.Tables("Name")
.ValueMember = " IDvalue "
.DisplayMember = " TextName "
End With
End If
You are already adding a fake/blank row to a table, you can do the same thing for a new item.
' form level datatable var
Private cboeDT As DataTable
Initializing:
cboeDT = New DataTable
Dim sql = "SELECT Id, Descr FROM TABLENAME ORDER BY Descr"
Using dbcon As New MySqlConnection(MySQLConnStr)
Using cmd As New MySqlCommand(sql, dbcon)
dbcon.Open()
cboeDT.Load(cmd.ExecuteReader())
' probably always need this even
' when there are no table rows (???)
Dim dr = cboeDT.NewRow
dr("Id") = -1 ' need a way to identify it
dr("Descr") = ""
cboeDT.Rows.InsertAt(dr, 0)
End Using
End Using
cboeDT.DefaultView.Sort = "Descr ASC"
cboE.DataSource = cboeDT
cboE.DisplayMember = "Descr"
cboE.ValueMember = "Id"
Note Users tend to have a preference as to the order of these things. The simple creatures tend to prefer alphabetical over a DB Id they may never see. To accommodate them, the DefaultView is sorted so that any new rows added will display in the correct order.
Add new items in the Leave event (much like Steve's):
Private Sub cboE_Leave(sender ...
' if it is new, there will be no value
If cboE.SelectedValue Is Nothing Then
' alternatively, search for the text:
'Dim item = cboeDT.AsEnumerable().
' FirstOrDefault(Function(q) String.Compare(q.Field(Of String)("Descr"),
' cboE.Text, True) = 0)
'If item Is Nothing Then
' ' its new...
Dim newid = AddNewItem(cboE.Text)
Dim dr = cboeDT.NewRow
dr("Id") = newid
dr("Descr") = cboE.Text
cboeDT.Rows.Add(dr)
' fiddling with the DS looses the selection,
' put it back
cboE.SelectedValue = newid
End If
End Sub
If you want to search by text:
Dim item = cboeDT.AsEnumerable().
FirstOrDefault(Function(q) String.Compare(q.Field(Of String)("Descr"),
cboE.Text, True) = 0)
If item Is Nothing Then
' its new...
...
Inserting will vary a little depending on the actual db. A key step though is to capture and return the ID of the new item since it is needed for the CBO:
Private Function AddNewItem(newItem As String) As Int32
Dim sql = "INSERT INTO MY_TABLE (Descr) VALUES (#v); SELECT LAST_INSERT_ID();"
Dim newId = -1
Using dbcon As New MySqlConnection(MySQLConnStr)
Using cmd As New MySqlCommand(sql, dbcon)
dbcon.Open()
cmd.Parameters.Add("#v", MySqlDbType.String).Value = newItem
' MySql provides it in the command object
If cmd.ExecuteNonQuery() = 1 Then
newId = Convert.ToInt32(cmd.LastInsertedId)
End If
End Using
End Using
Return newId
End Function
As noted MySql provides the LastInsertedID as a command object property. In SQL SERVER, tack ...";SELECT LAST_INSERT_ID();" to the end of your SQL and then:
newId = Convert.ToInt32(cmd.ExecuteScalar())
This is not conceptually very different from Steve's answer, except it uses the DataTable you build rather than collections which makes it (very) slightly simpler.
The way I do this, is on window load I perform a new SQL query to get the list of values in the table, and load them into a combobox.
Then once focus is lost, it then checks what's currently typed into the combobox against the current values already loaded. If it doesn't exist, then it's not in SQL. Something like the following...
Dim found As Boolean = False
For i As Integer = 0 To comboBox.Items.Count - 1
Dim value As String = comboBox.Items(i).ToString()
If comboBox.Text = value Then
found = True
End If
Next
If found = False Then
'the item doesn't exist.. add it to SQL
Else
'the item exists.. no need to touch SQL
End If
First thing I would do is to build a simple class to hold your values through a List of this class
Public Class DataItem
Public Property IDValue As Integer
Public Property TextName as String
End Class
Now, instead of building an SqlDataAdapter and fill a dataset, work with an SqlDataReader and build a List(Of DataItem)
' Class global...
Dim allItems = new List(Of DataItem)()
Sql="SELECT IDvalue, TextName from TblReference"
' Using to avoid leaks on disposable objects
Using objconn As New SqlConnection(conn)
Using cmd As New SqlCommand(Sql, objconn)
objconn.Open()
Using reader = cmd.ExecuteReader()
While reader.Read()
Dim item = new DataItem() With { .IDValue = reader.GetInt32(0), .TextName = reader.GetString(1)}
allItems.Add(item)
End While
End Using
if allItems.Count > 0 Then
allItems.Insert(0, new DataItem() With {.IDValue = -1, .TextValue = ""}
Dim bs = new BindingList(Of DataItem)(allItems)
c.DataSource = bs
c.ValueMember = "IDvalue"
c.DisplayMember = "TextName"
End If
End Using
End Using
Now the code that you want to add to your Leave event for the combobox
Sub c_Leave(sender As Object, e As EventArgs) Handles c.Leave
If Not String.IsNullOrEmpty(c.Text) Then
Dim bs = DirectCast(c.DataSource, BindingList(Of DataItem))
if bs.FirstOrDefault(Function(x) x.TextName = c.Text) Is Nothing Then
Dim item = new DataItem() With { .IDValue = -1, .TextName = c.Text}
bs.Add(item)
' here add the code to insert in the database
End If
End If
End Sub

How to find the original position in a DataTable from a filtered DataView

I want to find a particular ID in a DataTable, I am using a DataView to filter the results, but how do I know which row in the original table the filter view corresponds to? Any ideas? In know I could use LINQ but I don't think this would help either?
Dim dt As New DataTable
Dim dv As New DataView(dt)
dv.RowFilter = "ID = 123"
If dv.Count = 1 Then
'which datarow in the original datatable is this?
End If
EDIT
I want to avoid having to loop through the DataTable to find this:
For r As Integer = 0 To dt.Rows.Count - 1
If CInt(dt.Rows(r).Item("ID")) = 123 Then
Debug.WriteLine("Found it at row " + r.ToString)
Exit For
End If
Next
Nothing could be easier:
Dim dr As DataRow = dv(0).Row
http://msdn.microsoft.com/en-us/library/system.data.datarowview.row%28v=VS.100%29.aspx
Edit: According to your comment, you could use following to get the index of the row
Dim rowIndex As Int32 = -1
For i As Int32 = 0 To dr.Table.Rows.Count - 1
If dr.Table.Rows(i)("ID").Equals(dr("ID")) Then
rowIndex = i
Exit For
End If
Next
Or in this shorter way:
Dim rowIndex As Int32 = dr.Table.Rows.IndexOf(dr)
http://msdn.microsoft.com/en-us/library/system.data.datarowcollection.indexof.aspx