PDA

View Full Version : Validation of correct database connection



cecchinoSMI
5th March 2014, 09:02
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:

#ifndef DATABASE_H
#define DATABASE_H

#include <QDialog>

namespace Ui {
class DataBase;
}

class DataBase : public QDialog
{
Q_OBJECT



public:
explicit DataBase(QWidget *parent = 0);
~DataBase();

private:
Ui::DataBase *ui;
};

#endif // DATABASE_H

here the relative .cpp code of Dialog Application:


#include <database.h>
#include <ui_database.h>
#include <QtSql>
#include <QtDebug>
#include <iostream>
DataBase::DataBase(QWidget *parent) :
QDialog(parent),
ui(new Ui::DataBase)
{

QString servername="MARCO-VAIO";
QString dbname="IfaCOM";
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setConnectOptions();
QString dsn = QString("DRIVER={SQL Native Client}; SERVER=%1; DATABASE=%2;TRUSTED_CONNECTION=Yes;").arg(servername).arg(dbname);
db.setDatabaseName(dsn);
if(db.open()){
ui->label->setText("Connected....");
}


}

DataBase::~DataBase()
{
delete ui;
}

and here the error that accured:
10104

Please check this issue...thanks in advance.

anda_skoa
5th March 2014, 09:33
You are missing the ui->setupUi(this) call, so your call to ui->label accesses an invalid pointer.

Cheers,
_

cecchinoSMI
6th March 2014, 12:14
thank you very much...