DBNull.Value.ToString why it does not work?

Hi, I'm trying to use DBNull.Value.ToString in sql statement to insert nul value but it does not work, any suggestion?
LVL 3
Abdu_AllahAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
that won't work indeed...

if you used a Command object with Parameters, then you can assign the DBNull.Value as such to the parameter, then it will work.

please show your code.
0
Abdu_AllahAuthor Commented:

Dim fx as string
If x= 1 then
 fk = DBNull.Value.ToString
else
 fk = 5
end if
 
strSQL = "Insert into mytable(...,fk,...) values("... + fk + ")"

Open in new window

0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
you "could" make your code like this:
Dim fx as string
If x= 1 then
 fk = "NULL"
else
 fk = 5
end if
 
strSQL = "Insert into mytable(...,fk,...) values("... + fk + ")"

Open in new window

0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
however, I repeat, using the db Command + Parameter objects would be better...
0
Abdu_AllahAuthor Commented:
Isn't it possible to do that in the way I put?
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
actually, I never tried the ToString of the dbnull.value, so it might work.... for numerical data types columns.
creating the sql ad-hoc like that is subject to be sql-injected. so I don't really like giving advice on using it.

take this less for using SqlCommand and SqlParameter objects, with the DBNull.Value being assigned for the parameter object will work just fine.
http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson06.aspx
0
Éric MoreauSenior .Net ConsultantCommented:
1. You declare "fx" and use "fk".

2. Even if you were using the correct variable name, DBNull.Value.Tostring returns an empty string so it wouldn't work.
0
tiagosalgadoCommented:
You can ignore the field that you're trying to set a null value in your IF statement.


Dim fk as string
If x= 1 then
 strSQL = "Insert into mytable(...) values(...)"
else
 fk = 5
 strSQL = "Insert into mytable(...,fk,...) values("... + fk + ")"
 
end if
 

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

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.