943,731 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 4785
  • VB.NET RSS
Jun 19th, 2008
0

combobox selected value connected to a SQL statement

Expand Post »
Hi I have a prob with my code.
This way I get to see all the fields in table1 in the grid view but they hold no values! if u can help me make changes in this code..it will great!

Imports System.Data.oledb

Public Class Form1

Dim con As OleDbConnection = New OleDb.OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=C:\Documents and Settings\bashkark\Desktop\USERS.mdb")

Dim cmd As OleDbCommand




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

cmd = New OleDbCommand("select * from table1", con)

con.open()






ComboBox1.Items.Add("DELL")

ComboBox1.Items.Add("HP")

ComboBox2.Items.Add("HP xw6600 WS")

ComboBox2.Items.Add("HP xw6400 WS")

ComboBox2.Items.Add("HP xw4400 WS")

ComboBox2.Items.Add("HP Precision WS 670")

ComboBox2.Items.Add("HP Precision WS 690")

ComboBox2.Items.Add("HP Precision WS 650")

ComboBox2.Items.Add("HP Precision WS 470")

ComboBox2.Items.Add("HP Precision WS M65")

ComboBox2.Items.Add("HP Precision WS 530")

ComboBox2.Items.Add("HP Precision WS 550")

ComboBox3.Items.Add("1 GB")

ComboBox3.Items.Add("2 GB")

ComboBox3.Items.Add("3 GB")

ComboBox3.Items.Add("3.25 GB")

ComboBox3.Items.Add("8 GB")

ComboBox4.Items.Add("NVIDIA QUADRO FX4600")

ComboBox4.Items.Add("NVIDIA QUADRO FX540")

ComboBox4.Items.Add("NVIDIA QUADRO FX1500")

ComboBox4.Items.Add("NVIDIA QUADRO FX3450/4000 SDI")

ComboBox4.Items.Add("NVIDIA QUADRO 2 PRO")

ComboBox4.Items.Add("NVIDIA QUADRO FX1400")

ComboBox4.Items.Add("NVIDIA QUADRO FX3400")

ComboBox4.Items.Add("NVIDIA QUADRO 4900XGL")

ComboBox4.Items.Add("NVIDIA QUADRO 700XGL")

ComboBox4.Items.Add("3D Labs Wildcat T4 7110")

ComboBox5.Items.Add("6.14.11.6262")

ComboBox5.Items.Add("8.1.7.6")

ComboBox5.Items.Add("6.14.11.6008")

ComboBox5.Items.Add("6.6.0.35")

ComboBox5.Items.Add("8.4.2.6")

ComboBox5.Items.Add("6.1.2.9")

ComboBox5.Items.Add("8.4.8.0")

ComboBox5.Items.Add("6.6.0.38")

ComboBox5.Items.Add("6.14.11.6250")

ComboBox5.Items.Add("6.5.7.3.")

ComboBox5.Items.Add("8.3.1.3")

ComboBox5.Items.Add("3.0.8.2")

ComboBox5.Items.Add("5.6.7.3")

ComboBox5.Items.Add("6.1.6.1")

ComboBox5.Items.Add("7.2.2.3")

ComboBox5.Items.Add("3.0.8.2")

ComboBox5.Items.Add("8.4.3.7")

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

cmd = New OleDbCommand("select * from table1 where MFG='& combobox1.text &' ", con)

Dim da As OleDbDataAdapter = New OleDbDataAdapter(cmd)

Dim ds As DataSet = New DataSet()

da.Fill(ds, "table1")

DataGridView1.DataSource = ds.Tables("table1").DefaultView




End Sub

End Class
Reputation Points: 10
Solved Threads: 0
Junior Poster
kavithabhaskar is offline Offline
123 posts
since Jun 2008
Jun 21st, 2008
1

Re: combobox selected value connected to a SQL statement

cmd = New OleDbCommand("select * from table1 where MFG='& combobox1.text &' ", con)

I guess this should be-

cmd = New OleDbCommand("select * from table1 where MFG='" & Me.ComboBox1.SelectedItem & "'" ,con)

The code you have at present checks if the value & combobox1.text & is present in the field MFG and hence you are getting no records in your Data Grid View.

Its just that you have not used the concatenation correctly.

Hope it helps!
Reputation Points: 32
Solved Threads: 14
Junior Poster
tuse is offline Offline
173 posts
since Jul 2007
Jun 22nd, 2008
0

Re: combobox selected value connected to a SQL statement

Hi Tuse:
Thanks for your reply. I had observed that mistake and i rectified it. Now, when i pull down any value from combobox1 and hit the click button it works perfectly fine with the queries being displayed on my datagridview.

How do i do this for my combobox2 ? I have a set of values in combobox2 and I am sending my code along with this email. Please take a look at it and reply.

What I want to do is, even if i dont choose any value from combobox1 and just choose a value from combobox2, i still want the queries to be displayed.

It 'll be very helpful if you can correct me.

Thanks,
Kavitha.

Imports System.Data.oledb

Public Class Form1

Dim con As OleDbConnection = New OleDb.OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=C:\Documents and Settings\bashkark\Desktop\USERS.mdb")

Dim cmd As OleDbCommand

