1,105,226 Community Members

How to bind combobox to sql database?

Member Avatar
bernardz26
Newbie Poster
16 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hello programmers!.
Please help me. :(
I have here a combobx1 that has a value of Mr.A and Mr.B. want that if i choose from my combobox1 the selected value will go to my sql database.

Member Avatar
bernardz26
Newbie Poster
16 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

this is my code.
Dim con As New SqlConnection()
con.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True"
Dim da As New SqlDataAdapter("select * from BSIT", con)
Dim table As New DataTable()
da.Fill(table)
comboBox1.DataSource = New BindingSource(table, Nothing)
ComboBox1.DisplayMember = "BSIT"
ComboBox1.ValueMember = "Bernard"

Member Avatar
M.Waqas Aslam
Practically a Posting Shark
837 posts since Aug 2011
Reputation Points: 67 [?]
Q&As Helped to Solve: 137 [?]
Skill Endorsements: 4 [?]
Featured
 
0
 

ok you can do like this

dim con as new sqlconnection("connection string")
dim cmd as new sqlcommand
con.open()
cmd.connection= con
cmd.commandtext="insert into table1 (field1) values ("& combobox1.text &")"
cmd.executenonquery()
con.close

you can use this code at any button click event to save the value of combo in db and also at selected index change event of your grid .
in place of text you can use the selectedvalue .tostring .
Regards

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: