|
-
Aug 19th, 2010, 06:18 PM
#1
Thread Starter
Addicted Member
-
Aug 19th, 2010, 06:32 PM
#2
Re: SQL SELECT Statement mismatch
Which line gives you the error?
My usual boring signature: Nothing
 
-
Aug 19th, 2010, 06:44 PM
#3
Thread Starter
Addicted Member
Re: SQL SELECT Statement mismatch
On this line, when the data_adapter is filled with data_table query. The SQL statement... but... why ?
Code:
data_adapter.Fill(data_table)
Last edited by Alexandru_mbm; Aug 19th, 2010 at 08:43 PM.
I'm still learning VB.NET
Sorry for my bad english
Thanks for your help
-
Aug 19th, 2010, 08:57 PM
#4
Fanatic Member
Re: SQL SELECT Statement mismatch
Code:
SELECT id_serviciu FROM contracte WHERE stare='activ' AND id_beneficiar='" & Me.txt_cod_beneficiar.Text.ToString & "'
What datatype is id_beneficiar? If it is a numeric (integer, possibly an Identity or Autonumber?) then the exception results because you are trying to specify a parameter of type String.
You are setting single quotes around the concatenated value, which tells your database to treat the item between the sincle quotes as a String. If id_beneficiar is an Integer type, this will cause the exception you are seeing . . .
Try THIS (If the datatype of id_beneficiar is a Text, VarChar, or type OTHER than Integer, this won't work):
Code:
'Convert the String value of the text box to Integer:
Dim IdBeneficiar As Integer = Ctype(Me.txt_cod_beneficiar.Text, Integer)
'Concatenate the variable into your SQL string:
SELECT id_serviciu FROM contracte WHERE stare='activ' AND id_beneficiar= " & IdBeneficiar & ""
Lastly, I recommend checking out the FAQ section of the Database Development forum and read up on the use of Parameters in your SQL Code . . .
Hope that helps!
-
Aug 19th, 2010, 08:58 PM
#5
Re: SQL SELECT Statement mismatch
Is 'id_beneficiar' a number, by any chance?
-
Aug 20th, 2010, 04:14 AM
#6
Thread Starter
Addicted Member
-
Aug 20th, 2010, 08:48 AM
#7
Fanatic Member
Re: SQL SELECT Statement mismatch
I just woke up, and I am not fully functioning yet. Can you post the rest of your code? ALso , can you tell us a little more about your tables? Is there a Primary Key in one, and a foreign key in the other?
I suspect there is a better way to acheive whatever it is you are attempting to do using IN. I can't check this right now, but I also have a feeling the IN keyword may be part of the problem. It looks to me like you are using IN where you should be using an INNER JOIN. Barring that, possibly the EXISTS keyword?
I am off to work, but I will be interested to see what you post, and what other responses come along.
I suggest checking out INNER JOIN, if there are indeed related records in your two tables. If not, then look into EXISTS (this one can get a little messy, but is actually a very efficient way of returning results in cartin cases . . .).
-
Aug 20th, 2010, 09:48 AM
#8
Thread Starter
Addicted Member
Re: SQL SELECT Statement mismatch
I have replaced my "data_adapter" with this one
Code:
data_adapter = New OleDb.OleDbDataAdapter("SELECT servicii.id, servicii.denumire, servicii.pret FROM servicii INNER JOIN contracte ON servicii.id=contracte.id_serviciu WHERE contracte.id_beneficiar=" & Me.txt_cod_beneficiar.Text & " ORDER BY servicii.id".ToString, conexiune)
For all others who may have a similar problem here is the final working code...
Code:
Private Sub afisare_lista_servicii_contractate()
'######################################################################
' http://www.vbforums.com/showthread.php?p=3868568#post3868568
'######################################################################
conectare_db()
Try
data_table = New DataTable
data_adapter = New OleDb.OleDbDataAdapter("SELECT servicii.id, servicii.denumire, servicii.pret FROM servicii INNER JOIN contracte ON servicii.id=contracte.id_serviciu WHERE contracte.id_beneficiar=" & Me.txt_cod_beneficiar.Text & " ORDER BY servicii.id".ToString, conexiune)
command_builder = New OleDb.OleDbCommandBuilder(data_adapter)
data_adapter.Fill(data_table)
Me.dgv_lista_servicii_contractate.DataSource = data_table
Dim coloana_1 As DataGridViewColumn = dgv_lista_servicii_contractate.Columns(1)
Me.dgv_lista_servicii_contractate.Columns(0).Width = 90
coloana_1.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
Me.dgv_lista_servicii_contractate.Columns(2).Width = 90
Me.dgv_lista_servicii_contractate.Columns(0).HeaderText = "Cod"
Me.dgv_lista_servicii_contractate.Columns(1).HeaderText = "Denumire / Descriere"
Me.dgv_lista_servicii_contractate.Columns(2).HeaderText = "Pret"
Catch ex As Exception
MessageBox.Show("Eroare:" & vbCrLf & vbCrLf & ex.Message, "Eroare", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
deconectare_db()
End Sub
Thanks alot RunsWithScissors for the big help.
Last edited by Alexandru_mbm; Aug 20th, 2010 at 09:51 AM.
I'm still learning VB.NET
Sorry for my bad english
Thanks for your help
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
|