Click here to Skip to main content
15,923,087 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
my code is below: but i am getting error:"Failed to convert parameter value from a SqlParameter to DateTime".

Any suggestion plz will be appreciated.

DataTable dt = new DataTable();
            string spName = "spResult1";
            List<sqlparameter> paramlist=new List<sqlparameter>();
            paramlist.Add(new SqlParameter("@fromDate",obj.dateFrom));
            paramlist.Add(new SqlParameter("@toDate",obj.dateTo));

            DataSet ds = SqlHelper.ExecuteDataset(objconn._connectionString, spName, paramlist.ToArray());
            dt = ds.Tables[0];

        catch(Exception ex)
        return dt;</sqlparameter></sqlparameter>
Updated 11-May-12 4:34am
Maciej Los 11-May-12 10:45am    
You need to provide more details... like the body of stored procedure and the line in which error occurs.

There are two issues with your question;
1. obj.dateFrom and obj.dateTo, what do these return, a string or a DateTime object, if it returns a string the format can cause problems depending on the format dd/MM/yyyy vs MM/dd/yyyy you would be better off returning a DateTime instead of a string.
2. The SqlHelper class, how does this process DateTime objects?

In short when dealing with DateTime values either use an actual DateTime object as the parameter or if using a string use the dd MMM yyyy or yyyyMMdd formats so that you do not have to deal with ambiguous formats.
Share this answer
khosnur 11-May-12 10:29am    
thanx for ur obj contains DateTime.i debug it what i am passing same datetime format i used in the stored procedure it returns the results.but when i run the program and inserting the date it throwing that Exception
If you have a look at the error, it says "Failed to convert parameter value from a SqlParameter to DateTime"

So somewhere you try to set a SqlParameter to a DateTime variable, not necessarily in the code you've posted.

If you debug, at what line the exception is thrown (remember to step into SqlHelper.ExecuteDataset when debugging)?
Share this answer
khosnur 11-May-12 13:09pm    
Thank u very much to reply..Mika.
Wendelius 11-May-12 14:00pm    
You're welcome :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900