|
|
I have datetime field in the table in my database. How to assign that datetime in my local variable ?
i tried
DateTime createdDate;
createdDate = Convert.ToDateTime(result.GetString(1));
but didn't work.
suchita
|
|
|
|
|
What's the value in result.GetString(1)? Try pausing at this line and then see what the data is that's in there.
|
|
|
|
|
its the datetime field.
value is 2010-09-30 11:14:29.783
thanks
suchita
|
|
|
|
|
It sounds to me like there's a culture problem here. In other words, the format on the system doesn't match the format of data in the database.
|
|
|
|
|
yah. I have datetime field in the database. When i am retrieving using datareader, it is taking as string. I think there where the problem is? how to convert that data from the database in to the string value ?
suchita
|
|
|
|
|
|
System.InvalidCastException: Unable to cast object of type 'System.DateTime' to type 'System.String'.
this one is showing as error.
i declare the variable as datetime and string as well but it gave the same error in both. it seems since my datafield is datetime, it is having problem when i am extracting that datetime.
suchita
|
|
|
|
|
SayamiSuchi wrote: but didn't work.
In what way did it not work?
|
|
|
|
|
Assuming that result is a data reader or an sql parameter:
DateTime dateTime = (DateTime)dataReader["columnName"]
or
DateTime dateTime = (DateTime)sqlParameter.Value;
Are the most correct answers. If the field allows null, check for DbNull.Value first.
|
|
|
|
|
ya i already tried this before but it gave the same error.
suchita
|
|
|
|
|
Do not use strings for date/times if you can avoid it, as it opens up a whole range of potential problems. Instead have a datetime field in the database, and a DateTime variable in your code; then do what Ennis said.
If you must use strings, make sure to use a universal format. More is here[^]
|
|
|
|
|
I dont think anyone of you understood my problem. Let me explain again.
I have a table as
CREATE TABLE [dbo].[tblTest](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Date] [datetime] NULL )
Now I am retrieving that value :
DateTime createdDate;
SqlCommand command = new SqlCommand("SELECT ID,Date FROM tblTest WHERE ID = '1'", myConnection);
SqlDataReader result = command.ExecuteReader();
if (result.Read())
{
createdDate = result.GetString(1);
}
when i did this , it gave error.
System.InvalidCastException: Unable to cast object of type 'System.DateTime' to type 'System.String'.
so i declared createdDate as string and tried again. It gave the same error. So I thought its the problem when we are retrieving datetime field from the database. I tried this query in query analyzer, and it is working fine.
I even tried this:
createdDate =(DateTime)(result.GetString(1)); but same error.
suchita
|
|
|
|
|
Damn - I really should read these things through first. Basically, you are trying to read a datetime out as a string here. The code should read
if (!result.IsDBNull(1))
createdDate = result.GetDateTime(1);
|
|
|
|
|
thanks. it worked.
suchita
|
|
|
|
|
|
1: show this result(result.GetString(1));
2: the result fit to time of rule ;
3: String strTime = Convert.ToDateTime(the result fit to time of rule).ToString("yyyy-MM-dd")
4: DateTime dt = new DateTime();
dt = Convert.ToDateTime(the result);
|
|
|
|
|
Try:
DateTime.TryParseExact(....);
See MSDN for details.
V.
|
|
|
|
|
Hi all! I'm writing an Interop wrapper and am wrapping a method that takes an array of objects as a parameter.
void WrappedMethod(object[,] values);
void MyWrapper<T>(T[,] values)
{
WrappedMethod(values);
}
I thought all ref types were also objects, so why can't I cast T[,] to object[,] ?
Thank you all for you time
Matt
modified on Monday, October 25, 2010 4:17 PM
|
|
|
|
|
Do this:
private void WrappedMethod(object[,] values)
{
}
private void MyWrapper<T>(T[,] values) where T:class
{
WrappedMethod(values);
}
|
|
|
|
|
Nice, Nish. +5
So this has something to do with the inferrable type. Why does adding the T:class modifier help...said another way, what are the non-convertable types that it's griping about?
Cheers.
|
|
|
|
|
TheyCallMeMrJames wrote: Why does adding the T:class modifier help...said another way, what are the non-convertable types that it's griping about?
Value types. The compiler can't know in advance that T won't be a value type.
|
|
|
|
|
|
Awesome! Works perfectly! I was trying where T : Object but was getting a compiler error.
Thank you!
|
|
|
|
|
Hey All,
I have a problem with dynamically loading and referenced assembly at run time. The issue is that i am coding a 3rd party app for a larger application that is constantly getting patched and upgraded. Every time a new patch or upgrade comes out i need to recompile and redistribute the app too all the clients. Now nothing really ever changes in the referenced assembly aside from version number which is causing me to have to remove the reference and add it again. I don't even have to change code, just update the reference.
I know i can load this specific assembly when the reference isn’t found by attaching to the AssemblyResolve event; however this is where I’m having problems. I’m not exactly sure how i should implement this. All the documentation i can find from MS isn’t very helpful, as per usual.
Really the end goal is to have it load this referenced assembly when it is called at run time. The referenced assemblies path will change from machine to machine so it needs to fully dynamic, i can work out finding the proper assembly to load i just need some insight on the best way to implement this.
Also another quick question ... if i reference say v1.2 and then dynamically load v3.5 is the framework going to freak out about the version numbers or just accept that this is the assembly i want to load.
TIA
Don't comment your code - it was hard to write, it should be hard to read!
|
|
|
|