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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Using MS Code on my Mac 6 78
How do ASP.NET and MVC work together? 4 76
What namespace do I need to import? 2 52
Infrastructure development vs software development 3 107
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
In this post we will learn different types of Android Layout and some basics of an Android App.
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…

739 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