Thursday 27 June 2013

Insert Datetime in Default Insert Statement in ASP.net

Insert Datetime in Default Insert Statement in ASP.net

If u have Table is Like

UId=int
Uname= varchar2(50);
Udate= Datetime;

Then for Inserting Default date use Below 2 Methods:



Use below code  for Inserting  Current date As Default Value:


 '" + DateTime.Now.ToString("yyyy-MM-dd") + "'


        SqlCommand cmd = new SqlCommand("insert into user1 (uname,pwd,date1) values('" + TextBox1.Text + "','" + TextBox2.Text + "', '" + DateTime.Now.ToString("yyyy-MM-dd") + "') ", con);



Using  cmd.Parameter.AddwithValue Use Below Code to Insert Current Date As Defult

cmd.Parameters.AddWithValue("@date1", DateTime.Now);


        //SqlCommand cmd = new SqlCommand("insert into user1 (uname,pwd,date1)values(@uname,@pwd,@date1)", con);
        //cmd.Parameters.AddWithValue("@uname", TextBox1.Text);
        //cmd.Parameters.AddWithValue("@pwd", TextBox2.Text);
        //cmd.Parameters.AddWithValue("@date1", DateTime.Now);


No comments:

Post a Comment