Drop Down in VB.NET - vb.net

I have a small requirement and that is:
There are two combo boxes on a form and for populating the employee names and roles. I am populating the combo boxes as follows:
I have created a class called "DbConnect" and in that there are 02 functions as:
Public Function getEmployees() As DataTable
Dim employeeDS As New DataSet
Dim employeeDA As New SqlDataAdapter("prc_emp_list", conn)
employeeDA.Fill(employeeDS, "employees")
Return employeeDS.Tables("employees")
End Function
Public Function getRoles() As DataTable
Dim roleDS As New DataSet
Dim roleDA As New SqlDataAdapter("prc_role_list", conn)
roleDA.Fill(roleDS, "roles")
Return roleDS.Tables("roles")
End Function
Have designed a form with two combo boxes and am populating data into them as:
Public Sub employees()
accessFunction.Open()
cboEmployees.DataSource = accessFunction.getEmployees
cboEmployees.DisplayMember = "emp_name"
cboEmployees.ValueMember = "login_id"
End Sub
Public Sub roles()
accessFunction.Open()
cboRoles.DataSource = accessFunction.getRoles
cboRoles.DisplayMember = "role_name"
cboRoles.ValueMember = "role_id"
End Sub
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
employees()
roles()
End Sub
The data is getting populated into the combo boxes correctly and my requirement is that when I select and employee from the first combo, his corresponding role should get selected in the second combo.
Anyone, please help me on this requirement.
Regards,
George

You need to add a binding source and a data relationship to get this to work. Consider this walk through, it is for datagridviews but the concept is the same.
I did a quick mock up to give you an idea. Remember that "EmpTable" is the name that you assign to your datatable and "EmpColumn" is the parent column, similarly apply the same logic to the Roles table. The key change to your code is that both tables must be in the same dataset with a datarelationship.
Dim dtEmp as Datatable
Dim dtRole as Datatable
''//fill tables here
Dim ds as New Dataset()
ds.Tables.add(dtRole)
ds.Tables.add(dtEmp)
Dim dr as New DataRelation( _
ds.Tables("EmpTable").Columns("EmpColumn"),
ds.Tables("RoleTable").Columns("RoleColumn"))
''//create binding sources
Dim bsEmp as New BindingSource
Dim bsRole as New BindingSource
bsEmp.Datasource = ds
bsEmp.DataMember = "EmpTable"
bsRole.Datasource = bsEmp
bsRole.DataMeber = "RoleTable"
''//bind the binding sources to the appropriate comboboxes
cboEmployee.Datasource = bsEmp
cboRole.Datasource = bsRole
Good luck.

Related

How to Setup A Combox , always will with same data, as a user control to be used on multiple forms

I have a ComboBox that I use on multiple WinForms. Instead of dropping a ComboBox on each WinForm and then filling the ComboBox with data from a DataTable on each individual WinForm, couldn't I create a User Control (ComboBox) that has the data populated already and just use that UC on my Winforms?
Below is how I fill the data for each individual combobox now. (I have a public class for the sql stuff)
The Variable SQL comes from a Class called SQLControl. the Class has all the sql connection stuff.
Public Sub Fillcombobox()
sql.AddParam("#ExaminerType", 3)
sql.ExecQuery("MyStoredProcedure")
ComboBoxExaminer.ValueMember = "Examiner_ID"
ComboBoxExaminer.DisplayMember = "Last_Name"
ComboBoxExaminer.DataSource = sql.DBDT
End Sub
Private Sub MyWinform_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Call Fillcombobox()
End Sub
You can put a small Class Examiner
Public Class Examiner
Public Property Examiner_ID As Integer
Public Property Last_Name As String
Public Sub New(ID As Integer, lname As String)
Examiner_ID = ID
Last_Name = lname
End Sub
End Class
Then, when the first form loads, get the data in a list declared in a module so it can be accessed from any form in the application. Of course, you may have other things in the Module.
Module Module1
Public ExaminerData As New List(Of Examiner)
End Module
Private Sub MyWinform_Load(sender As Object, e As EventArgs) Handles MyBase.Load
FillExaminerList()
ComboBoxExaminer.ValueMember = "Examiner_ID"
ComboBoxExaminer.DisplayMember = "Last_Name"
ComboBoxExaminer.DataSource = ExaminerData
End Sub
Any other form that needs the data to fill a combo box can use ExaminerData. You only call FillExaminerList once at the beginning of the application. There is only a single hit on the database.
Private OPConStr As String = "Your connection string."
Private Sub FillExaminerList()
Dim dt As New DataTable
Using cn As New SqlConnection(OPConStr),
cmd As New SqlCommand("MyStoredProcedure", cn)
cmd.Parameters.Add("#ExaminerType", SqlDbType.Int).Value = 3
Using reader = cmd.ExecuteReader
dt.Load(reader)
End Using
End Using
For Each row As DataRow In dt.Rows
Dim ex As New Examiner(CInt(row("Examiner_ID")), row("Last_Name").ToString)
ExaminerData.Add(ex)
Next
End Sub

