How do i insert a value to a DateTime datafield in SQL database?

Last post 06-27-2009 1:16 AM by chintanpshah. 13 replies.

Sort Posts:

  • How do i insert a value to a DateTime datafield in SQL database?

    06-25-2009, 9:48 AM
    • Member
      20 point Member
    • Poku
    • Member since 03-20-2009, 9:00 AM
    • Posts 62

    How do i insert a value to a DateTime datafield in SQL database?

    I use this code:

    protected void InsertButton_Click(object sender, EventArgs e)
            {
                string msg = MsgTextArea.Value;
                DateTime start = Convert.ToDateTime(StartTextBox.Text);
                DateTime end = Convert.ToDateTime(StartTextBox.Text);
    
                db.insertSpecialClosing(msg, start, end);
            }
     
    public void insertSpecialClosing(string msg, DateTime start, DateTime end)
            {
                query("INSERT INTO SpecialClosing (Text, SpecialClosingStart, SpecialClosingEnd) VALUES('" + msg + "', '"+start+"', '"+end+"')");
            }

    And i get this error: 

    +  $exception {"The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.\r\nThe statement has been terminated."} System.Exception {System.Data.SqlClient.SqlException}

    What am i doing wrong?

  • Re: How do i insert a value to a DateTime datafield in SQL database?

    06-25-2009, 10:37 AM
    • Member
      518 point Member
    • arry.net
    • Member since 07-04-2008, 9:42 AM
    • Posts 136

    Try Removing  "' "         singl quote from msg



    arry.net
  • Re: How do i insert a value to a DateTime datafield in SQL database?

    06-25-2009, 10:37 AM
    • Star
      10,720 point Star
    • chintanpshah
    • Member since 11-19-2008, 12:39 AM
    • Ahmedabad
    • Posts 1,905

    Try to call like this:

    db.insertSpecialClosing(msg, start.Date, end.Date);

    Hope this helps...

    Don't forget to mark as answer, if it helps
  • Re: How do i insert a value to a DateTime datafield in SQL database?

    06-25-2009, 6:47 PM
    • Member
      20 point Member
    • Poku
    • Member since 03-20-2009, 9:00 AM
    • Posts 62

     Thank you for your replies, unfortunaly none of them works. Do you have any other suggentions?

  • Re: How do i insert a value to a DateTime datafield in SQL database?

    06-25-2009, 6:56 PM
    Answer
    • Star
      9,214 point Star
    • hans_v
    • Member since 01-29-2007, 9:03 PM
    • Posts 1,594

     User Paremeterized Queries.

    http://aspnet101.com/aspnet101/tutorials.aspx?id=1

    It will not only solve your problem, but it is also a defense against SQL injections...

  • Re: How do i insert a value to a DateTime datafield in SQL database?

    06-25-2009, 7:00 PM
    • Contributor
      5,624 point Contributor
    • RatheeshC
    • Member since 04-25-2008, 6:05 PM
    • Posts 1,198

    Hi,

     Try replaceing start by start.ToShortDateString() and End by end.ToShortDateString() in the query

    Thanks

    Thanks
    Ratheesh

    Please mark it as answer if it resolves your issue.
  • Re: How do i insert a value to a DateTime datafield in SQL database?

    06-25-2009, 9:59 PM
    • Participant
      1,398 point Participant
    • Sayre
    • Member since 07-24-2008, 5:03 AM
    • Manila, Philippines
    • Posts 349

    Hi,

    try this 

    FormatDateTime(txtdate.text,DateFormat.ShortDate)

    Hope This One Can Help.
    Thanks

    To Be Happy Is To Be YourSelf

    codeincsharp.blogspot.com
  • Re: How do i insert a value to a DateTime datafield in SQL database?

    06-26-2009, 4:29 AM
    • Star
      10,720 point Star
    • chintanpshah
    • Member since 11-19-2008, 12:39 AM
    • Ahmedabad
    • Posts 1,905
    ("INSERT INTO SpecialClosing (Text, SpecialClosingStart, SpecialClosingEnd) VALUES('" + msg + "', '"+start.ToString("MM/dd/yyyy")+"', '"+end.ToString("MM/dd/yyyy")+"')");   


    Use Query like this. 

    Hope this helps...

    Don't forget to mark as answer, if it helps
  • Re: How do i insert a value to a DateTime datafield in SQL database?

    06-26-2009, 9:41 AM
    • Member
      20 point Member
    • Poku
    • Member since 03-20-2009, 9:00 AM
    • Posts 62

     I tried all of your suggentions and they work as far of putting data into the database, but it doesn't insert the correct date. For example if a put in the date 24/06/2009 i get this date in the database: 29-06-1894 00:00:00. Why is that?

  • Re: How do i insert a value to a DateTime datafield in SQL database?

    06-26-2009, 10:17 AM
    • Contributor
      5,624 point Contributor
    • RatheeshC
    • Member since 04-25-2008, 6:05 PM
    • Posts 1,198

    Hi,

    Before inserting data into DB, try to call this

    set dateformat 'dmy'.

    in DB

    This will set the dateformat in your DB and then call the insert statement.

    Thanks

     

     

    Thanks
    Ratheesh

    Please mark it as answer if it resolves your issue.
  • Re: How do i insert a value to a DateTime datafield in SQL database?

    06-26-2009, 10:41 AM
    • Star
      9,214 point Star
    • hans_v
    • Member since 01-29-2007, 9:03 PM
    • Posts 1,594

    Poku:
    I tried all of your suggentions and they work as far of putting data into the database
     

    Did you try a parameter query also?

  • Re: How do i insert a value to a DateTime datafield in SQL database?

    06-26-2009, 9:49 PM
    • Participant
      1,398 point Participant
    • Sayre
    • Member since 07-24-2008, 5:03 AM
    • Manila, Philippines
    • Posts 349
    Hope This One Can Help.
    Thanks

    To Be Happy Is To Be YourSelf

    codeincsharp.blogspot.com
  • Re: How do i insert a value to a DateTime datafield in SQL database?

    06-27-2009, 1:13 AM
    Answer
    • Star
      10,720 point Star
    • chintanpshah
    • Member since 11-19-2008, 12:39 AM
    • Ahmedabad
    • Posts 1,905

    Try to use Parameterized query as suggested by someone. It will handle the date formats for you.

    Like:

    string strQuery;

    SqlCommand cmd;

    strQuery = "insert into customers (CustomerID, CompanyName) values(@CustomerID, @CompanyName)";

    cmd = new SqlCommand(strQuery);

    cmd.Parameters.AddWithValue("@CustomerID", "A234");

    cmd.Parameters.AddWithValue("@CompanyName", "DCB");

    InsertUpdateData(cmd);


    Refer to:

    http://aspnet101.com/aspnet101/tutorials.aspx?id=1

    http://www.aspsnippets.com/post/Parameterized-Queries-ADONet.aspx

    http://www.4guysfromrolla.com/webtech/092601-1.shtml

     

    Hope this helps...

    Don't forget to mark as answer, if it helps
  • Re: How do i insert a value to a DateTime datafield in SQL database?

    06-27-2009, 1:16 AM
    • Star
      10,720 point Star
    • chintanpshah
    • Member since 11-19-2008, 12:39 AM
    • Ahmedabad
    • Posts 1,905
Page 1 of 1 (14 items)