PDA

Click to See Complete Forum and Search --> : [2.0] Date Format


jsc0624
May 30th, 2007, 06:10 AM
I don't really understand why this code doesn't work?

dr.BeginEdit();
dr["ACSDate"] = hdnField.Value;
dr["FormNum"] = txtFormNumberNew.Text;
dr["ResponsiblePerson"] = txtResponsiblePersonNew.Text;
dr["SiteCodeID"] = GlobalVariable.g_sitecodeID;
dr.EndEdit();

da.Update(ds, "tblAsset");
ds.AcceptChanges();

The error says,

Incorrect date value: '5/30/2007 7:07:13 PM' for column 'ACSDate' at row 1

The value of hdnField.Value was taken in this code:

DateTime timeNow = DateTime.Now;
string myTime = timeNow.ToString("h:mm:ss tt");
string acsdate = ddlMonth.SelectedValue + "/" + ddlDays.SelectedValue + "/" + ddlYear.SelectedValue + " " + myTime;
hdnField.Value = acsdate;

What I am using is ASP.net using C# and my database is MySQL AB Database.

jmcilhinney
May 30th, 2007, 07:40 AM
Strings are not dates. If your column is supposed to contains dates then assign a DateTime object to it, not a string.

jsc0624
May 30th, 2007, 07:46 AM
DateTime myTime = timeNow.ToString("h:mm:ss tt");
DateTime acsdate = ddlMonth.SelectedValue + "/" + ddlDays.SelectedValue + "/" + ddlYear.SelectedValue + " " + myTime;
hdnField.Value = acsdate;

I tried to change it to DateTime but the the error message says:

Cannot implicitly convert type 'string' to 'System.DateTime'
Cannot implicitly convert type 'string' to 'System.DateTime'
Cannot implicitly convert type 'System.DateTime' to 'string'

That's why I tried to use String!

I need your help!

jmcilhinney
May 30th, 2007, 07:51 AM
If you have a date/time column in a database then you should be using DateTime objects in your VB code, NOT Strings. Do NOT create a string. Create a DateTime object. This code:ddlMonth.SelectedValue + "/" + ddlDays.SelectedValue + "/" + ddlYear.SelectedValue + " " + myTime;is creating a string. If you want to create a DateTime object then you use a constructor of the DateTime type.

jsc0624
May 30th, 2007, 07:55 AM
How can I convert it to DateTime?

I tried to removed this "/";

But then the error says that the ddlMonth contains a string, it cannot be implicitly converted to System.DateTime.

jmcilhinney
May 30th, 2007, 08:00 AM
I've told you that you need to use a DateTime constructor to create a DateTime object. Have you looked to see what constructors the DateTime structure has? I'm guessing not. I suggest you do so. Once you have then you'll know what values you need to provide to create a DateTime object. You then need to ask yourself what values you have and how you would go about converting from one to the other.