Click here to Skip to main content
15,905,558 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i select data from excel sheet and i want insert it in sql server but the date gave me error (Conversion failed when converting date and/or time from character string.)

this is code:

insert into DATA_NOTIFICATIONS(Notification_id,Notification_date)
Values (ds.Tables[0].Rows[i][11] + "','" +
ds.Tables[0].Rows[i][9].ToString() + "','" +)

What I have tried:

i try used datetime.ParseExact but it's give me same error.
so how i solve it this problem
Posted
Updated 8-Jan-17 21:03pm
v2

If I understand your situation correctly, instead of concatenating the values directly to SQL statement you should use parameters in the insert statement.

Have a look at the examples in Properly executing database operations[^]
 
Share this answer
 
Simple: Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
Convert your Excel value to a DateTime - assuming it comes in from Excel as a string, which it shouldn't - and pass the Datetime value directly to SQL via a parameter instead of converting it to a string.
 
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