?
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
Medium Priority
?
494 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 6

Accepted Solution

by:
nabehs earned 1000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
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…

718 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