Results 1 to 3 of 3

Thread: Validation of correct database connection

  1. #1
    Join Date
    Mar 2014
    Posts
    11
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Validation of correct database connection

    I'm trying to connect with a database created with Microsoft Sql Express 2012. I'm using qt 5.2.1, and at the end of the connection implementetion I write some code to have a validation. But an error accure, and I don't know why. The program is structured in this way:

    -in the MainWindow of qt GUI application there is a Button, if you click on this Button you open a Dialog Application. -in the Dialog Application is devolped the connection.

    here the .h code of the Dialog Application:
    Qt Code:
    1. #ifndef DATABASE_H
    2. #define DATABASE_H
    3.  
    4. #include <QDialog>
    5.  
    6. namespace Ui {
    7. class DataBase;
    8. }
    9.  
    10. class DataBase : public QDialog
    11. {
    12. Q_OBJECT
    13.  
    14.  
    15.  
    16. public:
    17. explicit DataBase(QWidget *parent = 0);
    18. ~DataBase();
    19.  
    20. private:
    21. Ui::DataBase *ui;
    22. };
    23.  
    24. #endif // DATABASE_H
    To copy to clipboard, switch view to plain text mode 

    here the relative .cpp code of Dialog Application:

    Qt Code:
    1. #include <database.h>
    2. #include <ui_database.h>
    3. #include <QtSql>
    4. #include <QtDebug>
    5. #include <iostream>
    6. DataBase::DataBase(QWidget *parent) :
    7. QDialog(parent),
    8. ui(new Ui::DataBase)
    9. {
    10.  
    11. QString servername="MARCO-VAIO";
    12. QString dbname="IfaCOM";
    13. QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
    14. db.setConnectOptions();
    15. QString dsn = QString("DRIVER={SQL Native Client}; SERVER=%1; DATABASE=%2;TRUSTED_CONNECTION=Yes;").arg(servername).arg(dbname);
    16. db.setDatabaseName(dsn);
    17. if(db.open()){
    18. ui->label->setText("Connected....");
    19. }
    20.  
    21.  
    22. }
    23.  
    24. DataBase::~DataBase()
    25. {
    26. delete ui;
    27. }
    To copy to clipboard, switch view to plain text mode 

    and here the error that accured:
    Cattura.JPG

    Please check this issue...thanks in advance.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Validation of correct database connection

    You are missing the ui->setupUi(this) call, so your call to ui->label accesses an invalid pointer.

    Cheers,
    _

  3. #3
    Join Date
    Mar 2014
    Posts
    11
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Validation of correct database connection

    thank you very much...

Similar Threads

  1. database connection
    By mak_user in forum Newbie
    Replies: 1
    Last Post: 9th February 2011, 09:23
  2. Connection With database
    By sudheer168 in forum Qt Programming
    Replies: 4
    Last Post: 22nd December 2010, 09:18
  3. Database connection
    By poporacer in forum Newbie
    Replies: 7
    Last Post: 7th September 2010, 07:49
  4. database connection
    By peace_comp in forum Qt Programming
    Replies: 4
    Last Post: 13th May 2008, 12:16
  5. Oracle Database Connection
    By ToddAtWSU in forum Qt Programming
    Replies: 6
    Last Post: 20th December 2007, 15:18

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.