Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / ADO.NET / February 2005

Tip: Looking for answers? Try searching our database.

ADO.Net DateTime issue

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Paul Yanzick - 28 Feb 2005 16:06 GMT
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
Ignacio Machin \( .NET/ C# MVP \) - 28 Feb 2005 16:16 GMT
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,

Signature

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

> Hello,
>
[quoted text clipped - 34 lines]
> Thanks
> Paul
Paul Yanzick - 28 Feb 2005 16:35 GMT
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 )" <ignacio.machin AT dot.state.fl.us> wrote
> hi,
>
[quoted text clipped - 52 lines]
>> Thanks
>> Paul
Cor Ligthert - 28 Feb 2005 16:22 GMT
Paul,

This is typical for the SQLparameter.

Then the date when you supply it as DateTime will normally be done right.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlr
fsystemdatasqlclientsqlcommandclassparameterstopic.asp


On this page is the parameter better desctibed.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlr
fsystemdatasqlclientsqlparameterclasstopic.asp


I don't see where that dataadapter is for in your code

SQLCommand.ExecuteScalar

Should in my idea be enough.

I hope this helps?

Cor
Cor Ligthert - 28 Feb 2005 16:31 GMT
Paul,

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

Cor
Paul Yanzick - 28 Feb 2005 16:38 GMT
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
> Paul,
>
[quoted text clipped - 16 lines]
>
> Cor

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.