473,378 Members | 1,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

database INSERT choking on dbNULL values

Hi, I'm trying to update a DBF file which I'm using VB.NET ODBC adapters,
commands and connections. I fill the dataset and databind the columns to
textboxes on my form. I can successfully view, delete and update the
records. The problem is when I try to add a new record.

Because I actually have a few controls bound to the same table I use
FORM.BINDINGCONTEXT to keep it all synced. So, using the
form.bindingcontext.addnew method. my textboxes clear and I enter 2 or 3 of
the 7 fields. When I hit my save button which does a
BindingContext.EndCurrentEdit and creates a new dataset (dsChanged) with
only the "changed" records, then I do a dataAdapter.Update(dsChanged) and it
throws an exception stating something about my first unentered field is
DBNull. and the database does not allow that. If I actually fill in the
field that I got the error on and try again, the error "moves" to the next
unentered field.

I've tried a couple of ways programatically "blank out" the fields in the
dsChanged dataset, but they remain (the debugger quickview confirms this) a
DBNull value.

How can I eliminate the DBNull values from my dataset programatically? Any
suggestions gratefully accepted.

Nov 21 '05 #1
4 3184
John,

As I have seen is ODBC rarely used by visitors to this newsgroups. The best
change on an answerer you therefore will have in a more dedicated (active)
newsgroup like.

Adonet

Adonet
news://msnews.microsoft.com/microsof...amework.adonet

Web interface:
http://communities2.microsoft.com/co...amework.adonet

I hope this helps,

Cor
Nov 21 '05 #2
The problem is not your code... it is a disconnect between your code and the
expectations of your database.

If you expect to be able to enter two of the seven fields and save the
record, then the other five fields must allow null values in the database.
If the other five fields do not allow nulls, you have to ask yourself: did
the database developer (was it you?) set up the column nullability correctly
on those columns.

In other words, the right answer may be to fix the database, and not your
code.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"JohnR" <Jo******@hotmail.com> wrote in message
news:0a1%d.9992$Ue6.7916@trndny04...
Hi, I'm trying to update a DBF file which I'm using VB.NET ODBC adapters,
commands and connections. I fill the dataset and databind the columns to
textboxes on my form. I can successfully view, delete and update the
records. The problem is when I try to add a new record.

Because I actually have a few controls bound to the same table I use
FORM.BINDINGCONTEXT to keep it all synced. So, using the
form.bindingcontext.addnew method. my textboxes clear and I enter 2 or 3
of the 7 fields. When I hit my save button which does a
BindingContext.EndCurrentEdit and creates a new dataset (dsChanged) with
only the "changed" records, then I do a dataAdapter.Update(dsChanged) and
it throws an exception stating something about my first unentered field is
DBNull. and the database does not allow that. If I actually fill in the
field that I got the error on and try again, the error "moves" to the next
unentered field.

I've tried a couple of ways programatically "blank out" the fields in
the dsChanged dataset, but they remain (the debugger quickview confirms
this) a DBNull value.

How can I eliminate the DBNull values from my dataset programatically?
Any suggestions gratefully accepted.

Nov 21 '05 #3
Hi Nick,

You have a very good point, and I will check it out. However the DB I'm
using now is just a test for when I convert an old VB6 application to VB.NET
and I wanted to make sure I understood how to do all the things I need to do
for the conversion.

The actual production DB may, in fact, not allow null values, so my
question is this: After I load the dsChanged dataset with the newly
inserted row, I try to replace any nulls with a blank string: here's a code
snippet:

dsChanged = dsTele.GetChanges() 'get all the changed rows and stick them in
a dataset

If Not dsChanged Is Nothing Then

For Each xRow As DataRow In dsChanged.Tables("rolodex").Rows

If Not xRow.RowState = DataRowState.Deleted Then 'can't change field on del
rec

'scan remaining columns to ck for null entries

For i As Int32 = 0 To xRow.ItemArray.GetLength(0) - 1

xRow.ItemArray(i) = utility.NullSafeString(xRow.ItemArray(i), " ")

Next

End If

Next

End If

The nullsavestring will stick the 2nd parameter (the " ") into the 1st
parameter (xRow.ItemArray(i) ) if it is null.

Do you see anything wrong with this code? Cause it doesn't seem to work,
even though when I trace it thru debug it seems like it should work.

Thanks, John

"Nick Malik [Microsoft]" <ni*******@hotmail.nospam.com> wrote in message
news:yr********************@comcast.com...
The problem is not your code... it is a disconnect between your code and
the expectations of your database.

If you expect to be able to enter two of the seven fields and save the
record, then the other five fields must allow null values in the database.
If the other five fields do not allow nulls, you have to ask yourself: did
the database developer (was it you?) set up the column nullability
correctly on those columns.

In other words, the right answer may be to fix the database, and not your
code.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"JohnR" <Jo******@hotmail.com> wrote in message
news:0a1%d.9992$Ue6.7916@trndny04...
Hi, I'm trying to update a DBF file which I'm using VB.NET ODBC adapters,
commands and connections. I fill the dataset and databind the columns to
textboxes on my form. I can successfully view, delete and update the
records. The problem is when I try to add a new record.

Because I actually have a few controls bound to the same table I use
FORM.BINDINGCONTEXT to keep it all synced. So, using the
form.bindingcontext.addnew method. my textboxes clear and I enter 2 or 3
of the 7 fields. When I hit my save button which does a
BindingContext.EndCurrentEdit and creates a new dataset (dsChanged) with
only the "changed" records, then I do a dataAdapter.Update(dsChanged) and
it throws an exception stating something about my first unentered field
is DBNull. and the database does not allow that. If I actually fill in
the field that I got the error on and try again, the error "moves" to the
next unentered field.

