Click here to Skip to main content
15,886,110 members
Articles / Desktop Programming / MFC

The Ultimate Grid Home Page

,
Rate me:
Please Sign up or sign in to vote.
5.00/5 (122 votes)
14 Sep 2013CPOL10 min read 4.6M   81.9K   418  
The Ultimate Grid is now Open Source
// ConnectDlg.cpp : implementation file
//

#include "stdafx.h"
#include "selectsqldlg.h"
#include "db7.h"
#include "ConnectDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CConnectDlg dialog


CConnectDlg::CConnectDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CConnectDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CConnectDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT

	// one time init
	m_nOpenType = CRecordset::dynaset;
}


void CConnectDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CConnectDlg)
	DDX_Control(pDX, IDC_SQLDOTBUTTON, m_sqlButton);
	DDX_Control(pDX, IDC_SQLEDIT, m_sqlEdit);
	DDX_Control(pDX, IDC_DSNCOMBO, m_dsnCombo);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CConnectDlg, CDialog)
	//{{AFX_MSG_MAP(CConnectDlg)
	ON_BN_CLICKED(IDC_BROWSEBUTTON, OnBrowsebutton)
	ON_BN_CLICKED(IDC_RADIODYNASET, OnRadiodynaset)
	ON_BN_CLICKED(IDC_RADIOSNAPSHOT, OnRadiosnapshot)
	ON_BN_CLICKED(IDC_SQLDOTBUTTON, OnSqldotbutton)
	ON_CBN_SELCHANGE(IDC_DSNCOMBO, OnSelchangeDsncombo)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CConnectDlg message handlers

void CConnectDlg::OnBrowsebutton() 
{
	// empty the array of SQL statements
	m_selectSQLDlg.m_strSQLArray.RemoveAll();

	if(UG_SUCCESS == m_pOdbc->Browse(this->m_hWnd)) {
		//  get the table names from the datasource...
		int i = 0;
		SQLDatasourceInfo*	dbI;
		CString sqlString;
		
		while(dbI = m_pOdbc->GetTableInfo(i++)){
			// ... and put them in the list in the select sql dialog
            // Put [] around name only if has spaces
            if(0 != _tcschr((LPTSTR)dbI->tableName, 32))
			    sqlString.Format(_T("Select * FROM [%s]"),dbI->tableName);
            else
			    sqlString.Format(_T("Select * FROM %s"),dbI->tableName);
			m_selectSQLDlg.m_strSQLArray.Add(sqlString);
		}
		// now allow the list to be invoked
		m_sqlButton.EnableWindow(TRUE);
	}
	m_strDSN = *m_pOdbc->GetConnectString();
	m_selectSQLDlg.m_strDSN = m_strDSN;
	if(!m_strDSN.IsEmpty()) {
		CStringArray *strArray = &((CDb7App*)AfxGetApp())->m_arrStrDsn;
		strArray->Add(m_strDSN);
		m_dsnCombo.SetCurSel(m_dsnCombo.AddString(m_strDSN));
	}
}

BOOL CConnectDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	CStringArray *strArray = &((CDb7App*)AfxGetApp())->m_arrStrDsn;
	int hiIndex = strArray->GetUpperBound();
	for(int i = 0; i <= hiIndex; i++) {
		m_dsnCombo.AddString(strArray->GetAt(i));
	}


	CButton *cbSnapshot = (CButton *) GetDlgItem(IDC_RADIOSNAPSHOT);
	CButton *cbDynaset = (CButton *) GetDlgItem(IDC_RADIODYNASET);

	if(m_nOpenType == CRecordset::snapshot) {
		cbDynaset->SetCheck(0);
		cbSnapshot->SetCheck(1);
	}
	else {
		cbDynaset->SetCheck(1);
		cbSnapshot->SetCheck(0);
	}		

	m_dsnCombo.SetWindowText(m_strDSN);
	m_sqlEdit.SetWindowText(m_strSQL);

	if(m_strDSN = m_selectSQLDlg.m_strDSN)
		m_sqlButton.EnableWindow(TRUE);
	else
		m_sqlButton.EnableWindow(FALSE);


	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CConnectDlg::OnRadiodynaset() 
{
	// TODO: Add your control notification handler code here
	m_nOpenType = CRecordset::dynaset;
}

void CConnectDlg::OnRadiosnapshot() 
{
	// TODO: Add your control notification handler code here
	m_nOpenType = CRecordset::snapshot;
}


void CConnectDlg::OnOK() 
{
	// TODO: Add extra validation here
	m_dsnCombo.GetWindowText(m_strDSN);
	m_sqlEdit.GetWindowText(m_strSQL);
	
	CDialog::OnOK();
}

void CConnectDlg::OnSqldotbutton() 
{
	// TODO: Add your control notification handler code here
	if(IDOK == m_selectSQLDlg.DoModal())
		m_sqlEdit.SetWindowText(m_selectSQLDlg.m_strSQL);
}

void CConnectDlg::OnSelchangeDsncombo() 
{
	// TODO: Add your control notification handler code here

	int pos = m_dsnCombo.GetCurSel();
	if(-1 != pos)
		m_dsnCombo.GetLBText(pos,m_strDSN);

	if(m_strDSN == m_selectSQLDlg.m_strDSN) {
		m_sqlButton.EnableWindow(TRUE);
	}
	else {
		m_sqlButton.EnableWindow(FALSE);
		m_sqlEdit.SetWindowText(_T(""));
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Canada Canada
In January 2005, David Cunningham and Chris Maunder created TheUltimateToolbox.com, a new group dedicated to the continued development, support and growth of Dundas Software’s award winning line of MFC, C++ and ActiveX control products.

Ultimate Grid for MFC, Ultimate Toolbox for MFC, and Ultimate TCP/IP have been stalwarts of C++/MFC development for a decade. Thousands of developers have used these products to speed their time to market, improve the quality of their finished products, and enhance the reliability and flexibility of their software.
This is a Organisation

476 members

Written By
Software Developer (Senior)
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions