DataSet basics - How to fill from stored proc?

D

deko

I have a number of ComboBoxes that I need to change the DataSource of based
on user selection. I'm new to working with ADO, so I'm sure this is a very
basic question...

I want to pass in a string to switch on, and create a DataSet from a stored
procedure, like so:

private void GetCboData(string strCbo)
{
string strStoredProc = "";
OleDbConnection cnxOle = new OleDbConnection();
cnxOle.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; " +
"Data Source=" + Application.StartupPath +
@"\myAccessDb.mdb";
switch (strCbo)
{
case "Customer":
strStoredProc = "someStoredProc";
break;
case "Something Else":
strStoredProc = "someOtherStoredProc";
break;
case "Another Thing"
strStoredProc = "anotherStoredProc";
break;
}
OleDbCommand cmdOle = new OleDbCommand(strStoredProc, cnxOle);
OleDbDataAdapter daOle = new OleDbDataAdapter(cmdOle);
DataSet ds = new DataSet();
daOle.Fill(ds); //<<==== ??????? throws exception
this.cboProductColor.DataSource = ds;
}

How do I fill the DataSet in this scenario?

Thanks in advance.
 
G

Guest

Hi deko,

One more line code is needed:

OleDbCommand cmdOle = new OleDbCommand(strStoredProc, cnxOle);

// add following code
cmdOle.CommandType=CommandType.StoredProcedure;

HTH

Elton Wang
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top