Wednesday, March 7, 2012

Null Values

I set up a new SQL database file, in that file I allowed nulls, When I went through code to save the record, the exception is saying it doesnt allow nulls.

Before I get to involved with SQL, is it a bad practice to use nulls?

If it is what do you enter in place of the null value,

which will lead to more code, right?

Davids Learning SQL

Having and using nulls is not bad practice.....

Your problem maybe that your trying to have a null on your key field........

|||

No, the primary key did have a number assigned that did not exist.

I did a msgbox to throw the value back to me just to see in case and it was the number I chose.

I intentionally did not enter a value into a int field to see what would happen.

I opened the database file, and allow nulls is checked for the field and the field is not the primary

Any thoughts?

Davids Learning

|||Is the field a date field?|||I remember running in to similar problem, but I am not sure if it is exactly the same. The work around (or the solution for that matter) was I opened the DataSet containing my dataTable. Clicked on the field in that dataTable and in properties, I Changed 'NullValue' property to (Nothing). By default, it's set to (Throw Exception), which forces the dataSet to throw exception even if its AllowDBNull is set to true. See if that solves your issue.|||

is there a way to default to "Nothing" instead of throwing an exception?

I wont be able to try your suggestion until Monday, when I get back to my server at work.

Thanks

Davids Learning

|||Move the thread in this forum in order to get better answers.|||That depends, what does nothing mean in your case ? Normally nothing is equal to NULL when "nothing" is entered.

Jens K. Suessmeyer.

http://www.sqlserver2005.de
|||

Hey, sorry for the delay, been having ticker problems, Im back now

What I was getting at was to have the designer default or not check for null instead of throwing exceptions.

There is not but 1 field that needs not to be null and thats the primary field and I am taking care of that.

Is there a way to accomplish that.

The tables that I am dealing with contain a lot of fields and it will take some time to go through and change.

Thanks Again

Davids Learning

|||Hi,

what about setting a default value in SQL Server then ?

Jens K. Suessmeyer.

http://www.sqlserver2005.de
|||

I need to post what I did to mask the problem

I went into the table designer in VB and changed how each column handles nulls, basically setting them to not throw an exception on dbnulls.

I didnt want values in some fields intentionally because of other factor, like dates, if I set a default date in an expiration field(that would be bad).

I will on the other hand have to watch and plan on handling nulls now, and thats alright.

Thanks

Davids Learning

|||To me, it looked like a VB bug, but I will leave the judgement for you guys. If I allow nulls in my database table and create a typed DataSet from that table, the DataSet throws an error when it encounters a null value. If the DataSet's AllowDBNull is set to true, then it should default to 'Nothing' for NullValue property. It does not seem logical to me (Allow DBNull, but throw an error!!!). For those who want to reproduce the problem, use bound DataGridview with some fields that allow DBNull and try to save without entering data in those fields.|||

Glad to see someone in the same boat

Davids Learning

No comments:

Post a Comment