Avatar of mugsey
mugseyFlag for United Kingdom of Great Britain and Northern Ireland

asked on 

problem with dataadapter.fill method

for some reason I get the error saying that the stored procedure is expecting a parameter that was not supplied.  Even though the parameter is being passed in OK

The code is in the code snippet window

It fails on the

dataAdapter.Fill(dataTable);

line

Even though it is getting passed through OK on this code

foreach (SqlParameter param in reportParameters)
        {
            DbParameter dbparam = dbCommand.CreateParameter();
            dbparam.Direction = ParameterDirection.Input;            
            dbparam.Value = param.Value;            
            dbCommand.Parameters.Add(dbparam);
        }

Any comments appreciated

public static DataTable GetReportDataTable(string strSprocName, List<SqlParameter> reportParameters)
	{
        string connString = ConfigurationManager.ConnectionStrings["test"].ConnectionString;
        SqlConnection sqlConnection = new SqlConnection(connString);
        DbProviderFactory pv = DbProviderFactories.GetFactory("System.Data.SqlClient");
 
        DbCommand dbCommand = sqlConnection.CreateCommand();
        dbCommand.CommandText = strSprocName;
        dbCommand.CommandType = CommandType.StoredProcedure;
        dbCommand.CommandTimeout = 6000;        
 
        foreach (SqlParameter param in reportParameters)
        {
            DbParameter dbparam = dbCommand.CreateParameter();
            dbparam.Direction = ParameterDirection.Input;            
            dbparam.Value = param.Value;            
            dbCommand.Parameters.Add(dbparam);
        }
 
        DbDataAdapter dataAdapter = pv.CreateDataAdapter();
        dataAdapter.SelectCommand = dbCommand;
 
        DataTable dataTable = new DataTable();
        dataAdapter.Fill(dataTable);
        return dataTable;      
	}

Open in new window

.NET ProgrammingEditors IDEs

Avatar of undefined
Last Comment
mugsey
Avatar of Faheem Shaikh
Faheem Shaikh
Flag of India image

Check the number of parameters the stored procedure is expecting and how many are actually being passed through your foreach code. The code will fail on fill function and not while you are adding the parameters if the number of parameters passed is less. Do a simple match on the number of parameters expected and supplied.
Avatar of mugsey
mugsey
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

hi

There is only one parameter expected on the sproc.  Hmm, I using reporting services and creating a strongly typed dataset to design the report.  
ASKER CERTIFIED SOLUTION
Avatar of Faheem Shaikh
Faheem Shaikh
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of mugsey
mugsey
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Thanks

I put a watch on

dataAdapter.SelectCommand = dbCommand;

and dbCommand states that the parameter name is "Parameter1"  now the proper parameter name is @id

Avatar of mugsey
mugsey
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

DOHH!!

Sorted it

I forgot to add

dbparam.ParameterName = param.ParameterName;

Grrr!!  :-)
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo