Textbox lookup through ComboBox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • CodeJoker
    New Member
    • Jan 2010
    • 2

    Textbox lookup through ComboBox

    Hey I am trying to create a textbox that relays text to my combobox on my for so I may lookup details in my DB but I get two errors . . .First there is a Primary Key error, "Table does not have a primary key" and if I do it the other way around by asking my textbox alone to lookup my values that I put in I seem to receive the same values regardless of what I put in my texbox. . . Please review these different codes and see if anyone can spot my mistake. . . Replies would be greatly appreciated.

    TEXTBOX LOOKUP CODE:
    Code:
    Private m_cnADONetConnection As New OleDb.OleDbConnection()
    Private m_daDataAdapter As OleDb.OleDbDataAdapter
    Private m_cbCommandBuilder As OleDb.OleDbCommandBuilder
    Private m_dtCD As New DataTable
    Dim m_rowPosition As Integer = 0
    
    Private Sub StockDetailsForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
       m_cnADONetConnection.ConnectionString = _
    "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\TEST.mdb"
    
       m_cnADONetConnection.Open()
    
       m_daDataAdapter = New OleDb.OleDbDataAdapter("Select * From CD", m_cnADONetConnection)
       m_cbCommandBuilder = New OleDb.OleDbCommandBuilder(m_daDataAdapter)
       m_daDataAdapter.Fill(m_dtCD)
    
    
    Public Sub StockCheck()
    
    StockEdit_Open_Conn()
    Dim sql = "SELECT CouplingCode ,AlbumTitle ,ReleaseDate ,TotalSongs ,PPD FROM CD WHERE CouplingCode='" & StockDetailsForm.txtSearch.Text & "'"
    Dim cmd As New System.Data.OleDb.OleDbCommand
    
    cmd = New OleDbCommand(sql, m_cnADONetConnection)
    Dim exReader As OleDbDataReader = cmd.ExecuteReader
    m_cbCommandBuilder = New OleDb.OleDbCommandBuilder(m_daDataAdapter)
    
    
    Private Sub txtSearch_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.Leave
    
    Try
    If exReader.Read = False Then
    MessageBox.Show("Stock Item does not exist", "Stock Item Error", MessageBoxButtons.OK, MessageBoxIcon.Stop)
    m_cnADONetConnection.Close()
    m_cnADONetConnection.Dispose()
    Else
    StockDetailsForm.CouplingCodeID.Text = _
    m_dtCD.Rows(m_rowPosition)("CouplingCode").ToString()
    StockDetailsForm.txtAlbumName.Text = _
    m_dtCD.Rows(m_rowPosition)("AlbumTitle").ToString()
    StockDetailsForm.txtPPD.Text = _
    m_dtCD.Rows(m_rowPosition)("PPD").ToString()
    StockDetailsForm.txtTotalSongs.Text = _
    m_dtCD.Rows(m_rowPosition)("TotalSongs").ToString()
    StockDetailsForm.dtpReleaseDate.Text = _
    m_dtCD.Rows(m_rowPosition)("ReleaseDate").ToString()
    StockDetailsForm.txtQtyHand.Text = _
    m_dtCD.Rows(m_rowPosition)("QtyOnHand").ToString()
    End If
    End Sub
    TEXTBOX TO COMBOBOX LOOKUP CODE :
    Code:
        Private m_cnADONetConnection As New OleDb.OleDbConnection
        Private m_daDataAdapter As OleDb.OleDbDataAdapter
        Private m_cbCommandBuilder As OleDb.OleDbCommandBuilder
        Private m_dtCD As New DataTable
        Dim m_rowPosition As Integer = 0
    
        Private Sub NewEditCDForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            m_cnADONetConnection.ConnectionString = _
            "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\TEST.mdb"
    
            m_daDataAdapter = New OleDb.OleDbDataAdapter("Select * From CD", m_cnADONetConnection)
            m_cbCommandBuilder = New OleDb.OleDbCommandBuilder(m_daDataAdapter)
            m_daDataAdapter.Fill(m_dtCD)
    
            m_cnADONetConnection.Open()
        End Sub
    
        Private Sub txtSearch_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.Leave
            Try
                CDLookup.Text = txtSearch.Text
                CDLookup.Text = _
                m_dtCD.Rows.Find("CouplingCode").ToString()
    
                Dim sql = "SELECT * FROM CD WHERE CouplingCode='" & txtSearch.Text & "'"
                Dim cmd As New System.Data.OleDb.OleDbCommand
                cmd = New OleDbCommand(sql, m_cnADONetConnection)
                Dim exReader As OleDbDataReader = cmd.ExecuteReader
    
    
                If exReader.Read = False Then
    
                    MessageBox.Show("Invalid Username and/or Password. . . ", "Authentication Has Faild", MessageBoxButtons.OK, MessageBoxIcon.Stop)
    
                Else
                    CouplingCodeID.Text = _
                    m_dtCD.Rows.Find(m_rowPosition)("CouplingCode").ToString()
                    Exit Sub
                End If
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub
    Last edited by Frinavale; Jan 18 '10, 06:30 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.
  • mzmishra
    Recognized Expert Contributor
    • Aug 2007
    • 390

    #2
    Did you try to debug and see what value you are getting as textbox text.I think it is somehow set to some default or initial value

    Comment

    Working...