❔ issue trying to convert string into datetime in c# to be sent into sql datetime column
in my c# datagridview the column is in dd/mm/yyyy and in sql the column is in yyyy/mm/dd and im struggling to take in a string of a date from a textbox in c# and update my table which has the datetime column with this data
34 Replies
The get set connected to the method called conversion in the image above
This is an example of what i would send through and would break it
1. you should be using parameterized queries so you don't have SQL injection vulnerabilities in your program
2. once you do that you can easily set the parameter type/value to a plain datetime instance and ADO.NET will handle the conversion
Date and Time Data - ADO.NET
Learn about data types for handling date and time information in the .NET Framework Data Provider for SQL Server.
this is for school and we were taught to do it this way
the way your school is teaching you to do it is dangerous
😭
still is there anyway to do it with it like how it is right now or no?
you'll have to figure out why it can't convert the text form of your date to a datetime on the SQL side, i'd start by just printing out your query string to make sure it looks the way you expect
i did see online some people trying to convert the string to yyyy/mm/dd and sent it that way but cant get it working
well when i do the text form it takes the first digit as month for some reason
then when it sends it out it breaks cause i assume its cause the first digit has to be month?
just print out the query string and look at it/share it here
the first step of debugging is making sure you're sending the inputs you think you're sending
if it's a case of the date format not matching what the sql server expects, you can fix that by formatting the datetime differently
alright ill try it rn
yeah it's probably a culture issue
you can try formatting the datetime as a standard ISO 8601 timestamp, e.g.
DateTime.Now.ToString("O")
that should be unambiguousJimmacle
REPL Result: Success
Result: string
Compile: 358.727ms | Execution: 35.539ms | React with ❌ to remove this embed.
that is not what that should have done
Jimmacle
REPL Result: Success
Result: string
Compile: 342.227ms | Execution: 34.513ms | React with ❌ to remove this embed.
how would i format it, ive never done this before
well, like that supposedly
datetime must not have an ISO8601 format specifier
does that not take the current date and time
Jimmacle
REPL Result: Success
Result: string
Compile: 525.450ms | Execution: 46.331ms | React with ❌ to remove this embed.
for some reason modix is being really weird
that's what it actually does
i still dont get how i would implement that into the text input i get from the textbox
you don't, you apply it to your datetime when you format it into your SQL query
this will convert it to a standard format that it shouldn't have any trouble understanding
so i would include that somewhere here?
yes, like
editobj.DateofEvent.ToString("O")
inputted 30 as the date and it broke
heres my sql table if u need it idk
im wondering if this could somehow be the issue
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.