C
C#13mo ago
Williamo

❔ 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
No description
No description
No description
34 Replies
Williamo
WilliamoOP13mo ago
The get set connected to the method called conversion in the image above
No description
Williamo
WilliamoOP13mo ago
This is an example of what i would send through and would break it
No description
Jimmacle
Jimmacle13mo ago
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
Jimmacle
Jimmacle13mo ago
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.
Williamo
WilliamoOP13mo ago
this is for school and we were taught to do it this way
Jimmacle
Jimmacle13mo ago
the way your school is teaching you to do it is dangerous
Williamo
WilliamoOP13mo ago
😭 still is there anyway to do it with it like how it is right now or no?
Jimmacle
Jimmacle13mo ago
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
Williamo
WilliamoOP13mo ago
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?
Jimmacle
Jimmacle13mo ago
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
Williamo
WilliamoOP13mo ago
alright ill try it rn
Williamo
WilliamoOP13mo ago
No description
Jimmacle
Jimmacle13mo ago
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 unambiguous
MODiX
MODiX13mo ago
Jimmacle
REPL Result: Success
DateTime.Now.ToString("O")
DateTime.Now.ToString("O")
Result: string
11/7/2023 5:59:06 PM
11/7/2023 5:59:06 PM
Compile: 358.727ms | Execution: 35.539ms | React with ❌ to remove this embed.
Jimmacle
Jimmacle13mo ago
that is not what that should have done PepeHmmm
Williamo
WilliamoOP13mo ago
harold
MODiX
MODiX13mo ago
Jimmacle
REPL Result: Success
DateTime.Now.ToString("o")
DateTime.Now.ToString("o")
Result: string
11/7/2023 6:00:03 PM
11/7/2023 6:00:03 PM
Compile: 342.227ms | Execution: 34.513ms | React with ❌ to remove this embed.
Williamo
WilliamoOP13mo ago
how would i format it, ive never done this before
Jimmacle
Jimmacle13mo ago
well, like that supposedly datetime must not have an ISO8601 format specifier
Williamo
WilliamoOP13mo ago
does that not take the current date and time
MODiX
MODiX13mo ago
Jimmacle
REPL Result: Success
DateTimeOffset.Now.ToString("o")
DateTimeOffset.Now.ToString("o")
Result: string
11/7/2023 6:00:57 PM
11/7/2023 6:00:57 PM
Compile: 525.450ms | Execution: 46.331ms | React with ❌ to remove this embed.
Jimmacle
Jimmacle13mo ago
Wat for some reason modix is being really weird
Jimmacle
Jimmacle13mo ago
No description
Jimmacle
Jimmacle13mo ago
that's what it actually does
Williamo
WilliamoOP13mo ago
i still dont get how i would implement that into the text input i get from the textbox
Jimmacle
Jimmacle13mo ago
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
Williamo
WilliamoOP13mo ago
so i would include that somewhere here?
No description
Jimmacle
Jimmacle13mo ago
yes, like editobj.DateofEvent.ToString("O")
Williamo
WilliamoOP13mo ago
No description
Williamo
WilliamoOP13mo ago
No description
Williamo
WilliamoOP13mo ago
inputted 30 as the date and it broke
Williamo
WilliamoOP13mo ago
heres my sql table if u need it idk
No description
Williamo
WilliamoOP13mo ago
im wondering if this could somehow be the issue
No description
Accord
Accord13mo ago
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.
Want results from more Discord servers?
Add your server