Showing posts with label dd-mm-yyyy. Show all posts
Showing posts with label dd-mm-yyyy. Show all posts

Friday, February 24, 2012

Insert querry

Hello

Can anyone give me the code to insert date from textbox to database(SQL Server 2000). The date fromat in SQL is dd-mm-yyyy.

Rathish

rathish,

here is some code if you wish to take the code behind approach.

string yourDate;yourDate = txtBox.Text;string insertStr ="insert into yourTable values ('" + yourDate +"')";SqlConnection conn =new SqlConnection(connectionStr);SqlCommand cmd =new SqlCommand(insertStr, conn);using(conn){using(cmd){ conn.Open(); cmd.ExecuteNonQuery(); }}you can try something like that. hope it helps! -- jp
|||

Hello

Its giving me the following error:

Syntax error convertig datetime from character string.

The datatype is datetime in the database.

Rathish

|||

Hi,

I would suggest you use parameters update database

string yourDate;
yourDate = txtBox.Text;
string insertStr = "insert into yourTable(datefield) values (@.datefield)";
SqlConnection conn = new SqlConnection(connectionStr);
SqlCommand cmd = new SqlCommand(insertStr, conn);
cmd.Parameters.Add("@.datefield", SqlDbType.DateTime, 8)
cmd.Parameters["@.datefield"].Value = DateTime.Parse(yourDate)
using(conn){
using(cmd){
conn.Open();
cmd.ExecuteNonQuery();
}
}

HTH. If this does not answer your question, please feel free to mark the post as Not Answered and reply. Thank you!