Dim cmd1 As OleDbCommand




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

con.open()

ComboBox1.Items.Add("DELL")
ComboBox1.Items.Add("HP")

ComboBox2.Items.Add("HP xw6600 WS")
ComboBox2.Items.Add("HP xw6400 WS")
ComboBox2.Items.Add("HP xw4400 WS")
ComboBox2.Items.Add("HP Precision WS 670")
ComboBox2.Items.Add("HP Precision WS 690")
ComboBox2.Items.Add("HP Precision WS 650")
ComboBox2.Items.Add("HP Precision WS 470")
ComboBox2.Items.Add("HP Precision WS M65")
ComboBox2.Items.Add("HP Precision WS 530")
ComboBox2.Items.Add("HP Precision WS 550")

ComboBox3.Items.Add("1 GB")
ComboBox3.Items.Add("2 GB")
ComboBox3.Items.Add("3 GB")
ComboBox3.Items.Add("3.25 GB")
ComboBox3.Items.Add("8 GB")

ComboBox4.Items.Add("NVIDIA QUADRO FX4600")
ComboBox4.Items.Add("NVIDIA QUADRO FX540")
ComboBox4.Items.Add("NVIDIA QUADRO FX1500")
ComboBox4.Items.Add("NVIDIA QUADRO FX3450/4000 SDI")
ComboBox4.Items.Add("NVIDIA QUADRO 2 PRO")
ComboBox4.Items.Add("NVIDIA QUADRO FX1400")
ComboBox4.Items.Add("NVIDIA QUADRO FX3400")
ComboBox4.Items.Add("NVIDIA QUADRO 4900XGL")
ComboBox4.Items.Add("NVIDIA QUADRO 700XGL")
ComboBox4.Items.Add("3D Labs Wildcat T4 7110")

ComboBox5.Items.Add("6.14.11.6262")
ComboBox5.Items.Add("8.1.7.6")
ComboBox5.Items.Add("6.14.11.6008")
ComboBox5.Items.Add("6.6.0.35")
ComboBox5.Items.Add("8.4.2.6")
ComboBox5.Items.Add("6.1.2.9")
ComboBox5.Items.Add("8.4.8.0")
ComboBox5.Items.Add("6.6.0.38")
ComboBox5.Items.Add("6.14.11.6250")
ComboBox5.Items.Add("6.5.7.3.")
ComboBox5.Items.Add("8.3.1.3")
ComboBox5.Items.Add("3.0.8.2")
ComboBox5.Items.Add("5.6.7.3")
ComboBox5.Items.Add("6.1.6.1")
ComboBox5.Items.Add("7.2.2.3")
ComboBox5.Items.Add("3.0.8.2")
ComboBox5.Items.Add("8.4.3.7")

MsgBox("please select any combination which could be just one value or more than one of your choice and hit the click button")

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

cmd = New OleDbCommand("select * from table1 where Mfg='" & ComboBox1.Text & "' ", con)

Dim da As OleDbDataAdapter = New OleDbDataAdapter(cmd)

cmd1 = New OleDbCommand("select * from table1 where Make='" & ComboBox2.Text & "' ", con)

Dim da1 As OleDbDataAdapter = New OleDbDataAdapter(cmd1)

Dim ds As DataSet = New DataSet()

da.Fill(ds, "table1")

DataGridView1.DataSource = ds.Tables("table1").DefaultView

MsgBox(" If you are done with your search, please hit the exit button to exit")

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
End
End Sub
End Class














Click to Expand / Collapse  Quote originally posted by tuse ...
cmd = New OleDbCommand("select * from table1 where MFG='& combobox1.text &' ", con)

I guess this should be-

cmd = New OleDbCommand("select * from table1 where MFG='" & Me.ComboBox1.SelectedItem & "'" ,con)

The code you have at present checks if the value & combobox1.text & is present in the field MFG and hence you are getting no records in your Data Grid View.

Its just that you have not used the concatenation correctly.

Hope it helps!
Reputation Points: 10
Solved Threads: 0
Junior Poster
kavithabhaskar is offline Offline
123 posts
since Jun 2008
Jun 22nd, 2008
0

Re: combobox selected value connected to a SQL statement

You should use the OR statement in your SQL query.

So your SQL command text should be-

cmd = New OleDbCommand("select * from table1 where MFG='" & Me.ComboBox1.SelectedItem & "' OR MFG='" & Me.ComboBox2.SelectedItem & "'" ,con)


I am guessing that the values from the 2nd ComboBox are also queried against the MFG field that you have in the database. If this is not so, change it accordingly as per the required field.

You can also add multiple clauses with the OR.

However if what you wish is something like this-

ComboBox1 - Value A
ComboBox2 - Value B
ComboBox3 - Value C
ComboBox4 - Value D

i.e. if you select value A in 1st box, value B in 2nd , value C for 3rd....
and it should show records where field1 in database matches Value A, field2 matches Value B .....etc, then use the AND statement
Reputation Points: 32
Solved Threads: 14
Junior Poster
tuse is offline Offline
173 posts
since Jul 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: Calculate File Size on Disk in KB
Next Thread in VB.NET Forum Timeline: update problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC