Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

databind to a combo box

Status
Not open for further replies.

kaya17

Programmer
Feb 9, 2004
78
SG
I have an access database with 3tables:names, location, device, all stored text strings. what I want to do is get the text from the database and put it in 3 different combo boxes. how can i set up the connetion with the database? i mean i know how to set it up if to show the records to a list, but to a combo box, really no idea...

so how to bind the data to combo-box?
and how to tell which record the user chooses?

any help would be very much appreciated.

KAYA
 
Are you doing that in a managed or in an unmanaged application?

Ion Filipski
1c.bmp
 
what do u mean managed or unmanaged?
now i am building a database interface, so in the insertion interface, rather than let the user key in the location, i want to provide a combo-box so that it is less error prune....(in case the user keys in the wrong spelling or ect)


any suggestions?
 
is it with .NET virtual machine(clr) ? or it is just Win32?

Ion Filipski
1c.bmp
 
i think it is just win32.
cos i build everything using MFC under Windows

 
In this case your code is unmanaged.
Teher are three different methods of accessing databases, ADO/ADO/ODBC, try the one you like more:
Code:
//using ODBC
#include<windows.h>
#include<iostream>
#include<string>
#include<sql.h>
#include<sqlext.h>
using namespace std;
int main()
{
    HENV hEnv;
    HDBC hDbc;
    RETCODE rc;
    int iOut;
    char strOut[256];
    char szDSN[256] = "driver={Microsoft Access Driver (*.mdb)};dbq=[c:\\db1.mdb];";
    //dsn samples:
    //"driver={Microsoft Access Driver (*.mdb)};dbq=[f:\\db1.mdb];"
    //"driver={SQL Server};pwd={password there};Server={server name};Database={dbname there}"
    //driver names for different databases:
    //{SQL Server}
    //{Microsoft ODBC for Oracle}
    //{Oracle in oracle9}
    //{Microsoft Access Driver (*.mdb)}
    //{MySQL ODBC 3.51 Driver}

    char* szSql = "select * from table1";
    rc = SQLAllocEnv(&hEnv);
    rc = SQLAllocConnect(hEnv, &hDbc);

    rc = SQLDriverConnect(hDbc, NULL, (unsigned char*)szDSN, 
        SQL_NTS, (unsigned char*)strOut, 
        255, (SQLSMALLINT*)&iOut, SQL_DRIVER_NOPROMPT);
    {
        int ival;
        char chval[128];
        int ret1;
        int ret2;
        HSTMT hStmt;
        rc = SQLAllocStmt(hDbc,&hStmt);
        rc = SQLPrepare(hStmt,(unsigned char*)szSql, SQL_NTS);//1
        //rc = SQLBindCol(hStmt, tab_column, tr_type, tr_value, tr_len, len_or_ind);
        rc = SQLBindCol(hStmt, 1, SQL_C_ULONG, &ival, 4, (SQLINTEGER*)& ret1);
		rc = SQLBindCol(hStmt, 2, SQL_C_CHAR, chval, 128, (SQLINTEGER*)&ret2);
        rc = SQLExecute(hStmt); //2
        //if you have queries like drop/create/many update... 
        //instead of //1 //2 and //3 you could use
        //rc=SQLExecDirectA(hStmt,(unsigned char*)szSql,SQL_NTS);
        cout<< ">table:"<< endl;
        while(1) //3
        {
            rc = SQLFetch(hStmt);
            if(rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO)break;
            cout<< "{"<< ival<<"}{"<< chval<< "}"<< endl;
        }
        rc=SQLFreeStmt(hStmt, SQL_DROP);
    }
    rc = SQLDisconnect(hDbc);
    rc = SQLFreeEnv(hEnv);
    return 0;
}

// not automated ADO 2ATL
#import "G:\Program Files\Common Files\System\ADO\msado15.dll"
#include "Debug\msado15.tlh"
#include<atlbase.h>
#include<iostream>

using namespace std;

