0% found this document useful (0 votes)
46 views2 pages

Easy Programming!

This code is populating dropdown lists and datagrid from a SQL database. It defines methods to bind categories from the database to a combobox. When the combobox selection changes, it gets the selected category ID and uses it to filter products in another combobox and datagrid.

Uploaded by

Xandra Lee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views2 pages

Easy Programming!

This code is populating dropdown lists and datagrid from a SQL database. It defines methods to bind categories from the database to a combobox. When the combobox selection changes, it gets the selected category ID and uses it to filter products in another combobox and datagrid.

Uploaded by

Xandra Lee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Imports System.Data.

SqlClient

Public Class exrcise3
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()
ComboBox1. _
Items.Add(String. _
Format("{0} | {1} ", reader.GetInt32(0), _
reader.GetString(1)))
End While
End Using
End Using
End Using
Catch ex As Exception
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
BindCategories()
End Sub
Public Sub BindDataGrid(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()

Dim dt As New DataTable

dt.Columns.Add("ProductID", GetType(System.Int32))
dt.Columns.Add("ProductName", GetType(System.String))

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)
dt.Rows.Add(row)
End While
DataGridView1.DataSource = dt
End Using
End Using
End Using

Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Public Sub BindComProducts(ByVal CategoryID As Integer)
Try
Dim sqlString As String = "SELECT ProductID, ProductName FROM dbo.Products
WHERE CategoryID = " & CategoryID
Using cnn As New SqlConnection(My.Settings.Connstr)
Using cmd As New SqlCommand(sqlString, cnn)
cnn.Open()
ComboBox2.Items.Clear()
Using reader As SqlDataReader = cmd.ExecuteReader()
While reader.Read()
ComboBox2.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, ByVal e As
System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Try
Dim Value() As String = ComboBox1.SelectedItem.ToString.Split(CChar("|"))
MsgBox(Value(0))
BindComProducts(CInt(Value(0)))
BindDataGrid(CInt(Value(0)))
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub exrcise3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles MyBase.Load

End Sub
End Class



'cmd.executenonQuery()
For saving

You might also like