Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My database table is Events
EventId int
EventName varchar(50)
EventDate  datetime


I try to insert eventname and eventdate through insert query by using code it is necessary for me to keep datatype datetime of column EventDate

My code is this
C#
protected void Button1_Click(object sender, EventArgs e)
   {
       SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\omar\\Documents\\Visual Studio 2005\\WebSites\\Project2\\App_Data\\Database.mdf;Integrated Security=True;User Instance=True");
       con.Open();
       string que="insert into Events(EventName,EventDate) values('"+TextBox1.Text+"','"+TextBox2.Text+"')"; /* I try to a store date thorugh insert query and i use textbox2.text error comes conversion of chardatatype resulted in an out of range datetime */
       SqlCommand com=new SqlCommand(que,con);
       com.ExecuteNonQuery();
       con.Close();
   }
Posted
Updated 30-Apr-11 4:39am
v4

http://www.java2s.com/Code/ASP/Development/ConverttextinasptextboxintoDateC.htm[^]
OR
Convert date in textbox to either datetime or null

I hope the above information will be helpful. If you have more concerns, please let me know.
 
Share this answer
 
v3
Don't do that way
Use parameterized SQL queries[^]

C#
SqlConnection objConnection = new SqlConnection(_ConnectionString);
objConnection.Open();
SqlCommand objCommand = new SqlCommand(
   "INSERT INTO Events(EventName,EventDate) VALUES(@EventName,@EventDate)",
   objConnection);

SqlParameter parameter1 = new SqlParameter();
parameter1.ParameterName = "@EventName";
parameter1.SqlDbType = SqlDbType.NVarChar;
parameter1.Direction = ParameterDirection.Input;
parameter1.Value = categoryName;

objCommand.Parameters.Add(parameter1);

SqlParameter parameter2 = new SqlParameter();
parameter2.ParameterName = "@EventDate";
parameter2.SqlDbType = SqlDbType.DateTime;
parameter2.Direction = ParameterDirection.Input;
parameter2.Value = categoryName;

objCommand.Parameters.Add(parameter2);

objCommand.ExecuteNonQuery();
objConnection.Close();


BTW don't write Database related coding in your form or page. write in a separate layer. Refer this article. Three Layer Architecture in C# .NET[^]
 
Share this answer
 
Comments
RaviRanjanKr 30-Apr-11 13:41pm    
yeah! using Parametrized queries always help you with injection attacks so its a nice suggestion.
My vote of 5
Kim Togo 30-Apr-11 16:21pm    
My 5. SqlParameter for the win.
Try this sql statement

C#
string que="insert into Events(EventName,EventDate) values('"+TextBox1.Text+"',"+Convert.ToDateTime(TextBox2.Text)+")";


P.S: Try to do with command parameters to avoid sql injection attacks. Have a look here[^]

See some date time conversion issues[^]
 
Share this answer
 
v2
Comments
Monjurul Habib 30-Apr-11 10:46am    
nice advice. but what if user gives invalid date into the Textbox.
TweakBird 30-Apr-11 10:56am    
Validate datetime format before doing insert. is your application winforms or Asp.net?
if C# with winforms see here : http://stackoverflow.com/questions/371987/validate-a-datetime-in-c
otherwise (ASP.Net) see this http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/Calendar/Calendar.aspx
Monjurul Habib 30-Apr-11 11:56am    
nice links. my 5
TweakBird 30-Apr-11 13:17pm    
Thank you.
Prasanta_Prince 30-Apr-11 13:07pm    
Good one.

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