...

View Full Version : insert datetime into the database



aish
09-30-2005, 01:56 PM
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.

Cipher
10-01-2005, 12:15 AM
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 + "'"

aish
10-01-2005, 11:24 AM
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");

Cipher
10-01-2005, 01:38 PM
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.

aish
10-04-2005, 08:23 AM
yes, it works thanks, can't we equal this null value to variable? otherwise I have to call this stored procedure twice.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum