Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends,
Please anyone rectify the error, i m not able to insert the date from textbox to sql database

C#
protected void btnAdd_Click(object sender, EventArgs e)
       {
           if(cnn.State==ConnectionState.Closed)
           cnn.Open();
           string str=txtDate.Text ;
           DateTime dt;
           dt = DateTime.Parse(str);
           cmd= new SqlCommand("insert into tbl_holiday values('"+str.ToString()+"','"+txtHoliday.Text+"')",cnn);
           cmd.ExecuteNonQuery();
          


       }
Posted
Comments
Prasad_Kulkarni 9-Aug-12 7:36am    
..what error you're getting Vijay?
[no name] 9-Aug-12 7:45am    
Probably because of str.ToString() when you wanted your dt. Why would you call ToString() on a string anyway?

First of all make sure your column has the right data type (DateTime)
Then keep dates as DateTime. No need to convert it to string before sending it to your database.

Then use a SqlParameter to send data. The code you are using now is vulnerable for sql injections!

C#
protected void btnAdd_Click(object sender, EventArgs e)
{
if(cnn.State==ConnectionState.Closed)
 cnn.Open();

 DateTime dt = DateTime.Parse(txtDate.Text);
 cmd = new SqlCommand("insert into tbl_holiday values(@date, @holyday)",cnn);
 cmd.Parameters.AddWithValue("@date", dt);
 cmd.Parameters.AddWithValue("@holiday", txtHoliday.Text);
 cmd.ExecuteNonQuery();
}


Also when you parse dates like you do, you should use TryParse to see if you actually can parse the input from the user.
C#
string d = txtDate.Text;
DateTime dt;
if(DateTime.TryParse(d, out dt))
{
    // Parse ok..
}
else{
    // Could not parse the date..
}
 
Share this answer
 
v2
Comments
Rockstar_ 10-Aug-12 1:09am    
Thanks ....
pass your date time variable dt instead of str.

C#
protected void btnAdd_Click(object sender, EventArgs e)
       {
           if(cnn.State==ConnectionState.Closed)
           cnn.Open();
           string str=txtDate.Text ;
           DateTime dt;
           dt = DateTime.Parse(str);
           cmd= new SqlCommand("insert into tbl_holiday values('"+dt+"','"+txtHoliday.Text+"')",cnn);
           cmd.ExecuteNonQuery();



       }
 
Share this answer
 
Comments
StianSandberg 9-Aug-12 8:10am    
the dt will be converted to a string anyway. The SqlCommand CommandText is a string... AND it's vulnerable for sql injections!
As I can see your code, you are parsing the textbox value to datetime. But instead of parsed datetime,you are inserting string value of the textbox again. Try this:
C#
protected void btnAdd_Click(object sender, EventArgs e)
{
   if(cnn.State==ConnectionState.Closed)
   cnn.Open();
   string str=txtDate.Text ;
   DateTime dt;
   dt = DateTime.Parse(str);
   cmd= new SqlCommand("insert into tbl_holiday values('"+str.ToString()+"','"+dt+"')",cnn);
   cmd.ExecuteNonQuery();
}



--Amit
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900