I'm having problem with a query which is based on fixed-part and all I do is that I'm modifying part after WHERE something.value='... .
This whole text is a string which I need to be connected to my query.
'113') AND (reklamace.kod_reklamace = '932' '

Error message:
Conversion failed when converting the nvarchar value ''113').......' to data type int.

Could any of you help me with this query? Thank you

PS: I forgot to write that its a MS SQL 2008 Express server

Edited by Ycefire: n/a

7 Years
Discussion Span
Last Post by Ycefire

From the part you posted you may have too many single quotes. Note that an integer column does not need to have single quotes around the value. Can't say if there is anything else, unless you post your entire query.


Heres the entire code: (with ASP.NET)

Label called lblDotaz is changed by code, and query is completed by its value "text"

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:DbConnStr %>" 
        SelectCommand="SELECT reklamace.kod_reklamace, reklamace.cislo_rekl_zakazky, reklamace.kod_vyrobku, vyrobky.nazev, reklamace.stav, reklamace.reklamovane_mnozstvi, reklamace.jednotka, reklamace.pr, reklamace.tp, reklamace.tot, reklamace.r, reklamace.datum_pr_reklamace, reklamace.datum_pr_vzor, reklamace.no_odeslano_dne, reklamace.datum_uz_reklamace FROM reklamace INNER JOIN vyrobky ON vyrobky.kod_vyrobku = reklamace.kod_vyrobku WHERE (reklamace.cislo_rekl_zakazky = @cislo_rekl_zakazky)">
            <asp:ControlParameter ControlID="lblDotaz" Name="cislo_rekl_zakazky" 
                PropertyName="Text" Type="String" />

Heres the button which causes Searching

Sub cmdVyhledat_Click() Handles cmdVyhledat.Click
        Dim retezec As String
        If (txtCisloReklamace.Text <> "") And (txtNazevVyrobku.Text <> "") Then
            lblDotaz.Visible = True
            lblDotaz.Text = "Chyba, není možné vyhledávat pomocí spojeného výrazu"
        ElseIf (txtCisloReklamace.Text <> "") And (txtNazevVyrobku.Text = "") Then
            retezec = "'" & Session("jmeno") & "')" & " AND (reklamace.kod_reklamace = '" & txtCisloReklamace.Text & "'"
            'Session.Add("hledani", retezec)
            lblDotaz.Text = retezec

           ' retezec = Session("jmeno") & " AND (vyrobek.nazev = " & txtNazevVyrobku.Text 
         '   Session.Add("hledani", retezec)

        End If


    End Sub

So lets break it: Theres a fix part SELECT * FROM table INNER JOIN another_table ON .... WHERE table.something = MY DYNAMIC PART )

When "MY DYNAMIC PART" is equal just to one int number query works fine, but when its more complex its not working with cannot convert nvarchar to int

Edited by Ycefire: n/a


Echo the query that is generated before you use it (including the dynamic part). I'm sure it is a quoting issue.

retezec = "'" // why start with a single quote ?
  & Session("jmeno")  // what is in here ?
  & "')" // why a single quote AND a parenthesis ?
  & " AND (reklamace.kod_reklamace = '" 
  & txtCisloReklamace.Text 
  & "'"
retezec = "'" // why start with a single quote ?
  & Session("jmeno")  // what is in here ?
  & "')" // why a single quote AND a parenthesis ?
  & " AND (reklamace.kod_reklamace = '" 
  & txtCisloReklamace.Text 
  & "'"

AD start with single quote: sorry that was my fault, I was so down with code not working I've tried all I could imagine.

Session("jmeno") contains integer number

Single quote and then parenthesis same as start with a single quote, my fault

The problem is that query won't even be created.
This is what I get as an error: Conversion failed when converting the nvarchar value '113) AND (reklamace.kod_reklamace = 932 ' to data type int.


I'd like to ask mods to delete this thread, since I discovered mine query is correct, only ASP.NET can't handle mine data type conversions (still don't know why)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.