ADO.Net DateTime issue

P

Paul Yanzick

Hello,

I'm having some issues pulling a datetime object from a database and am
looking for any suggestions I can get.

I have written a function to make querying specific fields in a table as
generic as possible. The function is below:

private string GetFieldValue(string IDKey, string FieldName)

{

SqlConnection Connection = new
SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);

SqlCommand Command=new SqlCommand("Select " + FieldName + " from
issueescalation where IDKey='" + IDKey + "'",Connection);

SqlDataAdapter Adapter=new SqlDataAdapter(Command);

Connection.Open();

string RetValue=(string)Adapter.SelectCommand.ExecuteScalar();

Connection.Close();

return RetValue;

}

The problem is when I query a datetime field. I keep getting an error
message saying the specified cast is invalid. The date field is populated,
and I am able to modify the SQL command and pull components of the datetime
such as the year, but keep getting the same error. Any suggestions? If you
need more info, please let me know.

Thanks
Paul
 
I

Ignacio Machin \( .NET/ C# MVP \)

hi,

the code you present is no generic at all, you have hardcoded both the
table name as well as the where clause

You do not especify it, but I think that you are getting this error in the
return statement

string RetValue=(string)Adapter.SelectCommand.ExecuteScalar();

It's because of the cast, there is no implicit cast of a DateTime to string
change it to this:

string RetValue= Adapter.SelectCommand.ExecuteScalar().ToString();


cheers,
 
C

Cor Ligthert

C

Cor Ligthert

Paul,

I forgot, and you need probably the Convert.ToString or DateTime.Parse when
the value you get is a datetime.

Cor
 
P

Paul Yanzick

Hello Ignacio,

You are correct, it isn't a totally generic function; actually it is generic
for this table... When the function is called, I specify the field I want
and the IDkey of the record that I receive via another method... at least
that is the plan.

Thanks for your help, that worked! Using (string) in front of the other
values work for the other items, why does it not work for DateTime?

Thanks
Paul


Ignacio Machin ( .NET/ C# MVP ) said:
hi,

the code you present is no generic at all, you have hardcoded both the
table name as well as the where clause

You do not especify it, but I think that you are getting this error in the
return statement

string RetValue=(string)Adapter.SelectCommand.ExecuteScalar();

It's because of the cast, there is no implicit cast of a DateTime to
string
change it to this:

string RetValue= Adapter.SelectCommand.ExecuteScalar().ToString();


cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



Paul Yanzick said:
Hello,

I'm having some issues pulling a datetime object from a database and am
looking for any suggestions I can get.

I have written a function to make querying specific fields in a table as
generic as possible. The function is below:

private string GetFieldValue(string IDKey, string FieldName)

{

SqlConnection Connection = new
SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);

SqlCommand Command=new SqlCommand("Select " + FieldName + " from
issueescalation where IDKey='" + IDKey + "'",Connection);

SqlDataAdapter Adapter=new SqlDataAdapter(Command);

Connection.Open();

string RetValue=(string)Adapter.SelectCommand.ExecuteScalar();

Connection.Close();

return RetValue;

}

The problem is when I query a datetime field. I keep getting an error
message saying the specified cast is invalid. The date field is
populated, and I am able to modify the SQL command and pull components of
the datetime such as the year, but keep getting the same error. Any
suggestions? If you need more info, please let me know.

Thanks
Paul
 
P

Paul Yanzick

Cor,

I had added the SqlDataAdapter as something to try in resolving this
issue... It had actually resolved a problem I had when trying to get an
identity after writing to the table.

It seems that Command.ExecuteScalar().ToString() worked for me. I
appreciate the links and will look through thouse.

Thanks
Paul
 

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