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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…

756 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