|
-
Mar 19th, 2011, 11:01 AM
#1
Thread Starter
Member
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.
-
Mar 19th, 2011, 11:30 AM
#2
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.
-
Mar 20th, 2011, 01:47 AM
#3
Thread Starter
Member
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.
-
Mar 20th, 2011, 04:04 AM
#4
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.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|