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



  • 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();
    
        }
    
     }

  • Lifetime Qt Champion

    Hi,

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



  • @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+"'.


  • Lifetime Qt Champion

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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.