How can I control the colour of multiple buttons in a form depending on a value?

I am creating a hotel booking system for a hotel with 31 rooms, so I have 31 buttons called "Button1", "Button2", etc.
I need to change the back colour of the buttons depending on availability of a room (red/green).
Each room has an "IsVacant" variable in the database that is either true or false and that value will be imported as an array into the form. I just need a way to link the colour of all buttons depending on the corresponding values of the imported array.
Screenshot of Form
Make a class to hold you room data.
Public Class Room
Public Property RoomNubmer As Integer
Public Property IsVacant As Boolean
Public Property Details As String
Public Sub New(RN As Integer, Vac As Boolean, Det As String)
RoomNubmer = RN
IsVacant = Vac
Details = Det
End Sub
Public Overrides Function ToString() As String
Return RoomNubmer.ToString
End Function
End Class
In the form create a form level variable List(Of T) so the room data will be visible from anywhere in the form.
To fill the list we first connect to the database and retrieve the data. Next, loop through the DataTable rows creating a new room and adding the room to the list.
Private ConStr As String = "Your connection string"
Private RoomList As New List(Of Room)
Private Function GetRoomData() As DataTable
Dim dt As New DataTable
Using cn As New SqlConnection(ConStr),
cmd As New SqlCommand("Select * From Rooms", cn)
cn.Open()
Using reader = cmd.ExecuteReader
dt.Load(reader)
End Using
End Using
Return dt
End Function
Private Sub FillRoomList()
Dim dt = GetRoomData()
For Each row As DataRow In dt.Rows
RoomList.Add(New Room(CInt(row(0)), CBool(row(1)), row(2).ToString))
Next
End Sub
In the Form.Load we fill the room list. Then make a list of Buttons. Assuming the length of the two lists is the same:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
FillRoomList()
Dim ButtonList As New List(Of Button) From {Button1, Button2, Button3, Button4} 'etc
For i = 0 To ButtonList.Count - 1
ButtonList(i).Text = RoomList(i).ToString
If RoomList(i).IsVacant Then
ButtonList(i).BackColor = Color.Green
Else
ButtonList(i).BackColor = Color.Red
End If
Next
End Sub
As you can see, List(Of T) is just handy as a pants pocket for collections of things. Learn more about them in List<T> Class.
loop for each button
query IsVacant from the database
change the back color of the button according to the query result

Unable to load data on Select method of Datatable

