0

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.

3
Contributors
4
Replies
6
Views
12 Years
Discussion Span
Last Post by plazmo
0

yes my table has only one feild,because this is test code 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");

Edited by pyTony: fixed formatting

0

try not parseing the textbox as a DateTime

because the value should be entered for a date in a db like this:
command.CommandText= "INSERT INTO Table2 VALUES('12/20/2005')"

note the single quote around the date.
also parseing it to datetime will not work because its not the same format

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.