Thank you for your reply. DateTime' — Ashok kumar. Sorry, I don't think I made myself clear enough. It looks like a lot of people are saying the same thing. This can have big repercussions in your app, though, so do this carefully and with lots of testing. Good luck! Waqas Waqas 6, 2 2 gold badges 29 29 silver badges 43 43 bronze badges. DateTime is not Nullable per se, you need to specify it as a Nullable, as already noted.
You need the column to be nullable too then. Yeah, I have done like this already. My problem is with the stored procedure insert statement. Ashok Wouldn't harm if you would show that proc and the exact exception text in your post. Modifying the stored procedure works, but I think its a bit sloppy. You can handle it in code, this work for me: DateTime? Parse TextBoxWithDate. Diomedes Diomedes 10 10 silver badges 20 20 bronze badges. Radhakrishnan V Radhakrishnan V 11 4 4 bronze badges.
Value — Radhakrishnan V. Tunaki k 44 44 gold badges silver badges bronze badges. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. When you then try and insert said empty string into your database via a SQL-injection-prone approach, you're really running a query something like:.
You need to parameterize your queries. Then you can pass Nothing directly to your SQL command, and it will work. Aside from that, you have to shift some abstraction so that you only add ' characters if the argument is not Nothing , and if it is, then you need to pass the string NULL. ToString returns an unconverted instance of the original value. In other words, String. IsNullOrEmpty value3 already does what you're checking for in your latter condition, except if the latter condition were ever true it would throw a NullReferenceException or whatever the equivalent of that is in VB, I don't know whether it uses Nothing because I've literally never written anything in VB before.
Next, you should be aware that value3 will never be Nothing. It comes directly from a TextBox. Text property, and will consequently be at most an empty string. So basically, long story short, parameterize your queries. For all our sakes.
But if you absolutely aren't going to, this should fix this particular problem:. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 7 years, 5 months ago. Active 7 years, 5 months ago. Viewed 22k times. RowCommand If e. Cells 9. In the stored procedure I have variables that accept null for inserting into the table.
My problem is when I try to insert a null value into my table's datetime column it throws error. The key being DBNull. I imagine you have a nullable DateTime variable, i. You can test it for null and if it is use DBNull. Value as shown above.
First of all, to be able to insert null into a database column, the column must accept null s. Open the table in the designer, and see if the relevant column is nullable or not. If it's not and you try to insert a null value, you'll get the error:. That said, since you are using stored procedures to insert data, I would recommend using default values for the parameters on the procedure level, instead of burdening the aplication code. Than, you can just not set the parameter on the application side, and it would work correctly:.
Note that there is no else branch, so if the InsertDate is empty, the parameter is not sent at all. Value ; else cmd. AddWithValue " enddate", Convert. ToDateTime InsertDate. Text ;. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. How to insert null in datetime type column in sql server Ask Question.
Asked 8 years, 5 months ago. Active 2 years ago. Viewed 83k times. Null; if InsertDate. Parameters[" InsertDate"]. Which error is thrown? Does the column allow null values? Add a comment.
0コメント