Trying to populate a set of data from datatable to Combobox on the basis of text entered in Combobox. But getting a System.Data.DataRow error in the Select method of datatable.
Following is the code which binds the datatable on Form Load and rebinds data on the call of Search method.
Note that the search has to be on Tab press not AutoComplete
Imports System.Data.SqlClient
Public Class Form1
Dim connection As SqlConnection = New SqlConnection()
Dim table As New DataTable
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
LoadComboBox()
End Sub
Private Sub LoadComboBox()
Dim adp As SqlDataAdapter = New SqlDataAdapter("select stage from sample", connection)
adp.Fill(table)
ComboBox1.DataSource = New BindingSource(table, Nothing)
ComboBox1.DisplayMember = "stage"
End Sub
Private Sub Search()
Dim filteredTable As New DataTable
Dim filterRow As DataRow()
Dim str As String = ComboBox1.Text.Trim
filterRow = table.Select("stage like '%" & ComboBox1.Text.ToString & "%'")
'**Error in above table(datatable)**
For Each rw As DataRow In filterRow
filteredTable.ImportRow(rw)
Next
ComboBox1.DataSource = New BindingSource(filteredTable, Nothing)
ComboBox1.DisplayMember = "stage"
End Sub
Private Sub ComboBox1_PreviewKeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles ComboBox1.PreviewKeyDown
If e.KeyCode = Keys.Tab Then
Search()
End If
End Sub
End Class
in your binding to combo box you have a data table already why are you using a binding source
why not just say ComboBox1.DataSource = filteredTable
Also i would advice you to use Key Value Pair rather than using Like easier binding
Like is regex search at SQL Server this is killing your Database and you are creating overhead over nothing
Key | Value >>
ID | Name
so taking this from another stack over flow for fast reference
gridview bind dropdownlist to List<keyvaluePair<int, string>>
you can bind a dictionary , datatable , dataview does not matter.
if you want to get text just say your dropdown.text for the ID value dropdown.value if you want to do something later to in the database.
You can make your drop down read only and when user types basically what he is typing is filtering according to what you have binded that way your text search can be made
var dictionary = new Dictionary<int, string>();
dictionary.Add(1, "Home");
dictionary.Add(2, "Work");
dictionary.Add(3, "Mobile");
dictionary.Add(4, "Fax");
dropDown.DataTextField = "Value";
dropDown.DataValueField = "Key";
dropDown.DataSource = dictionary; //Dictionary<int, string>
dropDown.DataBind();
Tried it with a bit of Linq.
Private Sub Search()
Dim filteredTable As New DataTable
Dim str As String = ComboBox1.Text.Trim
Dim filterRows = (From row As DataRow In Table.AsEnumerable
Where row.Field(Of String)("Name").Contains(ComboBox1.Text)
Select row.Field(Of String)("Name")).ToList
ComboBox1.DataSource = filterRows
End Sub
Just substitute your column name for "Name"

How to retrieve values from multiple columns in sql tables when Image click on ListView of vb.net form?

your valuable suggestions are highly appreciated.
I created sql table as follows.
Create table Products (
ItemCode int NOT NUll Primary Key,
ItemName varchar(255),
ItemImage varbinary(MAX),
Category int,
Price Money,
Note varchar(255))
Then I created a vb.net windows form which contains some textboxes and Listview. and gave following code at form load event.
Imports System.Data.Sql
Imports System.Data.SqlClient
Imports System.Configuration.ConfigurationManager
Imports System.IO
Public Class items
Dim sqlCon As New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("myPosConString").ConnectionString)
Dim Command As SqlCommand
Dim Reader As SqlDataReader
Dim i As Integer
Dim Query As String
'Fill Items with pictures into listview
Private Sub FillItemListView()
Dim Adapter As New SqlDataAdapter
Dim dt_Images As New DataTable
Try
lvItem.Clear()
Dim imglist As New ImageList
imglist.ColorDepth = ColorDepth.Depth32Bit
lvItem.LargeImageList = imglist
lvItem.LargeImageList.ImageSize = New System.Drawing.Size(100, 100)
sqlCon.Open()
Query = "select * from products "
Command = New SqlCommand(Query, sqlCon)
Adapter.SelectCommand = Command
Adapter.Fill(dt_Images)
For Each dr As DataRow In dt_Images.Rows
Dim img_buffer = CType(dr("ItemImage"), Byte())
Dim img_stream As New MemoryStream(img_buffer, True)
img_stream.Write(img_buffer, 0, img_buffer.Length)
imglist.Images.Add(dr("ItemCode").ToString(), New Bitmap(img_stream))
img_stream.Close()
Dim lstv As New ListViewItem
lstv.Text = dr("ItemName").ToString
lstv.ImageKey = dr("ItemCode").ToString
lvItem.Items.Add(lstv)
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub items_Load(sender As Object, e As EventArgs) Handles MyBase.Load
FillItemListView()
End Sub
End Class
Everything is working as I wanted. But I want all my textboxes to get values from other columns of sql table when I click on a large image in Listview.
listview name is lvItem and
txtboxes are txtCode, txtName, txtCategory, txtPrice, txtNote.
Can you please teach me how to do this.
thank you.
If you need to use a View other than Details in the ListView then a ListView it must be, as the DataGridView doesn't support the other views.
I would suggest that you bind your DataTable to a BindingSource and then bind that to your TextBoxes. When the user selects an item in the ListView, you can assign the Index of that item to the Position of the BindingSource. That will then display the fields of the row at that index in the bound TextBoxes.
If the user can sort your ListView then you could also set the Sort property of the BindingSource to keep them in sync.

