Solved

Data from MS SQL table to MFC Combobox(Drop list) mode via OLE DB (Compiler: MS Visual C++ 6)

Posted on 2004-08-25
3
412 Views
Last Modified: 2013-11-25
I am trying to get data from a table on a SQL server ( Departments ) and put into a ComboBox using a OLE DB connection set up by a ATL object. I have by now not been able to find any help anywhere.
The ComboBox is being used in a Dialogbased MFC Application, i have added a Document object decending from CDocument to save the data into.
I really hope somebody can help me out..

Thanks  
0
Comment
Question by:syska
3 Comments
 
LVL 6

Accepted Solution

by:
nabehs earned 250 total points
ID: 11900093
1. In Class View, Right Click, New Class. From the Dialog Combo, select Generic Class, Enter the name CDepartments and press OK. A new class is created with departments.cpp and departments.h files
2. Open departments.h file
3. Assume that you have the table Departments with the following fields
department_id : varchar(10)
department_name : varchar(100)
4. Modify the class CDepartments in departments.h to the following
class CDepartments
{
public:
    CDepartments();
    virtual ~CDepartments();

    CHAR m_szDeptId[11]; // always character array size + 1. So you have varchar(10): you will need CHAR [11]
    CHAR m_szDeptName[101];

BEGIN_COLUMN_MAP(CDepartments)
    COLUMN_ENTRY(1, m_szDeptId)
    COLUMN_ENTRY(2, m_szDeptName)
END_COLUMN_MAP( )
};

5. In stdafx.h add the following line:
#include <atldbcli.h>

6. Add the following function to fill the combo with the departments:

#include "departments.h"
void FillComboWithDepartments(CComboBox& combo);

void FillComboWithDepartments(CComboBox& combo)
{
    CoInitialize(NULL);

    CCommand<CAccessor<CDepartments> > rs;
    CString strConnectionString = "Provider=SQLOLEDB.1;Initial Catalog=MyDatabase;Data Source=MyServer;User Id=sa;Password=;";
    // Replace MyDatabase with your database name
    // Replace MyServer with the server name where SQL Server is installed
    // Replace sa with the user id
    // Replace the blank password with the correct password

    CDataSource ds;
    CSession session;
    HRESULT hr;

    // without error checking
    hr = ds.OpenFromInitializationString(strConnectionString.AllocSysString( ));
    hr = session.Open(ds);
    hr = rs.Open(session, "SELECT * FROM Departments ORDER BY department_name");
    while(rs.MoveNext( ) == S_OK)
    {
        combo.AddString(rs.m_szDeptName);
    }
    rs.Close( );
    session.Close( );
    ds.Close( );

    CoUninitialize( );
}

7. Call this function and pass the combobox reference


0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now