Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
| Search | Today's Posts | Mark Forums Read
ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application .
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ADO.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old June 22nd, 2004, 06:17 PM
Friend of Wrox
 
Join Date: Jan 2004
Location: , , USA.
Posts: 303
Thanks: 0
Thanked 0 Times in 0 Posts
Default Handling Nulls

I am using a DataReader object to return a single row from a database. I then use each of the fields within this row to populate controls on an .aspx page chances are there that few columns may contain a null value. Right now I have to check each and every database fields whether it contains a "NULL", Is there a better way to approach it?
  #2 (permalink)  
Old June 22nd, 2004, 10:58 PM
Friend of Wrox
Points: 3,558, Level: 25
Points: 3,558, Level: 25 Points: 3,558, Level: 25 Points: 3,558, Level: 25
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: California, USA
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default

Hi there:
Im sure this code will help u. save it in a ASPX file & run, u'll see the result.
Code:
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<Script Runat="Server">
  void Page_Load( Object s, EventArgs e )
  {
    SqlConnection myConnection;
    SqlCommand myCommand;
    myConnection = new SqlConnection( "Server=Localhost;uid=sa;Database=Pubs" );
    myCommand = new SqlCommand( "Select title, notes From Titles", myConnection );
    myConnection.Open();
    myRepeater.DataSource = myCommand.ExecuteReader();
    myRepeater.DataBind();
    myConnection.Close();
  }

  String fixNulls( String theString )
  {
    if ( theString == "" ) 
      return "[NULL]";
    else
      return theString;
  }
</Script>

<html>
<head><title>Titles</title></head>
<body>
 
<form Runat="Server">
 
<asp:Repeater
  id="myRepeater"
  Runat="Server">
  <HeaderTemplate>
  <table border=1 cellspacing=0 cellpadding=5>
  </HeaderTemplate>
  <ItemTemplate>
  <tr>
    <td><%# fixNulls( DataBinder.Eval( Container.DataItem, "title" ).ToString() ) %></td>
    <td><%# fixNulls( DataBinder.Eval( Container.DataItem, "notes" ).ToString() ) %></td>
  </tr>
  </ItemTemplate>
  <AlternatingItemTemplate>
  <tr bgcolor="lightyellow">
    <td><%# fixNulls( DataBinder.Eval( Container.DataItem, "title" ).ToString() ) %></td>
    <td><%# fixNulls( DataBinder.Eval( Container.DataItem, "notes" ).ToString() ) %></td>
  </tr>
  </AlternatingItemTemplate>
  <FooterTemplate>
  </table>
  </FooterTemplate>
</asp:Repeater>
</form>

</body>
</html>
Always:),
Hovik Melkomian.
  #3 (permalink)  
Old June 23rd, 2004, 11:43 AM
Friend of Wrox
 
Join Date: Jan 2004
Location: , , USA.
Posts: 303
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I ended up doing this (in my case i am populating the web controls), does anybody see any problem with it..Please Let me know.


Dim objDR As OleDbDataReader
objConn.Open()
objDR = objCmd.ExecuteReader(CommandBehavior.CloseConnecti on)

If objDR.Read Then
'Populating web controls
txtCompanyName.Text = "" & objDR("CompanyName")
txtContactName.Text = "" & objDR("ContactName")
txtContactTitle.Text = "" & objDR("ContactTitle")
txtAddress.Text = "" & objDR("Address")
txtCity.Text = "" & objDR("City")
txtRegion.Text = "" & objDR("Region")
txtPostalCode.Text = "" & objDR("PostalCode")
txtCountry.Text = "" & objDR("country")
txtPhone.Text = "" & objDR("phone")
txtFax.Text = "" & objDR("Fax")
txtHomePage.Text = "" & objDR("HomePage")
End If
  #4 (permalink)  
Old June 23rd, 2004, 11:31 PM
Friend of Wrox
Points: 3,558, Level: 25
Points: 3,558, Level: 25 Points: 3,558, Level: 25 Points: 3,558, Level: 25
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: California, USA
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default

Doesnt it work? Does it gives error?!
but I do guess u should convert to string.
Code:
  ...
   txtCompanyName.Text = "" & objDR("CompanyName").ToString()
   txtContactName.Text = "" & objDR("ContactName").ToString()
   txtContactTitle.Text = "" & objDR("ContactTitle").ToString()
  ...


Always:),
Hovik Melkomian.
  #5 (permalink)  
Old June 24th, 2004, 11:29 AM
Friend of Wrox
 
Join Date: Jan 2004
Location: , , USA.
Posts: 303
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thank you, Hovik



Similar Threads
Thread Thread Starter Forum Replies Last Post
IN and NULLs joxa83 SQL Server 2005 8 September 29th, 2008 03:03 AM
WHERE Clause, LIKE, and Nulls jurio SQL Language 5 May 28th, 2007 02:53 AM
Gracefully Handling Nulls BrianWren Beginning VB 6 3 May 25th, 2005 02:06 PM
Gracefully Handling Nulls BrianWren VB.NET 2002/2003 Basics 0 May 19th, 2005 04:10 PM
looking for nulls Warbird C# 4 July 1st, 2004 07:50 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.