Why I am getting System.Data.DataRowViewā€¯ instead of real values from my Listbox in vb.net

Could someone help here?
I need to extract data from a Database into a combolistbox in VB.net. I have got the data, but now find that The first and the 'x' line need to be removed from the combolistbox (they are validation entries for another software) and shouldn't be selected for this application.
I tried to simply remove the offending entries from lists by using :- cbCubeARivet.Items.RemoveAt(index), but had an error letting me know I cannot use "Items" with a DataSource.
I decided to send the data to a listbox, and then try to transfer the entries to the combolistbox. This then lead me to getting multiple entries of System.Data.DataRowView in the combolist box. To demonstrate my problem I include an example code modified from MSDN.
Imports System
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Collections
Imports System.Data.SqlClient
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' Populate the list box using an array as DataSource.
Dim SQLConnectionString As String = "Data Source=HL605\RIVWARE;Database=RIVWARE;Integrated Security=true;"
Dim mySQLConnection As New SqlConnection(SQLConnectionString)
mySQLConnection.Open()
Dim SQLDataTable As New System.Data.DataTable
'Create new DataAdapter
'Use DataAdapter to fill DataTable
Dim mySQLDataAdapter = New SqlDataAdapter("SELECT * FROM [Rivware].[dbo].[RivetTypes]", mySQLConnection)
mySQLDataAdapter.Fill(SQLDataTable)
ListBox1.DataSource = SQLDataTable
ListBox1.DisplayMember = "RivetType"
'original code from MSDN
'Dim USStates As New ArrayList()
'USStates.Add(New USState("Alabama", "AL"))
'USStates.Add(New USState("Washington", "WA"))
'USStates.Add(New USState("West Virginia", "WV"))
'USStates.Add(New USState("Wisconsin", "WI"))
'USStates.Add(New USState("Wyoming", "WY"))
'ListBox1.DataSource = USStates
' Set the long name as the property to be displayed and the short
' name as the value to be returned when a row is selected. Here
' these are properties; if we were binding to a database table or
' query these could be column names.
' Bind the SelectedValueChanged event to our handler for it.
AddHandler ListBox1.SelectedValueChanged, AddressOf ListBox1_SelectedValueChanged
' Ensure the form opens with no rows selected.
ListBox1.ClearSelected()
End Sub 'NewNew
Private Sub ListBox1_SelectedValueChanged(ByVal sender As Object, ByVal e As EventArgs)
If ListBox1.SelectedIndex <> -1 Then
TextBox1.Text = ListBox1.SelectedValue.ToString()
' If we also wanted to get the displayed text we could use
' the SelectedItem item property:
' Dim s = CType(ListBox1.SelectedItem, USState).LongName
End If
End Sub
End Class 'ListBoxSample3
Public Class USState
Private myShortName As String
Private myLongName As String
Public Sub New(ByVal strLongName As String, ByVal strShortName As String)
Me.myShortName = strShortName
Me.myLongName = strLongName
End Sub 'NewNew
Public ReadOnly Property ShortName() As String
Get
Return myShortName
End Get
End Property
Public ReadOnly Property LongName() As String
Get
Return myLongName
End Get
End Property
End Class 'USState
I may not get this correct so here goes, the following uses mocked up data to display a string in both a ListBox and ComboBox where a integer is available by casting the current item as a DataRowView, access Row then access the data via Row.Field(Of Integer)("ID").
In the code I use a copy of the underlying DataTable for the ComboBox as using the same data table for listbox and combobox will cause one to traverse when is generally unwanted. The cast aspect would be done in another event but wanted to stay simple. Again I may not be on track here, let me know and can adjust to better suit your question.
Code using Option Infer On for the Linq anonymous statement which could be strongly typed too.
Dim dt As New DataTable
dt.Columns.Add(New DataColumn With {.ColumnName = "ID", .DataType = GetType(Integer)})
dt.Columns.Add(New DataColumn With {.ColumnName = "Name", .DataType = GetType(String)})
Dim data =
(
From M In System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.MonthNames
Where Not String.IsNullOrEmpty(M)).ToList.Select(
Function(monthName, index) New With
{
.ID = index, .Name = monthName
}
).ToList
For Each item In data
dt.Rows.Add(New Object() {item.ID, item.Name})
Next
ListBox1.DataSource = dt
ListBox1.DisplayMember = "Name"
ComboBox1.DataSource = dt.Copy
ComboBox1.DisplayMember = "Name"
Dim theTable As DataTable = CType(ComboBox1.DataSource, DataTable)
Dim theRow As DataRow = theTable.AsEnumerable _
.Where(
Function(row) row.Field(Of String)("Name") = "September") _
.FirstOrDefault()
If theRow IsNot Nothing Then
theTable.Rows.Remove(theRow)
End If
Thanks again #Karen Payne,
I used your code to lead me in the right direction.
I created a new application and added a textbox, and two Listboxes, then paste the code. To run you will need to point to your own Server, Database, and Table.
This is what I came up with. It is useful as this will give you the actual data in a useable form:-
Imports System
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Collections
Imports System.Data.SqlClient
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' get the data
Dim SQLConnectionString As String = "Data Source=HL605\RIVWARE;Database=RIVWARE;Integrated Security=true;"
Dim mySQLConnection As New SqlConnection(SQLConnectionString)
' Populate the list box using an array as DataSource.
mySQLConnection.Open()
Dim SQLDataTable As New System.Data.DataTable
'Create new DataAdapter
Dim mySQLDataAdapter = New SqlDataAdapter("SELECT * FROM [Rivware].[dbo].[RivetTypes]", mySQLConnection)
mySQLDataAdapter.Fill(SQLDataTable)
ListBox1.DataSource = SQLDataTable
ListBox1.DisplayMember = "RivetType"
' remove validation data from list
Dim Count As Integer
Dim X As Integer
'get the column of data to search for
For Count = 0 To ListBox1.DataSource.Columns.Count - 1
X = InStr(ListBox1.DataSource.Columns.Item(Count).ToString, "Name")
If X <> 0 Then Exit For
Next
' now search for any invalid rows, in that column. those containing "---"
Dim TheTable As DataTable = CType(ListBox1.DataSource, DataTable)
Dim theRow As DataRow() = TheTable.Select()
Dim RowNumber As Integer
For RowNumber = 0 To UBound(theRow) - 1
If theRow(RowNumber).Item(Count).ToString <> "---" Then
' data is OK so transer it to the other listbox
ListBox2.Items.Add(theRow(RowNumber).Item(Count - 1).ToString)
End If
Next
ListBox2.ClearSelected()
End Sub 'NewNew
Private Sub ListBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox2.SelectedIndexChanged
TextBox1.Text = ListBox2.SelectedItem.ToString()
End Sub
End Class 'ListBoxSample3