Passing parameter for Stored Procedure to Data Adapter/Data Set

G

Guest

Hey all,

I'm trying to pass a parameter into my Stored procedure to be accessed by
my data adapter into a dataset

db2.AddParameter("@intClientID",
cmbClientList.SelectedValue.ToString());
DataSet rsDataset = db2.Fill("spListMappingDetails",
CommandType.StoredProcedure);

However, when i try to run it, it gives me an error

{"Procedure 'spListMappingDetails' expects parameter '@intClientId', which
was not supplied."}

Any help is appreciated.

I used a executescalar and executequery function and it has no problem.

thanks in advance
 
W

William \(Bill\) Vaughn

While your code creates the parameter, it does not set the Value property.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
INETA Speaker
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
G

Guest

This cmbClientList.SelectedValue.ToString() is suppose to be my value, how
else should i set the value?

Thanks
 
W

William \(Bill\) Vaughn

Sorry, I blinked and missed it.
The Fill method is executed off the DataAdapter--not the Command. It expects
a SelectCommand to be populated with the CommandText that has the SQL SELECT
query and the SelectCommand.Parameters collection to be populated with
Parameters. Let's see the real code.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
INETA Speaker
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
G

Guest

DataSet d = new DataSet("DataSet");
IDataAdapter apt = IDataAdapter GetAdapter(<SqlStatement>,
CommandType.StoredProcedure );

apt.Fill(d);
return d;
 
W

William \(Bill\) Vaughn

I don't know why you're using this approach. I also don't see a GetAdapter
in the Framework object browser so it must be in the DAB or in some custom
libary. I can offer help on more conventional means, but not on this...

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
INETA Speaker
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
P

piggy

Hi,

First:
I do not believe that parameter names are case insensitive. Did you try
adding parameter as '@intClientId' instead of '@intClientID'

db2.AddParameter("@intClientId",cmbClientList.SelectedValue.ToString());

Second:
to be sure the cmbClientList has a selected value, try using a string
variable to get the cmbClientList.SelectedValue.ToString and (after) use
the string variable as argument for AddParameter:

mystring = cmbClientList.SelectedValue.ToString()
db2.AddParameter("@intClientId",mystring);

hope this will help
 
G

Guest

Just curious, how different is it to pass a parameter to a data adapter as
compared to a Recordset (i.e data reader)
 
W

William \(Bill\) Vaughn

I don't know what planet you're getting your examples, but you can't pass a
parameter to a DataAdapter--only to the Command objects it manages. There is
no Recordset in the SqlClient namespace and the DataReader is a returned
data stream, not a way to execute SQL--that can only be done with a Command.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
INETA Speaker
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
G

Guest

Thanks. That cleared things out for me. I'm new to all this, just got
confused. Will try to work it out.
 
G

Guest

Basically this is what i am working with:-

protected internal abstract IDbDataParameter GetParameter(string name,
object val);

public virtual void AddParameter(string name, object val)
{
if (Command == null) { ResetCommand(); }
if (val == null) { val = DBNull.Value; }
Command.Parameters.Add(GetParameter(name, val));
}

protected abstract IDataAdapter GetAdapter(string sql, CommandType type);

public virtual DataSet Fill(string sql, CommandType type)
{
DataSet d = new DataSet("DataSet");
IDataAdapter apt = GetAdapter(sql, type);
apt.Fill(d);
return d;
}


dbConnection.AddParameter("@<Parameter>", "<Value>");
DataSet rsDataset = dbConnection.Fill("<StoredProcedure>",
CommandType.StoredProcedure);
 

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