Results 1 to 6 of 6

Thread: [2.0] Date Format

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2007
    Posts
    240

    [2.0] Date Format

    I don't really understand why this code doesn't work?

    Code:
    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:

    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.
    Last edited by jsc0624; May 30th, 2007 at 06:48 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] Date Format

    Strings are not dates. If your column is supposed to contains dates then assign a DateTime object to it, not a string.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2007
    Posts
    240

    Re: [2.0] Date Format

    Code:
    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!

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] Date Format

    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:
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Mar 2007
    Posts
    240

    Re: [2.0] Date Format

    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.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] Date Format

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width