Save a Round Trip to the Database

Save a Round Trip to the Database

Save a round trip to the database when a keyed item is selected from any list type. This example uses a combobox, but any type of list control that accomodates databinding can work.

Public Sub LoadComboBox()    'A custom object - use your own db access method to get a table    Dim sb As New SqlBuilder    With sb         .ConnectString = WinAppConfig.ConnectString         .ObjectName = "Publishers"         'Get ALL the columns fields from the database table         .SQL = "SELECT * FROM publishers "    End With    'Load the table to the control's datasource    ComboBox1.DataSource = sb.GetDataSet.Tables("Publishers")    'Set the display and value properties    ComboBox1.DisplayMember = "pub_name"    ComboBox1.ValueMember = "pub_id"    'Set the databinding with the table and key value    ComboBox1.DataBindings.Add("SelectedValue", sb.GetDataSet.Tables(0),"pub_id")    sb = NothingEnd Sub'At this point, you usually would use the SelectedValue property as the keyto do the'next lookup and pop the database for the corresponding row data, but withthis method,'you don't need to because you already have it - you just need to get it outPrivate Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal eAs System.EventArgs) _Handles ComboBox1.SelectedIndexChanged    'Protect the code from the load event firings    If ComboBox1.DataBindings.Count > 0 Then        Dim bmb As BindingManagerBase = Me.BindingContext(ComboBox1)        Dim en As IEnumerator        Dim dt As DataTable        Dim dr As DataRow        Dim str As String        en = ComboBox1.DataBindings.GetEnumerator()        en.MoveNext()        dt = CType(ComboBox1.DataBindings.Item(0).DataSource, DataTable)        dr = dt.Rows(ComboBox1.SelectedIndex)        str = "You Selected: " & vbCr        str &= "pub_id=" & dr.Item("pub_id") & vbCr        str &= "pub_name=" & dr.Item("pub_name") & vbCr        str &= "city=" & dr.Item("city") & vbCr        str &= "state=" & dr.Item("state") & vbCr        str &= "country=" & dr.Item("country") & vbCr        MessageBox.Show(str)    End IfEnd Sub
Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of