Solved

C# .NET MySql - handling null fields

Posted on 2008-06-14
2
1,595 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
ID: 21786744
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
ID: 31467258
Veru ellegant and convinient. Thanks
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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 and create keystrokes in Netbeans IDE 8.0 for Windows.

920 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

15 Experts available now in Live!

Get 1:1 Help Now