Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    insert datetime into the database

    I want to insert datetime into the database.I want to insert date as a dd/MMMM/yyyy format

    SqlDateTime x;
    if((TextBox1.Text).Length==0)
    {
    x=SqlDateTime.Null;
    }
    else
    {
    x=DateTime.Parse(TextBox1.Text);
    }
    command=sqlConnection.CreateCommand();
    command.CommandText="insert into Table2 values('"+x+"')";
    sqlConnection.Open();
    command.ExecuteNonQuery();
    Response.Write("Save");

    when I add this type of dates ex.25/02/2005 it occur error, how can I solve this.

  • #2
    Regular Coder Cipher's Avatar
    Join Date
    Dec 2004
    Posts
    123
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up

    Well the way i use with date is to save the date in string variable then insert it into the database as string also.

    Dim UserDate as String = TextBox1.Text
    "insert into Table2 Value '" + UserDate + "'"
    Last edited by Cipher; 10-05-2005 at 03:17 PM.

  • #3
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I change my code like this, now it works fine but I have small problem.
    if my dropdowns SelectedIndex==0 then I wants to insert null value. I used for DBNull.Value, but it save like this "01/01/1900" but I want to insert "<Null>" How can I do this?

    string Mydate;

    if(cboDateofBirth_Day.SelectedIndex!=0 && cboDateofBirth_Month.SelectedIndex!=0 && cboDateofBirth_Year.SelectedIndex!=0)
    {
    Mydate=cboDateofBirth_Day.SelectedValue+"/"+cboDateofBirth_Month.SelectedValue+"/"+cboDateofBirth_Year.SelectedValue;

    }
    else
    {
    Mydate=DBNull.Value.ToString();

    }
    command=sqlConnection.CreateCommand();
    command.CommandText="set dateformat dmy;exec p_Insert_Test '"+Mydate+"' ";
    sqlConnection.Open();
    command.ExecuteNonQuery();
    Response.Write("Save");

  • #4
    Regular Coder Cipher's Avatar
    Join Date
    Dec 2004
    Posts
    123
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You can make it this way and i think it will work

    string Mydate;
    string sqlCommand

    if(cboDateofBirth_Day.SelectedIndex!=0 && cboDateofBirth_Month.SelectedIndex!=0 && cboDateofBirth_Year.SelectedIndex!=0)
    {
    Mydate=cboDateofBirth_Day.SelectedValue+"/"+cboDateofBirth_Month.SelectedValue+"/"+cboDateofBirth_Year.SelectedValue;

    sqlCommand = "set dateformat dmy;exec p_Insert_Test '"+Mydate+"' ";
    }
    else
    {

    sqlCommand = "set dateformat dmy;exec p_Insert_Test NULL ";

    }
    command=sqlConnection.CreateCommand();
    command.CommandText=sqlCommand
    sqlConnection.Open();
    command.ExecuteNonQuery();
    Response.Write("Save");

    i do it my self to save null values in this way and it works.

  • #5
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yes, it works thanks, can't we equal this null value to variable? otherwise I have to call this stored procedure twice.


  •  

    Posting Permissions

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