Go Back   CodingForums.com > :: Server side development > ASP.NET

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-30-2005, 01:56 PM   PM User | #1
aish
New to the CF scene

 
Join Date: Sep 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
aish is an unknown quantity at this point
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.
aish is offline   Reply With Quote
Old 10-01-2005, 12:15 AM   PM User | #2
Cipher
Regular Coder

 
Cipher's Avatar
 
Join Date: Dec 2004
Posts: 123
Thanks: 0
Thanked 0 Times in 0 Posts
Cipher is an unknown quantity at this point
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 02:17 PM..
Cipher is offline   Reply With Quote
Old 10-01-2005, 11:24 AM   PM User | #3
aish
New to the CF scene

 
Join Date: Sep 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
aish is an unknown quantity at this point
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");
aish is offline   Reply With Quote
Old 10-01-2005, 01:38 PM   PM User | #4
Cipher
Regular Coder

 
Cipher's Avatar
 
Join Date: Dec 2004
Posts: 123
Thanks: 0
Thanked 0 Times in 0 Posts
Cipher is an unknown quantity at this point
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.
Cipher is offline   Reply With Quote
Old 10-04-2005, 08:23 AM   PM User | #5
aish
New to the CF scene

 
Join Date: Sep 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
aish is an unknown quantity at this point
yes, it works thanks, can't we equal this null value to variable? otherwise I have to call this stored procedure twice.
aish is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:06 PM.


Advertisement
Log in to turn off these ads.