Problem in SQL statement in VB.NET.. Assistance Required.

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2008
Posts: 117
Reputation: kavithabhaskar is an unknown quantity at this point 
Solved Threads: 0
kavithabhaskar kavithabhaskar is offline Offline
Junior Poster

Problem in SQL statement in VB.NET.. Assistance Required.

 
0
  #1
Jul 21st, 2008
Can you please tell me what's wrong with this statement ? i get no errors but I get no o/p either!

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click

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

If (ComboBox6.SelectedItem = 0.5) And (ComboBox7.SelectedItem) Then

Dim cmd1 As New OleDbCommand

cmd1 = New OleDbCommand("select * from table1 where RAM between 0.5000 and 2.000")

End If

End Sub
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 22
Reputation: RipperJT is an unknown quantity at this point 
Solved Threads: 3
RipperJT RipperJT is offline Offline
Newbie Poster

Re: Problem in SQL statement in VB.NET.. Assistance Required.

 
0
  #2
Jul 21st, 2008
Hi,

You specified the connection and command objects but you probably want a data reader...also you'll need to open the connection.

  1. Dim dr As OleDbDataReader
  2. dr = cmd1.ExecuteReader(CommandBehavior.Default)
  3.  
  4. Do while dr.read()
  5. variablename = dr("columnName")
  6.  
  7. loop
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 117
Reputation: kavithabhaskar is an unknown quantity at this point 
Solved Threads: 0
kavithabhaskar kavithabhaskar is offline Offline
Junior Poster

Re: Problem in SQL statement in VB.NET.. Assistance Required.

 
0
  #3
Jul 22nd, 2008
Hi Ripper thanks for ur reply..

the error that i get is

"Conversion from string "" to type 'Double' is not valid." at the line

"If (ComboBox6.SelectedItem = "") And (ComboBox7.SelectedItem = "") Then"

please help me rectify this problem!!


Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click

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

Dim cmd1 As New OleDbCommand

If (ComboBox6.SelectedItem = "") And (ComboBox7.SelectedItem = "") Then

MsgBox("No Values have been entered ! ")

End If

cmd1 = New OleDbCommand("select * from table1 where RAM between '%" & ComboBox6.SelectedItem & "%' and '%" & ComboBox7.SelectedItem & "%'", con)

End Sub

--------------------------------------------------------------------------------
KB - Knowledge is Power!
















QUOTE=RipperJT;652629]Hi,

You specified the connection and command objects but you probably want a data reader...also you'll need to open the connection.

  1. Dim dr As OleDbDataReader
  2. dr = cmd1.ExecuteReader(CommandBehavior.Default)
  3.  
  4. Do while dr.read()
  5. variablename = dr("columnName")
  6.  
  7. loop
[/QUOTE]
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 22
Reputation: RipperJT is an unknown quantity at this point 
Solved Threads: 3
RipperJT RipperJT is offline Offline
Newbie Poster

Re: Problem in SQL statement in VB.NET.. Assistance Required.

 
0
  #4
Jul 22nd, 2008
It's trying to convert the datatype on the left side to the empty string on the right side of the =. Try...

  1. If (ComboBox6.SelectedItem = 0) And (ComboBox7.SelectedItem = 0) Then
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 117
Reputation: kavithabhaskar is an unknown quantity at this point 
Solved Threads: 0
kavithabhaskar kavithabhaskar is offline Offline
Junior Poster

Re: Problem in SQL statement in VB.NET.. Assistance Required.

 
0
  #5
Jul 22nd, 2008
Originally Posted by RipperJT View Post
It's trying to convert the datatype on the left side to the empty string on the right side of the =. Try...

  1. If (ComboBox6.SelectedItem = 0) And (ComboBox7.SelectedItem = 0) Then

just curious..what does that statement do ?
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 117
Reputation: kavithabhaskar is an unknown quantity at this point 
Solved Threads: 0
kavithabhaskar kavithabhaskar is offline Offline
Junior Poster

Re: Problem in SQL statement in VB.NET.. Assistance Required.

 
0
  #6
