Binding a DBNull with a Textbox

I just wonder if people you know this trick I use (VB) all the time to avoid the infamous DBNull headache.

MyTextbox.Text = MyReader("Field") + ""

It's surely cleaner to have a function to check DBNull but I found this more easy and quicker to type.

I am not sure if this has some performances issues, not as I am aware for the moment.

UPDATE : Of course only with String values !

3 Comments

  • I do something similar. On string values that I know can be null, I'd do MyReader("Field").ToString().

  • You have to be careful with ToString, if the value is null (really null, not DBNull), you can get a null reference exception. Yah, this shouldn't happen with a datareader pulling from SQL Server, but there are plenty of other places where it would.



    in C#, you can use the "as" operator to do this type of thing (MyReader["field"] as string).



    In all languages, you also have the option of: Convert.ToString(MyReader["field"]), which works pretty good too.

  • so simple and funny



    regards,



Comments have been disabled for this content.