Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SQL Server]Conversion failed when converting the varchar value 'Boston' to data type int."
Forum Updated to NodeBB v4.3 + New Features

[SQL Server]Conversion failed when converting the varchar value 'Boston' to data type int."

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 1.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    ssoffel
    wrote on 7 Aug 2017, 21:58 last edited by
    #1

    Can someone help explain why I keep getting this error. I have a table with 5 columns and I would like to click on any box and retrieve the row.

    Teams Wins Loses Ties Pts
    Boston 10 10 1 20
    NJ 5 5 1 10
    Clev 1 10 0 2

    For some reason whenever I click on the Teams column I get the error :
    SQL Server]Conversion failed when converting the varchar value 'Boston' to data type int."

    However if I change the query to
    qry.prepare("SELECT * FROM [dbo].[tbl_Record] WHERE Team='"+value+"'); it works and doesn't try to convert teams to int.

    void TableView::on_tableView_doubleClicked(const QModelIndex &index)
    {
    QString value = ui->tableView->model()->data(index).toString();

     QSqlQuery qry;
     qry.prepare("SELECT * FROM [dbo].[tbl_Record] WHERE Team='"+value+"' or Wins='"+value+"' or Loses='"+value+"' ");
    
     if(qry.exec())
     {
        while(qry.next())
        {
             team = qry.value(0).toString();
             wins = qry.value(1).toString();
             loses = qry.value(2).toString();
             ties = qry.value(3).toString();
             pts = qry.value(4).toString();
    
        }
    
     }
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 7 Aug 2017, 22:28 last edited by
      #2

      Hi,

      Can you show the code that you use that triggers that warning ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • S Offline
        S Offline
        ssoffel
        wrote on 7 Aug 2017, 22:35 last edited by
        #3

        @ssoffel said in [SQL Server]Conversion failed when converting the varchar value 'Boston' to data type int.":

        qry.prepare("SELECT * FROM [dbo].[tbl_Record] WHERE Team='"+value+"' or Wins='"+value+"' or Loses='"+value+"' ");

        qry.prepare("SELECT * FROM [dbo].[tbl_Record] WHERE Team='"+value+"' or Wins='"+value+"' or Loses='"+value+"' ");

        The error occurs WHERE Team='"+value+"'.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 8 Aug 2017, 20:14 last edited by
          #4

          You should rather use a prepared query, it will be cleaner.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          1/4

          7 Aug 2017, 21:58

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved