none
how to SQL Command in visual basic 2010

    Question

  • hi all

    how to SQL Command and fill Result in ComboBox? in visual basic 2010

    my DataBase

    Dim SqlCon As New SqlClient.SqlConnection("Data Source=DIG-PC\DIGSER;Initial Catalog=;Integrated Security=True")
    Dim ConDatBas As New SqlClient.SqlConnection("Data Source=DIG-PC\DIGSER;Initial Catalog=DigSMSystem;Integrated Security=True")
    help me with sample code
    with regards
    Thursday, April 05, 2012 4:08 PM

Answers

  • I thought we had a sample for you, it was not like that so I made it.

    Imports System.Data.SqlClient
    Public Class Form1
        ' for earlier versions than VB10SP1 add byval
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim dt As New DataTable
            Using da As New SqlDataAdapter("Select Firstname + ' ' + LastName as FullName from Employees",
                                           "Data Source=MyServer;Initial Catalog=Northwind;Integrated Security=True")
                da.Fill(dt)
                ComboBox1.DataSource = dt.DefaultView
                ComboBox1.DisplayMember = "FullName"
            End Using
        End Sub
    End Class

    I've added it now to our website

    http://www.vb-tips.com/WindowsFormsComboboxWithDataAdapter.ASPX


    Success
    Cor



    Thursday, April 05, 2012 4:33 PM

All replies

  • Try this:

    Dim conn As New SqlConnection("connString")
    Dim da As New SqlDataAdapter("SELECT ColumnName FROM TableName")
    Dim table As New DataTable("DataFormComboBox")
    da.Fill(table)
    comboBox1.DataSource = table.DefaultView
    comboBox1.DisplayMember = "ColumnName"
    'dispose IDisposable classes:
    conn.Dispose()
    da.Dispose()



    Mitja

    Thursday, April 05, 2012 4:32 PM
  • I thought we had a sample for you, it was not like that so I made it.

    Imports System.Data.SqlClient
    Public Class Form1
        ' for earlier versions than VB10SP1 add byval
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim dt As New DataTable
            Using da As New SqlDataAdapter("Select Firstname + ' ' + LastName as FullName from Employees",
                                           "Data Source=MyServer;Initial Catalog=Northwind;Integrated Security=True")
                da.Fill(dt)
                ComboBox1.DataSource = dt.DefaultView
                ComboBox1.DisplayMember = "FullName"
            End Using
        End Sub
    End Class

    I've added it now to our website

    http://www.vb-tips.com/WindowsFormsComboboxWithDataAdapter.ASPX


    Success
    Cor



    Thursday, April 05, 2012 4:33 PM
  • hello Cor Ligthert

    thanks. it's worked fine

    Realy I APPRECIATE you

    then how to SQL with Conditions. Means

    Dim EmpID As String

    EmpID = "A0012"

    WHERE (EmployeeID = EmpID)

    i used like below but get error why?

     Using da As New SqlDataAdapter("Select Firstname + ' ' + LastName as FullName from Employees WHERE (EmployeeId = EmpID)",
                                          
    "Data Source=MyServer;Initial Catalog=Northwind;Integrated Security=True")

    can help me

    with regards

    Thursday, April 05, 2012 10:38 PM
  • Try the following one,

    Using da As New SqlDataAdapter("Select Firstname + ' ' + LastName as FullName from Employees WHERE (EmployeeId = " + EmpID +)",
                                           "Data Source=MyServer;Initial Catalog=Northwind;Integrated Security=True")

    Regards,

    Narendran Ponpandiyan

    Friday, April 06, 2012 7:02 AM
  • You can do it like this, but this is not really a way for a combobox, it fills only one row, so don't mark it as answer on your question.

      Using da As New SqlDataAdapter("Select Firstname + ' ' + LastName as FullName from Employees Where ID = @ID",
                                           "Data Source=KAMER\KAMER;Initial Catalog=Northwind;Integrated Security=True")
                da.SelectCommand.Parameters.AddWithValue("@ID" = EmpID)
                da.Fill(dt)


    Success
    Cor

    Friday, April 06, 2012 12:00 PM
  • hello

    Cor Ligthert

    thanks many many thanks

    it's worked fine

    Saturday, April 07, 2012 12:32 AM