Results 1 to 8 of 8

Thread: Binding combobox

Threaded View

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2009
    Location
    Oklahoma, USA
    Posts
    92

    Binding combobox

    I am new to VB.net so I am trying to learn how to create the same project with several methods. My first attempt is trying to use code instead of wizards. I seem to be having a problem creating a combobox that will fill from a table called Personnel and bind to a field in a table called Shift_log.Operator. Shift_log.Operator contains the initials of the operator. I want to display all the operators names allowing the user to select one and then store that value in the shift_Log.Operator field.
    Imports System.Data.SqlClient
    Imports System.Data

    Code:
    Class frmShift_Log
    
        Private Conn As New SqlConnection("Data Source=O2WBURKE\SQLEXPRESS;Initial Catalog=LaserMiaintLog;Integrated Security=True")
    
        Private da As New SqlDataAdapter("SELECT * FROM Shift_Log", Conn)
        Private ds As New DataSet
    
        Private SQLBld As New SqlCommandBuilder(da)
        Dim MaxRows As Integer
        Dim inc As Integer = 0
    
        Private Sub frmShift_Log_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            Conn.Open()
    
            da.Fill(ds, "ShiftLog")
    
            MaxRows = ds.Tables("ShiftLog").Rows.Count
            inc = 0
            NavigateRecords()
    
            Dim daOper As New SqlDataAdapter("SELECT Name, Initials FROM Personnel", Conn)
            Dim dsOper As New DataSet
            daOper.Fill(dsOper, "Personnel")
    
            Conn.Close()
    
            txtOperator.DisplayMember = "Name"
            txtOperator.ValueMember = "Initials"
            txtOperator.DataSource = dsOper
    
    
        End Sub
    
        Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
    
            If inc <> MaxRows - 1 Then
                inc = inc + 1
                NavigateRecords()
            Else
                MsgBox("No More Rows")
            End If
    
        End Sub
    
        Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
            If inc > 0 Then
                inc = inc - 1
                NavigateRecords()
            Else
                MsgBox("No More Rows")
            End If
    
        End Sub
    
        Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
    
            inc = 0
            NavigateRecords()
    
        End Sub
    
        Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
    
            inc = MaxRows - 1
            NavigateRecords()
    
        End Sub
    
        Private Sub btnUpdate_Click(ByVal sender As System.Object, _
                    ByVal e As System.EventArgs) Handles btnUpdate.Click
    
            Dim strmsg As String
            Try
    
                ds.Tables("ShiftLog").Rows(inc).Item("Shift_Key") = txtShift_Key.Text
                ds.Tables("ShiftLog").Rows(inc).Item("Operator") = txtOperator.Text
                ds.Tables("ShiftLog").Rows(inc).Item("Log_Date") = txtLogDate.Text
                ds.Tables("ShiftLog").Rows(inc).Item("Shift_Hours") = txtShiftHours.Text
                ds.Tables("ShiftLog").Rows(inc).Item("Operator_Comments") = txtOperComments.Text
    
                da.Update(ds, "ShiftLog")
                MsgBox("Data updated")
            Catch ex As Exception
                strmsg = "Error - Update " & ex.Message
                MsgBox(strmsg)
            End Try
    
        End Sub
    
        Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
    
            btnCommit.Enabled = True
            btnAdd.Enabled = False
            btnUpdate.Enabled = False
            btnDelete.Enabled = False
    
            txtShift_Key.Clear()
            txtShift_Key.Clear()
    
        End Sub
    
        Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
    
            btnCommit.Enabled = False
            btnAdd.Enabled = True
            btnUpdate.Enabled = True
            btnDelete.Enabled = True
    
            inc = 0
            NavigateRecords()
    
        End Sub
    
        Private Sub btnCommit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCommit.Click
    
            If inc <> -1 Then
                Dim dsNewRow As DataRow
                dsNewRow = ds.Tables("ShiftLog").NewRow()
                dsNewRow.Item("Shift_Key") = txtShift_Key.Text
                dsNewRow.Item("Operator") = txtOperator.Text
                dsNewRow.Item("Log_Date") = txtLogDate.Text
                dsNewRow.Item("Shift_Hours") = txtShiftHours.Text
                dsNewRow.Item("Operator_Comments") = txtOperComments.Text
    
                ds.Tables("ShiftLog").Rows.Add(dsNewRow)
                da.Update(ds, "ShiftLog")
    
                MsgBox("New Record added to the Database")
    
                btnCommit.Enabled = False
                btnAdd.Enabled = True
                btnUpdate.Enabled = True
                btnDelete.Enabled = True
            End If
    
        End Sub
    
        Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
    
            If MessageBox.Show("Do you really want to Delete this Record?", _
                "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = DialogResult.No Then
    
                MsgBox("Operation Cancelled")
                Exit Sub
    
            End If
    
            ds.Tables("ShiftLog").Rows(inc).Delete()
    
            MaxRows = MaxRows - 1
            inc = 0
            NavigateRecords()
            da.Update(ds, "ShiftLog")
            MsgBox("Record deleted from the Database")
    
        End Sub
    
        Private Sub NavigateRecords()
    
            txtShift_Key.Text = ds.Tables("ShiftLog").Rows(inc).Item("Shift_Key")
            txtOperator.Text = ds.Tables("ShiftLog").Rows(inc).Item("Operator")
            txtLogDate.Text = ds.Tables("ShiftLog").Rows(inc).Item("Log_Date")
            txtShiftHours.Text = ds.Tables("ShiftLog").Rows(inc).Item("Shift_Hours")
            If Not IsDBNull(ds.Tables("ShiftLog").Rows(inc).Item("Operator_Comments")) Then
                txtOperComments.Text = ds.Tables("ShiftLog").Rows(inc).Item("Operator_Comments")
            Else
                txtOperComments.Text = " "
            End If
    
        End Sub
    
    End Class
    Last edited by wjburke2; Aug 21st, 2012 at 10:36 AM. Reason: Clarifcation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width