Results 1 to 4 of 4

Thread: Adding Combo box into a Windows Form.

  1. #1

    Thread Starter
    Member Ahmed Shahni's Avatar
    Join Date
    Feb 2011
    Posts
    45

    Lightbulb Adding Combo box into a Windows Form.

    Dear Sir,

    I'm using windows form (vb2010) for data entry.. so now I want to add a combo box into my windows form using sql code because all my connection & Inserting records in SQL command. And other important matter is the combo box should connect to my SQL Server database Table.

    Combo table Name : TblPosition
    Data entry Table : EmployeeList (with EmpId, EmpName, Position, BasicSalary) fields.

    Please help me in this regard,
    Thanks.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Adding Combo box into a Windows Form.

    ComboBoxes don't connect to databases. A ComboBox just displays a list of data. If you want to get data from a database then, using ADO.NET, there are just three ways to do it:

    1. Create a DbCommand and call ExecuteScalar to get a single value.
    2. Create a DbCommand and call ExecuteReader to create a DbDataReader.
    3. Create a DataAdapter and call Fill to populate a DataTable.

    That's the case no matter what you intend to do with the data. If you already know how to populate a DataTable then you already know how to get the data. If you don't know then follow the Database FAQ link in my signature and check out the .NET resources. One of them is my own ADO.NET code examples.

    Displaying data in a ComboBox is the same no matter where the data comes from. It has no specific connection to databases. You eat a cake the same way no matter whether your mother made it or you bought it from a shop, right? To display data in a ComboBox you can either add to its Items collection or, if you already have a list, use data-binding. If you have a DataTable, that's your list. Set the ValueMember to the name of the PK column, the DisplayMember to the name of the column you want to display and the DataSource to the Datatable itself. Done.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member Ahmed Shahni's Avatar
    Join Date
    Feb 2011
    Posts
    45

    Re: Adding Combo box into a Windows Form.

    Sir I got help form your ADO.NET thanks..but when I run the error message "EmpPosition not defined" so please correct my codes.
    My requirement:- I want to have a combobox on my windows form and the combo should contain records from Database Table in SqlServer.

    Imports System.Data.SqlClient
    Public Class EmpEntryForm
    Dim connstring As String = "Data Source=.\SQLExpress;Initial Catalog=StudioSamra;Integrated Security=True;Pooling=False"
    Dim connection As New SqlConnection(connstring)
    Dim sSql As String = "select position from EmpList"
    Dim da As New SqlDataAdapter(sSql, connection)
    Dim dataset As New DataSet()

    Private Sub EmpEntryForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    connection.Open()
    Me.ComboBox1.ValueMember = "EmpPosition"
    Me.ComboBox1.DataSource = connection.GetSchema("EmpPosition")
    End Sub

    Thanks a lot.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Adding Combo box into a Windows Form.

    I'm fairly sure that you think that this is doing something other than what it is:
    Code:
    Me.ComboBox1.DataSource = connection.GetSchema("EmpPosition")
    You should be populating a DataTable and using that as the data source. That thread of mine shows you how to populate a DataTable.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width