Programming for Everybody: May 2024

Code VB.Net SQL query combine two columns that contain different data type and display in ComboBox

 



Imports System.Data.SqlClient

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Dim conn As New SqlConnection("Data Source=.;Initial Catalog=products;integrated Security=true")

        'conn.Open()

        Dim cmd As New SqlCommand("Select id, '(' +  Cast(id As nvarchar(50))+ ')' + item_product  As products from names_products", conn)

        Dim da As New SqlDataAdapter(cmd)

        Dim dt As New DataTable

        da.Fill(dt)

        With ComboBox1

            .DataSource = dt

            .DisplayMember = "products"

            .ValueMember = "id"

            .Text = "Select Product"

        End With

    End Sub

End Class