Results 1 to 8 of 8

Thread: SQL SELECT Statement mismatch

  1. #1

    Thread Starter
    Addicted Member Alexandru_mbm's Avatar
    Join Date
    Jul 2007
    Location
    VBForums.com
    Posts
    157

    Angry SQL SELECT Statement mismatch

    What's wrong with this ?

    ERROR: Type mismatch in expression.

    Code:
            conectare_db()
    
            data_table = New DataTable
            data_adapter = New OleDb.OleDbDataAdapter("SELECT id, denumire, pret FROM servicii WHERE id IN (SELECT id_serviciu FROM contracte WHERE stare='activ' AND id_beneficiar='" & Me.txt_cod_beneficiar.Text.ToString & "');".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"
    
            deconectare_db()
    I just can't see what's wrong!!!
    Last edited by Alexandru_mbm; Aug 19th, 2010 at 06:21 PM.
    I'm still learning VB.NET
    Sorry for my bad english
    Thanks for your help

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: SQL SELECT Statement mismatch

    Which line gives you the error?
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Addicted Member Alexandru_mbm's Avatar
    Join Date
    Jul 2007
    Location
    VBForums.com
    Posts
    157

    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

  4. #4
    Fanatic Member
    Join Date
    Jun 2008
    Location
    Portland, OR, USA
    Posts
    659

    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!

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: SQL SELECT Statement mismatch

    Is 'id_beneficiar' a number, by any chance?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Addicted Member Alexandru_mbm's Avatar
    Join Date
    Jul 2007
    Location
    VBForums.com
    Posts
    157

    Re: SQL SELECT Statement mismatch

    Sorry for the delay

    Yes it's a number... LongInteger.

    It's a number (not AutoNumber) stored on the "Contracte" table, to "id_beneficiar" column.

    I've tried the code above (as Integer, LongInteger) but it's giving me the same error, even if i use it as "Text" (with my original code). I just don't get it.
    Last edited by Alexandru_mbm; Aug 20th, 2010 at 04:33 AM.
    I'm still learning VB.NET
    Sorry for my bad english
    Thanks for your help

  7. #7
    Fanatic Member
    Join Date
    Jun 2008
    Location
    Portland, OR, USA
    Posts
    659

    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 . . .).

  8. #8

    Thread Starter
    Addicted Member Alexandru_mbm's Avatar
    Join Date
    Jul 2007
    Location
    VBForums.com
    Posts
    157

    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
  •  



Click Here to Expand Forum to Full Width