Question Combobox if statement

Rishad Hasan

Member
Joined
Feb 1, 2013
Messages
8
Programming Experience
Beginner
Hello All,

I am kind a new in vb.net. I have a combobox where I have two choices and for those two choices I have data stored in two different fields in the same access database table. I was wondering what code should I write to pick the right part from database table according to user's pick from combobox. I am here attaching what I got from me. Would you mind suggest me where should I correct in this code.

Thanks.


Private Sub findPartNumberforItem15()
 

Dim Table_ As String
 
Table_ =
"Item_15"
 

Dim query As String = "SELECT * FROM " & Table_

Dim MDBConnString_ As String = dataBaseLocation

Dim ds As New DataSet

Dim cnn As OleDb.OleDbConnection = New OleDb.OleDbConnection(MDBConnString_)
cnn.Open()

Dim cmd As New OleDb.OleDbCommand(query, cnn)

Dim da As New OleDb.OleDbDataAdapter(cmd)
da.Fill(ds, Table_)
cnn.Close()

Dim t1 As DataTable = ds.Tables(Table_)

'Dim row As DataRow

Dim i As Integer

For i = 0 To 3
MsgBox(i)
MsgBox(
"*" + t1.Rows(i).Item("Size") + "*")
MsgBox(
"*" + ComboBox4.Text + "*")

If ComboBoxInletPressure.SelectedIndex = 0 Then
Table_ =
"Item_15"

ElseIf t1.Rows(i).Item("Size") = ComboBox4.Text Then

If Not IsDBNull(t1.Rows(i).Item("0_5PSI")) Then
partNumber = t1.Rows(i).Item(
"0_5PSI")
 

ElseIf Not IsDBNull(t1.Rows(i).Item("6_25PSI")) Then
partNumber = t1.Rows(i).Item(
"6_25PSI")

End If

Exit For

End If
 
 

Next i
 
 

End Sub
 
Hi,

From what I can tell you want to use the value of ComboBox4, being a Size and then get an associated field, being either 0_5PSI or 6_25PSI, from the same DataRow of a DataTable in your DataSet.

On this basis you do not need to iterate through your DataTable every time you need to find something. The way to do this would be to associate 0_5PSI and 6_25PSI with the relevant Size information in your ComboBox.

If I were doing this then I would create a custom class object to hold all the information I needed and then add the class to the ComboBox so that when the ComboBox changes you can easily get the relevant PSI information of the selected Size in the ComboBox.

Have a look at this example. Ignore the fact that I have used an Employee table, you would just adapt this to use your own data source.

VB.NET:
Imports System.Data.SqlClient
 
Public Class Form1
  'Here we create a new custom class that will hold each record from the
  'table in the DataSet
  Private Class myDataClass
    'Define the classes properties
    Public Property strSize As String
    Public Property str0_5PSI As String
    Public Property str6_25PSI As String
 
    'This is the constructor to convert the contents of the
    'DataRow into the appropriate class properties
    Public Sub New(ByVal myDataRow As DataRow)
      strSize = IIf(Not myDataRow.IsNull(1), myDataRow(1).ToString, String.Empty).ToString
      str0_5PSI = IIf(Not myDataRow.IsNull(0), myDataRow(0).ToString, String.Empty).ToString
      str6_25PSI = IIf(Not myDataRow.IsNull(2), myDataRow(2).ToString, String.Empty).ToString
    End Sub
 
    'Here we override what is displayed by the ToString method
    Public Overrides Function ToString() As String
      Return strSize
    End Function
  End Class
 
  Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    'Here we define the database objects and read our Data into a DataSet
    Using sqlConn As New SqlConnection("Data Source=IANVAIO\SQLEXPRESS;Initial Catalog=NORTHWIND;Integrated Security=True")
      Using daEmployees As New SqlDataAdapter("Select * From Employees", sqlConn)
        Using DS As New DataSet
          daEmployees.Fill(DS, "Employees")
          'Here we convert the rows of the table in the DataSet into an
          'array of our custom myDataClass and add them to a ComboBox
          ComboBox1.Items.AddRange(Array.ConvertAll(DS.Tables(0).Rows.Cast(Of DataRow).ToArray, Function(x As DataRow) New myDataClass(x)))
        End Using
      End Using
    End Using
  End Sub
 
  Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    'When the SelectedIndexChanged event of the ComboBox fires we
    'convert the SelectedItem into our custom myDataClass and
    'then interrogate the data field properties to identify which
    'property we want to work with
    Dim SelectedDataClass As myDataClass = DirectCast(ComboBox1.SelectedItem, myDataClass)
    Dim MyNeededValue As String = String.Empty
 
    If Not SelectedDataClass.str0_5PSI = String.Empty Then
      MyNeededValue = SelectedDataClass.str0_5PSI
    ElseIf Not SelectedDataClass.str6_25PSI = String.Empty Then
      MyNeededValue = SelectedDataClass.str6_25PSI
    End If
 
    'Do whatever you want with the results of your conditional statement
    MsgBox(MyNeededValue)
  End Sub
End Class

Hope that helps.

Cheers,

Ian
 
Back
Top