Sie sind auf Seite 1von 3

Imports System.Data.SqlClient Public Class exercise3 Private Sub exercise3_Load(ByVal sender As System.Object, ByVal e As System. EventArgs) Handles MyBase.

Load 'LOAD EVENT End Sub Private Sub BindCategories() Dim sqlString As String = _ "SELECT CategoryID, CategoryName FROM Categories;" Try Using cnn As New SqlConnection( _ My.Settings.ConnStr) Using cmd As New SqlCommand(sqlString, cnn) cnn.Open() Using reader As SqlDataReader = cmd.ExecuteReader() While reader.Read() 'categoriesComboBox.Items.Add(reader.GetString(0)) categoriesComboBox. _ Items.Add(String. _ Format("{0} | {1} ", reader.GetInt32(0), _ reader.GetString(1))) End While End Using End Using End Using Catch exp As Exception resultsLabel.Text = exp.Message End Try End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.E ventArgs) Handles Button1.Click BindCategories() End Sub Public Sub BindComboProducts(ByVal CategoryID As Integer) Try Dim sqlString As String = "SELECT ProductID, ProductName " _ & " UnitsInStock FROM dbo.Products " _ & " WHERE CategoryID = " & CategoryID Using cnn As New SqlConnection(My.Settings.ConnStr) Using cmd As New SqlCommand(sqlString, cnn) cnn.Open() ComboBox1.Items.Clear() Using reader As SqlDataReader = cmd.ExecuteReader() While reader.Read ComboBox1.Items.Add(reader.GetString(1)) End While End Using End Using End Using

Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, By Val e As System.EventArgs) Handles ComboBox1.Click Dim value() As String = categoriesComboBox.SelectedItem.ToString.Split(C Char("|")) BindComboProducts(CInt(value(0))) End Sub Public Sub BindDataGrid(ByVal CategoryID As Integer) Try Dim sqlString As String = "SELECT ProductID, ProductName, UnitsInSto ck " _ & " FROM dbo.Products " _ & " WHERE CategoryID = " & CategoryID Using cnn As New SqlConnection(My.Settings.ConnStr) Using cmd As New SqlCommand(sqlString, cnn) cnn.Open() Dim dt As New DataTable dt.Columns.Add("ProductID", GetType(System.Int32)) dt.Columns.Add("ProductName", GetType(System.String)) dt.Columns.Add("UnitsInStock", GetType(System.Int16)) Using reader As SqlDataReader = cmd.ExecuteReader() While reader.Read Dim row As DataRow = dt.NewRow() row("ProductID") = reader.GetInt32(0) row("ProductName") = reader.GetString(1) row("UnitsInStock") = reader.GetInt16(2) dt.Rows.Add(row) End While DataGridView1.DataSource = dt 'mawawala to kapag INSERT lahat ng may READER End Using End Using End Using Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub categoriesComboBox_SelectedIndexChanged(ByVal sender As System.O bject, ByVal e As System.EventArgs) Handles categoriesComboBox.SelectedIndexChan ged Dim value() As String = categoriesComboBox.SelectedItem.ToString.Split(C Char("|")) BindComboProducts(CInt(value(0))) BindDataGrid(CInt(value(0)))

End Sub 'Private Sub ComboBox1_SelectedIndexChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged 'End Sub Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, By Val e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.C ellClick MsgBox(DataGridView1.Rows(e.RowIndex).Cells(0).Value) 'MsgBox(e.ColumnIndex & ":" & ) End Sub Private Sub DataGridView1_CellContentClick_1(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1 .CellContentClick End Sub Private Sub ComboBox1_SelectedIndexChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged End Sub End Class

Das könnte Ihnen auch gefallen