Author Topic: GET ID OF ITEM FROM COMBOBOX SELECTED ITEM  (Read 781 times)

stenjun

  • Newbie
  • *
  • Posts: 6
  • Karma: +0/-0
    • View Profile
GET ID OF ITEM FROM COMBOBOX SELECTED ITEM
« on: May 07, 2016, 09:05:53 AM »
Dear Sir,

How can i get an item id from selected item in the database e.g I have a Customer Table with CustomerID as primary key and Customer name. I fill a combobox with customer names from database. How can i get CustomerID  automatically when i select a customer on the combobox during billing.
When i try to access it using the Library Management style of using reference (in GetBookidfrombarcode function), it makes it a must for me to supply a customer name and an error is generated when i dont suppky a customer name.
Please assist.

stenjun

  • Newbie
  • *
  • Posts: 6
  • Karma: +0/-0
    • View Profile
Re: GET ID OF ITEM FROM COMBOBOX SELECTED ITEM
« Reply #1 on: June 15, 2016, 10:06:06 AM »
Solution

Add items combobox on form load.
Create a function to get ID from the Supplied Name
Call this function and supply required values and with this values, it gets the ID.
Sample Program

'FUNCTION TO GET THE ID FROM DATABASE
Public Shared Function GetCustIDFromName(ByVal DBMS As DBMSClass, ByVal CustLName As String, ByRef Cust_ID As String, _
                                             ByRef Cust_Due As Decimal) As Boolean
        Try
            Dim s As String = DBMS.CreateResultSet("SELECT * FROM customer where custlname =@0", CustLName)
            If DBMS.ReadAndNotEOF(s) Then
                Cust_ID = DBMS.GetColumnValue(s, "custid")
                Cust_Due = DBMS.GetColumnValue(s, "custdue")
                DBMS.CloseResultSet(s)
                Return True
            End If

            DBMS.CloseResultSet(s)
            DBMS.Rollback()

            Return False

        Catch ex As Exception
            DBMS.Rollback()
            Return False
        End Try
    End Function

'USE THE FUNCTION WHERE YOU NEED THE ID SUPPLIED
 Try
          Dim CustName as string = ""
            ElseIf Not CustomerClass.GetCustIDFromName(MainWindow.DBMS, CustName, CustId, CustDue) Then
                MainWindow.DBMS.Rollback()
            End If
            Catch ex As Exception
            MainWindow.DBMS.Rollback()
            MsgBox(ex.Message)
        End Try

Hope this helps someone.