Jul 22nd, 2008
I get the error in the "if" statement. please take a look at it..

if i hit the "click" button directly without choosing the RAM values I expect the Msg box to pop up ..instead i get an error.. ! pls see code for error../



Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click

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

Dim cmd1 As New OleDbCommand

If (ComboBox6.SelectedItem = " ") And (ComboBox7.SelectedItem = " ") Then //error:"conversion from string"" to 'double' is not valid"

MsgBox("No Values have been entered !")

End If

cmd1 = New OleDbCommand("select * from table1 where RAM between '& ComboBox6.SelectedItem & ' and '& ComboBox7.SelectedItem & '", con)

Dim da As OleDbDataAdapter = New OleDbDataAdapter(cmd1)

Try

Dim ds As DataSet = New DataSet()

da.Fill(ds, "table1") //error : data type mismatch in criteria expression

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

Finally

con.Close()

cmd1 = Nothing

da.Dispose()

con.Dispose()

End Try

End Sub
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 117
Reputation: kavithabhaskar is an unknown quantity at this point 
Solved Threads: 0
kavithabhaskar kavithabhaskar is offline Offline
Junior Poster

Re: Problem in SQL statement in VB.NET.. Assistance Required.

 
0
  #7
Jul 22nd, 2008
Data type mismatch in criteria expression.is the error that i get at line da.Fill(ds, "table1") if i modified the code as below (after removing the"if" statement"

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click

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

Dim cmd1 As New OleDbCommand

cmd1 = New OleDbCommand("select * from table1 where RAM between '& ComboBox6.SelectedItem & ' and '& ComboBox7.SelectedItem & '", con)

Dim da As OleDbDataAdapter = New OleDbDataAdapter(cmd1)

Try

Dim ds As DataSet = New DataSet()

da.Fill(ds, "table1")

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

Finally

con.Close()

cmd1 = Nothing

da.Dispose()

con.Dispose()

End Try

End Sub
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 22
Reputation: RipperJT is an unknown quantity at this point 
Solved Threads: 3
RipperJT RipperJT is offline Offline
Newbie Poster

Re: Problem in SQL statement in VB.NET.. Assistance Required.

 
0
  #8
Jul 23rd, 2008
Do you have real values selected in the combobox6 & 7 when the click event is fired or does it only fail when you haven't specified any values? If there isn't any values select then you should not run the query.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 117
Reputation: kavithabhaskar is an unknown quantity at this point 
Solved Threads: 0
kavithabhaskar kavithabhaskar is offline Offline
Junior Poster

Re: Problem in SQL statement in VB.NET.. Assistance Required.

 
0
  #9
Jul 23rd, 2008
hi ripper..either way i get the same error on da.fill(ds,"table1"). that is..when i dont choose any value from either combobox and hit the click and when i choose 1 value from each of the 2 combo boxes and hit click..

yesboth my combo boxes have numbers like.2.980, 3.000,0.5000,3.000,1.000,2.000,3.250 in each of them..

pls suggest something..i have been stuck on this for weeks now..
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 22
Reputation: RipperJT is an unknown quantity at this point 
Solved Threads: 3
RipperJT RipperJT is offline Offline
Newbie Poster

Re: Problem in SQL statement in VB.NET.. Assistance Required.

 
0
  #10
Jul 23rd, 2008
You can try to test to see if a value has been entered and if so then run your code.

  1. If IsNumeric(ComboBox6.SelectedItem) = True And IsNumeric(ComboBox7.SelectedItem) = True Then
  2. If ComboBox7.SelectedItem > ComboBox6.SelectedItem Then
  3.  
  4. 'code here
  5.  
  6. Else
  7.  
  8. 'Msgbox("Ram should be selected")
  9. Exit Sub
  10.  
  11. End If
  12.  
  13. Else
  14. 'exit the sub
  15. 'MsgBox("blah")
  16.  
  17. Exit Sub
  18.  
  19. End If

Here's info an the function IsNumeric

http://msdn.microsoft.com/en-us/libr...w1(VS.71).aspx
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the VB.NET Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC