Solved

C# .NET MySql - handling null fields

Posted on 2008-06-14
2
1,594 Views
Last Modified: 2013-12-17
In my MySQL table I allow NULL values in some fields.
When I itterate through the result set I get an error whenever the reader hits such a NULL value.
Here is the code:
IDataReader reader = dbcmd.ExecuteReader();

                while (reader.Read())
                {

                    string[] catalog_info = {
                (string)reader["tab_name"], (string)reader["document_name"], (string)reader["comment_text"]};

                    dataGridViewCurrentSessionPatientDocuments.Rows.Add(catalog_info);

                }

Here is the error: "Unable to cast object of type 'System.DBNull' to type 'System.String'."
What I need is to instruct the reader to substitute an emplty string ("") for a NULL value


0
Comment
Question by:eugene-g
2 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 50 total points
Comment Utility
or do it directly in the query? ...
COALESCE(yourfield, '' )

in the .net code:
string[] catalog_info = { 
                (string) (reader["tab_name"] == DBNull.Value ? "" : reader["tab_name"] )
              , (string) (reader["document_name"] == DBNull.Value ? "" : reader["document_name"] )
              , (string) (reader["comment_text"] == DBNull.Value ? "" : reader["comment_text"] )
               };

Open in new window

0
 

Author Closing Comment

by:eugene-g
Comment Utility
Veru ellegant and convinient. Thanks
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
First name pregmatch 11 29
Limiting string to two deciamls 18 30
Closing all open child forms. 4 22
Unable  to create new object 9 19
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now