insert datetime into the database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aish
    New Member
    • Sep 2005
    • 5

    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.Te xt).Length==0)
    {
    x=SqlDateTime.N ull;
    }
    else
    {
    x=DateTime.Pars e(TextBox1.Text );
    }
    command=sqlConn ection.CreateCo mmand();
    command.Command Text="insert into Table2 values('"+x+"') ";
    sqlConnection.O pen();
    command.Execute NonQuery();
    Response.Write( "Save");

    when I add this type of dates ex.25/02/2005 it occur error, how can I solve this.
  • aish
    New Member
    • Sep 2005
    • 5

    #2
    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 as "01/01/1900" but I want to insert "<Null>" How can I do this?

    string Mydate;

    if(cboDateofBir th_Day.Selected Index!=0 && cboDateofBirth_ Month.SelectedI ndex!=0 && cboDateofBirth_ Year.SelectedIn dex!=0)
    {
    Mydate=cboDateo fBirth_Day.Sele ctedValue+"/"+cboDateofBirt h_Month.Selecte dValue+"/"+cboDateofBirt h_Year.Selected Value;

    }
    else
    {
    Mydate=DBNull.V alue.ToString() ;

    }
    command=sqlConn ection.CreateCo mmand();
    command.Command Text="set dateformat dmy;exec p_Insert_Test '"+Mydate+"' ";
    sqlConnection.O pen();
    command.Execute NonQuery();
    Response.Write( "Save");

    Comment

    Working...