int main()
{
    CoInitialize(0);
    {
        HRESULT hr = -1;
        CComPtr<IUnknown> cnnobj;
        CComPtr<IUnknown> rdsnobj;
        CComPtr<IUnknown> errobj;
        hr = cnnobj.CoCreateInstance(L"ADODB.Connection.2.7");
        ADODB::_Connection* cnn;
        ADODB::_Recordset* rds;
        ADODB::Error* err;
        BSTR berr;
        ADODB::Fields* fds;
        ADODB::Field* fd1;
        ADODB::Field* fd2;

        
        hr = cnnobj->QueryInterface(&cnn);
        hr = cnn->Open(L"driver={Microsoft Access Driver (*.mdb)};dbq=[c:\\db1.mdb];", L"", L"", -1);
        
        hr = rdsnobj.CoCreateInstance(L"ADODB.Recordset.2.7");
        hr = rdsnobj->QueryInterface(&rds);

        hr = errobj.CoCreateInstance(L"ADODB.Error.2.7");
        hr = errobj->QueryInterface(&err);

        hr = rds->put_ActiveConnection(_variant_t(cnn));
        hr = rds->Open(_variant_t("select * from Table1"), _variant_t(cnn),  ADODB::adOpenUnspecified, ADODB::adLockUnspecified, -1);

        VARIANT_BOOL eof;
        VARIANT_BOOL bof;
        hr = rds->MoveFirst();
        hr = rds->get_Fields(&fds);
        hr = fds->get_Item(_variant_t(L"X"), &fd1);
        hr = fds->get_Item(_variant_t(L"Y"), &fd2);
        BSTR namex;
        BSTR namey;
        hr = fd1->get_Name(&namex);
        hr = fd2->get_Name(&namey);

        cout<< "filends count: "<< fds->GetCount()<< endl;
        wcout<< L"<"<< namex<< L"><"<< namey<< L">"<< endl;
        VARIANT valx, valy;
        while(true)
        {
            hr = rds->get_EOF(&eof);
            hr = rds->get_BOF(&bof);
            if(eof)break;
            if(bof)break;
            fd1->get_Value(&valx);
            fd2->get_Value(&valy);
            wcout<< L"{"<< (wchar_t*)_bstr_t(_variant_t(valx))<< L"}{";
            wcout<< (wchar_t*)_bstr_t(_variant_t(valy))<< L"}"<< endl;

            hr = rds->MoveNext();
        }
        hr = rds->Close();
        hr = cnn->Close();

    }
	CoUninitialize();
	
	return 0;
}

//using DAO
#import "G:\Program Files\Common Files\Microsoft Shared\DAO\dao360.dll"
#include "Debug\dao360.tlh"
#include<atlbase.h>
#include<iostream>

using namespace std;

int main()
{
    CoInitialize(0);
    {
        HRESULT hr = -1;
        CComPtr<IUnknown> app;
        DAO::_DBEngine* eng;
        DAO::Workspace* wr;
        DAO::Database* db;
        DAO::Recordset* rs;
        VARIANT_BOOL eof;
        VARIANT_BOOL bof;
        DAO::Fields* fs;
        DAO::_Field* f1;
        DAO::_Field* f2;
        hr = app.CoCreateInstance(L"DAO.DBEngine.36");
        hr = app->QueryInterface(&eng);
        hr = eng->raw_CreateWorkspace(L"TestWorkSpace1", L"Admin", L"", _variant_t(DAO::dbUseJet), &wr);
        //open dbname, ifexclusive, ifreadonly, connect, retdb
        hr = wr->raw_OpenDatabase(_bstr_t(L"c:\\db1.mdb"), _variant_t(0), _variant_t(0), _variant_t(L""), &db);
        hr = db->raw_OpenRecordset(_bstr_t(L"select * from Table1"), 
            _variant_t(DAO::dbOpenDynaset), _variant_t(0), _variant_t(2), &rs);
        hr = rs->get_Fields(&fs);
        hr = fs->get_Item(_variant_t(0), &f1);
        hr = fs->get_Item(_variant_t(1), &f2);
        wcout<< L"<"<< (BSTR)f1->Name<< L"><" << f2->Name<< L">"<< endl;
        VARIANT valx, valy;
        while(true)
        {
            hr = rs->get_EOF(&eof);
            hr = rs->get_BOF(&bof);
            if(eof)break;
            if(bof)break;
            f1->get_Value(&valx);
            f2->get_Value(&valy);
            wcout<< L"{"<< (wchar_t*)_bstr_t(_variant_t(valx))<< L"}{";
            wcout<< (wchar_t*)_bstr_t(_variant_t(valy))<< L"}"<< endl;
            hr = rs->MoveNext();
        }

        hr = hr;
       
    }
    CoUninitialize();
	
	return 0;
}

Ion Filipski
1c.bmp
 
hi, it is very kind of u to write all the codes. but what i need is to retrieve the data and put it in the combo-box. maybe an Access file is not necessary, a .txt file also can. so the combo-box will display whatever in the .txt file, and user can choose whatever data in the combo-box.

the combo-box thing is my main concern here.

but anyway, still thanks a lot!! do u know what should i do to achieve the above in MFC?

Thanks in advance.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top