devnew
devnew

Reputation: 13

Getting this error: Unable to cast object of type 'System.DateTime' to type 'Oracle.ManagedDataAccess.Client.OracleParameter'

I am trying to pass a select query into the oracle database and used the following code for passing as parameters

string startdate = "07/01/2021";
DateTime sd = DateTime.ParseExact(startdate, "d", provider);
OracleParameter param = new OracleParameter();
param.ParameterName = "startdate";
param.OracleDbType = OracleDbType.Date;
param.Value = sd;
cmd.Parameters.Add(param);

and the above code runs fine and gives me the required correct output but I wanted to make it shorter so used the following line of code to replace it with

cmd.Parameters.Add(new OracleParameter("startdate", OracleDbType.Date).Value = sd);                    

and I got this error Unable to cast object of type 'System.DateTime' to type 'Oracle.ManagedDataAccess.Client.OracleParameter'. What's the reason why I can't use the second format?

Upvotes: 1

Views: 1017

Answers (1)

Serge
Serge

Reputation: 43870

you are using the wrong syntax. Try this

cmd.Parameters.Add("startdate", OracleDbType.Date).Value = sd;    

Upvotes: 1

Related Questions