Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / Web Services / April 2006

Tip: Looking for answers? Try searching our database.

ObjectDataSource with nulls returned from DataReader

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dabbler - 05 Apr 2006 06:52 GMT
I'm trying to use SqlDataReader to retrieve values from a row and pass them
to a business class constructor (used by ObjectDataSource) but get exception
if the string field contains a null value in the table.  Is there some way to
have the DataReader return String.Empty if the column is null?

Any suggestions on how to handle this would be greatly appreciated.

public RegistrantDetails GetRegistrant(int registrantId){
 SqlConnection con = new SqlConnection(connectionString);
 SqlCommand cmd = new SqlCommand("GetRegistrant", con);
 cmd.CommandType = CommandType.StoredProcedure;
 cmd.Parameters.Add(new SqlParameter("@RegistrantId", SqlDbType.Int, 4));
 cmd.Parameters["@RegistrantId"].Value = registrantId;

 try
 {
     con.Open();
     SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow);  
       
     // Get the first row.
     reader.Read();
     RegistrantDetails det = new RegistrantDetails(
      (int)reader["RegistrantId"],
      (string)reader["FirstName"],
      (string)reader["MI"], // this column is null and generates exception
...
    );
    reader.Close();
    return det;
....
Dabbler - 05 Apr 2006 08:03 GMT
sorry, posted to the wrong group. solved the problem by testing for  DBNull
as in
  reg.MI = (string)(reader["MI"] is DBNull ? String.Empty : reader["MI"]);
tedious, as I have 50+ columns.

> I'm trying to use SqlDataReader to retrieve values from a row and pass them
> to a business class constructor (used by ObjectDataSource) but get exception
[quoted text clipped - 26 lines]
>     return det;
> ....

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.