How do I pass null values in an Insert statement to an oracle database?

I have this insert statement and I'm getting errors because it is not handling columns where null values are permitted...

strInsert = "INSERT INTO DRAWING" & _
                  "( DRAWING_ID, " & _
                  " DRAWING_NUMBER, " & _
                  " DRAWING_TYPE, " & _
                  " REVISION, " & _
                  " ISSUE_DATE) " & _
                  "VALUES " & _
                  "(" & drDrawing.Item("DRAWING_ID").ToString & "," & _
                          drDrawing.Item("DRAWING_NUMBER").ToString & "," & _
                          drDrawing.Item("DRAWING_TYPE").ToString & "," & _
                          drDrawing.Item("REVISION").ToString & "," & _
                          drDrawing.Item("ISSUE_DATE").ToString & ")"

The statement is passing values from the current data row object to a web service for execution. The problem is there is an error with this statement. I noticed that the only required column is Drawing_ID...if any of the other columns are passing a null then the whole block crashes.

What is the syntax for handling a null column? Any help would be appreciated...or enen a link in the right direction. I am on google right now trying to figure this out.

Who is Participating?
if you use bind variables,  it won't matter if any of the items are NULL or not, nor will you have to convert them to strings either.  You can pass strings as strings, dates as dates, numbers as numbers and nulls as nulls.

String concatenation also opens your application to sql injection.
use bind variables instead of string concatenation.

or, if you want to put in null...

strInsert = "INSERT INTO DRAWING" & _
                  "( DRAWING_ID, " & _
                  " DRAWING_NUMBER, " & _
                  " DRAWING_TYPE, " & _
                  " REVISION, " & _
                  " ISSUE_DATE) " & _
                  "VALUES " & _
                  "(" & drDrawing.Item("DRAWING_ID").ToString & ",NULL,NULL,NULL,NULL)"

Open in new window

imonfireDAMMITAuthor Commented:
I decided to use a string builder object and then pass the string builder to a string variable. This way I could pass the string to the webmethod.  THanks for your help I see that string concatenation was not my best bet
imonfireDAMMITAuthor Commented:
I decided to use a string builder to create my string...this made it easier to handle columns where nulls could be accepted because I could place If statements inside my string builder object.
string builder is still string concatenation and still open to sql injection.

Glad you got it to work, but you'll really benefit in the long run if you switch to using bind variables instead of embedded literals.  Your applications will run faster and your DBA's will start telling everyone else to code like you.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.