the conversion of a char data type to a

Posted by Ganthi1977 under ASP.NET on 6/30/2011 | Points: 10 | Views : 1636 | Status : [Member] | Replies : 2
Hi,

I am getting the error "the conversion of a char data type to a datetime data type resulted in an out-of-range datetime value in asp.net" when i insert date value into table.

aspx.code
---------
Private [date] As String = DateTime.Now.ToString

StrQry = ""
Conn.Open()
StrQry = "insert into csproject(projectname,projectcode,duration,CreatedBy,CreatedDate) values ('" & txtProjName.Text & "','" & txtProjCode.Text & "','" & Session("EmpNo") & "','" & [date] & "')"
Cmd.Connection = Conn
Cmd.CommandText = StrQry
Cmd.ExecuteNonQuery()
Cmd.Dispose()
Conn.Close()

What is the problem here...

My "CreatedDate" column datatype is datetime.


pls. help

Regards,
Ganesh.S




Responses

Posted by: Lakn2 on: 6/30/2011 [Member] Starter | Points: 25

Up
0
Down
then use convert.todatetime(date) in insert statement

Thanks&Regards
LakshmiNarayana Nalluri.

Ganthi1977, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ndebata on: 6/30/2011 [Member] Starter | Points: 25

Up
0
Down
Try to use GetDate() in your sql query, You do not need to pass the CurrentDate and Time, GetDate() will return current datetime.
Please do not pass sql parameter as a Formatted SQL string, which in some scenario may lead to SQL Injection attack.
Try to use SQl Parameters, Or Create a StoreProcedure with datetime parameter . then pass your datetime value as an object to that query/store procedure.
http://www.csharpfriends.com/articles/getarticle.aspx?articleid=78
http://www.codeproject.com/KB/cs/simplecodeasp.aspx

Ganthi1977, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response