Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | ASP.NET 2.0 Tutorials | Web Services | How Do I...? | Class Browser | WPF Quick Starts | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
Search :       Advanced Search »
Home » Blogs Home » Blog Detail

Dealing with Database Reader to avoid errors when DB has null value

 by Abhimanyu Kumar Vatsa on Feb 02, 2012

In this quick post you are going to learn the way to avoid errors that comes when DB passes null value to DB Reader.
Comments: 0 Views: 728 Printable Version 

Introduction

Yesterday we were working on a project with my team and we were getting an error message on our production server when I looked at the code, I found titled error. To fix this I am using following code:

string connectionString = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;

            SqlConnection conn = new SqlConnection(connectionString);

            conn.Open();

            SqlCommand command = new SqlCommand("SELECT * FROM NewReRegistration WHERE rollnumber=@RollNumber", conn);

            command.Parameters.Add(new SqlParameter("RollNumber", RollNumber));

            SqlDataReader reader = command.ExecuteReader();

            while (reader.Read())

            {

                candidatename.Text = reader.IsDBNull(4) ? "" : reader.GetString(4);

                fathername.Text = reader.IsDBNull(9) ? "" : reader.GetString(9);

                guardianname.Text = reader.IsDBNull(10) ? "" : reader.GetString(10);

                relationwithguardian.Text = reader.IsDBNull(11) ? "" : reader.GetString(11);

                dob.Text = reader.IsDBNull(12) ? "" : reader.GetString(12).ToString();

                permanentaddress.Text = reader.IsDBNull(16) ? "" : reader.GetString(16);

                correspondenceaddress.Text = reader.IsDBNull(17) ? "" : reader.GetString(17);

                mobilenumber.Text = reader.IsDBNull(18) ? "" : reader.GetString(18);

                otherphonenumbers.Text = reader.IsDBNull(19) ? "" : reader.GetString(19);

                emailid1.Text = reader.IsDBNull(20) ? "" : reader.GetString(20);

               

            }

 

This is the code I am still using for that web application.

Here is the code that has issue:

otherphonenumbers.Text = reader.GetString(19);

Actually, in above code I am trying to read 4th cell values from database and unfortunately database has null value and the same it produced error.

Now by using Ternary Operator we can fix this as follows:

          otherphonenumbers.Text = reader.IsDBNull(19) ? “” : reader.GetString(19);

And this concept worked fine for us.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
 
What do you say about this post? Post a comment here
*Title:
*Comment:
 
Comments not available.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor

 Blogger's Profile
Age: 24
Location:
Title: Developer
Joined: May 31, 2010
Education: Bachelors Degree
 More Blogs from this Blogger
No record available
 Latest Blogs
[Video] OnClose Handler
[Video] Storing and Loading the Window and Toolbar position
The Euclidean Algorithm
Swapping Exe Process
How Exe file is Generated by VS2005 C++ Project?
What is Exe
Header files: Multiple Inclusion problem - Solution B
Header files: Multiple Inclusion problem - Solution A
Header files: Multiple Inclusion problem - Reason
Header files: Multiple Inclusion problem
View all »
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 
6 Months Free & No Setup Fees ASP.NET Hosting!
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.