Click here to Skip to main content
15,900,461 members
Please Sign up or sign in to vote.
2.22/5 (2 votes)
See more:
in C# Windows Form
In this Paid_Dateis in string format, i am converting the string data type into datetime data type after i am insert it into sql db its show the error .
C#
string P_date = row.Cells["Paid_Date"].Value.ToString();
                                DateTime h1 = new DateTime();
                               
                                h1 = DateTime.Parse(P_date );
string s=" insert into payment (paid_date) values('"+h1 +"')";
 g.ExecuteNonQuery(s, null);



in sql server
C#
column name      datatype
paid_date 	    datetime	


I tried Lot but it show like this.
Error in sql server: conversion of char data type to datetime datatype resulted in an out-of-range datetime value.
The statement as terminated...........
Posted
Updated 4-May-15 20:52pm
v6
Comments
upendra shahi 5-May-15 2:45am    
DateTime.ParseExact(P_date );
Tomas Takac 5-May-15 2:56am    
You should use parameters with your SQL command. I'm sure your DA framework (the "g") supports it.
Abdul Samad KP 5-May-15 3:06am    
Always use SQL parameters in your command instead of appending command with the values.

Here the problem is date format.
If you systems are using English culture then you can try
string s=" insert into payment (paid_date) values('"+h1.ToString("dd/MMM/yyyy") +"')";
Member 11644272 5-May-15 3:28am    
Abdul samad KP Thanks a Lot.........

Don't do it like that! Do not 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.

You can then pass the actual value of the C# verified DateTime directly to the database.
(I can't tell you exactly how to do it, I have no idea what class your "g" variable is)

And it you are doing it there, the chances are you are concatenating string in other places as well - so check and change them as a high priority, or your database will be damaged or destroyed!
 
Share this answer
 
try with this code
Date("12/02/21 10:56:09").ToString("MMM. dd, yyyy HH:mm:ss")
and if you use visual studio change date format to <custom>
and set custom format <yyyy-mm-dd>
 
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