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
443 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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maxBlock challenge 30 129
only14 challenge 19 76
java ^ examples 8 66
withoutTen challenge 14 122
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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…

777 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