I've tried a couple of ways programatically "blank out" the fields in
the dsChanged dataset, but they remain (the debugger quickview confirms
this) a DBNull value.

How can I eliminate the DBNull values from my dataset programatically?
Any suggestions gratefully accepted.


Nov 21 '05 #4
Hi All,

Well, I found the answer to my own problem.... The line:

xRow.ItemArray(i) = utility.NullSafeString(xRow.ItemArray(i), " ")

was not updating the columns in the dataset as expected. when I changed the
ItemArray to Item like this:

xRow.Item(i) = utility.NullSafeString(xRow.Item(i), " ")

it worked fine. Now I have to go back and study the difference between
using Item and ItemArray.

John

"JohnR" <Jo******@hotmail.com> wrote in message
news:8Tl%d.12054$I16.11095@trndny03...
Hi Nick,

You have a very good point, and I will check it out. However the DB
I'm using now is just a test for when I convert an old VB6 application to
VB.NET and I wanted to make sure I understood how to do all the things I
need to do for the conversion.

The actual production DB may, in fact, not allow null values, so my
question is this: After I load the dsChanged dataset with the newly
inserted row, I try to replace any nulls with a blank string: here's a
code snippet:

dsChanged = dsTele.GetChanges() 'get all the changed rows and stick them
in a dataset

If Not dsChanged Is Nothing Then

For Each xRow As DataRow In dsChanged.Tables("rolodex").Rows

If Not xRow.RowState = DataRowState.Deleted Then 'can't change field on
del rec

'scan remaining columns to ck for null entries

For i As Int32 = 0 To xRow.ItemArray.GetLength(0) - 1

xRow.ItemArray(i) = utility.NullSafeString(xRow.ItemArray(i), " ")

Next

End If

Next

End If

The nullsavestring will stick the 2nd parameter (the " ") into the 1st
parameter (xRow.ItemArray(i) ) if it is null.

Do you see anything wrong with this code? Cause it doesn't seem to work,
even though when I trace it thru debug it seems like it should work.

Thanks, John

"Nick Malik [Microsoft]" <ni*******@hotmail.nospam.com> wrote in message
news:yr********************@comcast.com...
The problem is not your code... it is a disconnect between your code and
the expectations of your database.

If you expect to be able to enter two of the seven fields and save the
record, then the other five fields must allow null values in the
database.
If the other five fields do not allow nulls, you have to ask yourself:
did the database developer (was it you?) set up the column nullability
correctly on those columns.

In other words, the right answer may be to fix the database, and not your
code.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"JohnR" <Jo******@hotmail.com> wrote in message
news:0a1%d.9992$Ue6.7916@trndny04...
Hi, I'm trying to update a DBF file which I'm using VB.NET ODBC
adapters, commands and connections. I fill the dataset and databind the
columns to textboxes on my form. I can successfully view, delete and
update the records. The problem is when I try to add a new record.

Because I actually have a few controls bound to the same table I use
FORM.BINDINGCONTEXT to keep it all synced. So, using the
form.bindingcontext.addnew method. my textboxes clear and I enter 2 or 3
of the 7 fields. When I hit my save button which does a
BindingContext.EndCurrentEdit and creates a new dataset (dsChanged) with
only the "changed" records, then I do a dataAdapter.Update(dsChanged)
and it throws an exception stating something about my first unentered
field is DBNull. and the database does not allow that. If I actually
fill in the field that I got the error on and try again, the error
"moves" to the next unentered field.

I've tried a couple of ways programatically "blank out" the fields in
the dsChanged dataset, but they remain (the debugger quickview confirms
this) a DBNull value.

How can I eliminate the DBNull values from my dataset programatically?
Any suggestions gratefully accepted.



Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Arek | last post by:
Hey, I am inserting values in the table: Dim sqlcomm1 As SqlCommand = New SqlCommand("INSERT INTO tblTasks (idTask, outdate) VALUES ('" & IDTask.text & "','" & txtOutdate.Text & "')", conn)...
4
by: chambersdon | last post by:
I have an application that needs to insert nulls into the database and I don't seem to be able to do this. I am currently trying to do this with a Typed DataSet but I can't seem to Insert Nulls...
0
by: JohnR | last post by:
Hi, I'm trying to update a DBF file which I'm using VB.NET ODBC adapters, commands and connections. I fill the dataset and databind the columns to textboxes on my form. I can successfully view,...
3
by: Dylan Parry | last post by:
Hi folks, This is really confusing me. I am trying to insert a row into a database, and then use scope_identity() to return the value of the ID column for the record I have just created. The...
6
by: Ryan | last post by:
I have 2 Access '97 databases. I am trying to migrate all the data from one to the other (append to existing records). I do not have Access '97 and opening with Access XP or later causes...
9
by: Martin Arvidsson | last post by:
Hi! I have two computed datafields that i am displaying in my windowsform. When i try to update the dataset and commit the transaction. I get an error message telling me that i cant update...
8
by: Martin Z | last post by:
INSERT INTO dbo.Transmission (TransmissionDate, TransmissionDirection, Filename, TransmittedData) VALUES (@TransmissionDate,@TransmissionDirection,@Filename,@TransmittedData); SELECT @retVal =...
3
by: Finn Stampe Mikkelsen | last post by:
Hi I have defined a table in my database, with 2 date-fields. I have set a default value to DBNull. I have integrated a nullable datetimepicker control to my project and set the apropriate...
2
by: cday119 | last post by:
Can someone help me out with this one. I am trying to insert a row of data into an access database. The primary key is set to auto increment. It will work once but the next time you try an insert...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.