Null Values stopping code module.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?R3JlZw==?=

    Null Values stopping code module.

    I have the following three files.
    1. Users.aspx is a webpage that uses the <asp:ObjectData Sourcecontrol to
    populate a simple <asp:ListBoxcon trol.
    2. The UserDetails.cs file creates a Namespace named UserComponents and
    creates an object named UserDetails.
    3. The UserDB.cs file retrieves the actual data from the database.

    The code below has been condensed and only includes two fields from my
    datasource. I actually am also
    retrieving FirstName, MiddleName, etc. This code works fine as long as no
    user has a UserName equal to
    NULL. If any of the fields I include in the UserDetails object, I get the
    following error on line 46.

    Unable to cast object of type 'System.DBNull' to type 'System.String' .

    Line 46: UserDetails user = new UserDetails((in t)rdrUsers["UserID"],
    Line 47:
    (string)rdrUser s["UserName"],

    I get the error regardless of what field is set as the <DataTextField> . If I
    make sure all records have a
    UserName not set to NULL the code works great. This can become a big problem
    because in many cases
    not every field will contain a value. I've thought I woudl just set the
    default value of each field
    to a blank space, as this will stop the error from happening, but I'm not so
    sure I want to do this.

    It seems to me this code should work with NULL values. NULL Values frustrate
    me at times. Is there a way
    I can make the code listed below work with NULL values? FYI: One of my
    thoughts was to check for a NULL
    value for each field, but this even doesn't sound like a good solution.

    Can anyone provide some insight as to how I can make the following code work
    when any of the source fields
    may contain a NULL value?

    Users.aspx (Web Page displaying listbox with UserNames)

    <asp:ObjectData Source
    ID="sourceUsers " runat="server"
    TypeName="UserC omponents.UserD B" SelectMethod="G etUsers">
    </asp:ObjectDataS ource>
    <asp:ListBox
    ID="lstUserID" runat="server" DataSourceID="s ourceUsers"
    DataTextField=" UserName" Width="224px">
    </asp:ListBox>

    UserDetails.cs (in App_Code folder)
    namespace UserComponents
    {
    public class UserDetails
    {
    public int UserID
    {
    get { return m_userID; }
    set { m_userID = value; }
    }
    public string UserName
    {
    get { return m_userName; }
    set { m_userName = value; }
    }

    public UserDetails(int userID, string userName)
    {
    this.UserID = userID;
    this.UserName = userName;
    }
    public UserDetails()
    {
    // Default Constructor
    }
    }
    }
    UserDB.cs (in App_Code folder)
    namespace UserComponents
    {
    [DataObject]
    public class UserDB
    {
    [DataObjectMetho d(DataObjectMet hodType.Select, true)]
    public List<UserDetail sGetUsers()
    {
    SqlConnection sqlConn = new SqlConnection(m _strConn);
    SqlCommand cmdUsers = new SqlCommand("sUs ers", sqlConn);
    cmdUsers.Comman dType = CommandType.Sto redProcedure;
    // Create collection of all Users.
    List<UserDetail susers = new List<UserDetail s>();
    try
    {
    sqlConn.Open();
    SqlDataReader rdrUsers = cmdUsers.Execut eReader();

    while (rdrUsers.Read( ))
    {
    UserDetails user = new UserDetails( (int)rdrUsers["UserID"],
    (string)rdrUser s["UserName"]);
    users.Add(user) ;
    }
    rdrUsers.Close( );
    return users;
    }
    catch (SqlException ex)
    {
    throw new ApplicationExce ption ("Error Occured");
    }
    finally
    {
    sqlConn.Close() ;
    }
    }
    }
    }}




  • =?Utf-8?B?Q2h1Y2sgRA==?=

    #2
    RE: Null Values stopping code module.

    You need to test the values for DBNull before using

    String UserID = "";

    if (rdrUsers["UserID"] = System.DBNull)
    {UserID= "";}
    else
    { UserID = rdrUsers["UserID"];}

    There is an IsDBNull function on the reader, but I think it only takes an
    ordinal, not a column name. I could be wrong on this. If I'm not wrong, you
    can make your own function that takes the datareader value as input, and
    returns a string using the above logic.

    "Greg" wrote:
    I have the following three files.
    1. Users.aspx is a webpage that uses the <asp:ObjectData Sourcecontrol to
    populate a simple <asp:ListBoxcon trol.
    2. The UserDetails.cs file creates a Namespace named UserComponents and
    creates an object named UserDetails.
    3. The UserDB.cs file retrieves the actual data from the database.
    >
    The code below has been condensed and only includes two fields from my
    datasource. I actually am also
    retrieving FirstName, MiddleName, etc. This code works fine as long as no
    user has a UserName equal to
    NULL. If any of the fields I include in the UserDetails object, I get the
    following error on line 46.
    >
    Unable to cast object of type 'System.DBNull' to type 'System.String' .
    >
    Line 46: UserDetails user = new UserDetails((in t)rdrUsers["UserID"],
    Line 47:
    (string)rdrUser s["UserName"],
    >
    I get the error regardless of what field is set as the <DataTextField> . If I
    make sure all records have a
    UserName not set to NULL the code works great. This can become a big problem
    because in many cases
    not every field will contain a value. I've thought I woudl just set the
    default value of each field
    to a blank space, as this will stop the error from happening, but I'm not so
    sure I want to do this.
    >
    It seems to me this code should work with NULL values. NULL Values frustrate
    me at times. Is there a way
    I can make the code listed below work with NULL values? FYI: One of my
    thoughts was to check for a NULL
    value for each field, but this even doesn't sound like a good solution.
    >
    Can anyone provide some insight as to how I can make the following code work
    when any of the source fields
    may contain a NULL value?
    >
    Users.aspx (Web Page displaying listbox with UserNames)
    >
    <asp:ObjectData Source
    ID="sourceUsers " runat="server"
    TypeName="UserC omponents.UserD B" SelectMethod="G etUsers">
    </asp:ObjectDataS ource>
    <asp:ListBox
    ID="lstUserID" runat="server" DataSourceID="s ourceUsers"
    DataTextField=" UserName" Width="224px">
    </asp:ListBox>
    >
    UserDetails.cs (in App_Code folder)
    namespace UserComponents
    {
    public class UserDetails
    {
    public int UserID
    {
    get { return m_userID; }
    set { m_userID = value; }
    }
    public string UserName
    {
    get { return m_userName; }
    set { m_userName = value; }
    }
    >
    public UserDetails(int userID, string userName)
    {
    this.UserID = userID;
    this.UserName = userName;
    }
    public UserDetails()
    {
    // Default Constructor
    }
    }
    }
    UserDB.cs (in App_Code folder)
    namespace UserComponents
    {
    [DataObject]
    public class UserDB
    {
    [DataObjectMetho d(DataObjectMet hodType.Select, true)]
    public List<UserDetail sGetUsers()
    {
    SqlConnection sqlConn = new SqlConnection(m _strConn);
    SqlCommand cmdUsers = new SqlCommand("sUs ers", sqlConn);
    cmdUsers.Comman dType = CommandType.Sto redProcedure;
    // Create collection of all Users.
    List<UserDetail susers = new List<UserDetail s>();
    try
    {
    sqlConn.Open();
    SqlDataReader rdrUsers = cmdUsers.Execut eReader();
    >
    while (rdrUsers.Read( ))
    {
    UserDetails user = new UserDetails( (int)rdrUsers["UserID"],
    (string)rdrUser s["UserName"]);
    users.Add(user) ;
    }
    rdrUsers.Close( );
    return users;
    }
    catch (SqlException ex)
    {
    throw new ApplicationExce ption ("Error Occured");
    }
    finally
    {
    sqlConn.Close() ;
    }
    }
    }
    }}
    >
    >
    >
    >

    Comment

    • =?Utf-8?B?Q2h1Y2sgRA==?=

      #3
      RE: Null Values stopping code module.

      typo alert:

      if (rdrUsers["UserID"] == System.DBNull)
      {UserID= "";}
      else
      { UserID = rdrUsers["UserID"];}


      "Chuck D" wrote:
      You need to test the values for DBNull before using
      >
      String UserID = "";
      >
      if (rdrUsers["UserID"] = System.DBNull)
      {UserID= "";}
      else
      { UserID = rdrUsers["UserID"];}
      >
      There is an IsDBNull function on the reader, but I think it only takes an
      ordinal, not a column name. I could be wrong on this. If I'm not wrong, you
      can make your own function that takes the datareader value as input, and
      returns a string using the above logic.
      >
      "Greg" wrote:
      >
      I have the following three files.
      1. Users.aspx is a webpage that uses the <asp:ObjectData Sourcecontrol to
      populate a simple <asp:ListBoxcon trol.
      2. The UserDetails.cs file creates a Namespace named UserComponents and
      creates an object named UserDetails.
      3. The UserDB.cs file retrieves the actual data from the database.

      The code below has been condensed and only includes two fields from my
      datasource. I actually am also
      retrieving FirstName, MiddleName, etc. This code works fine as long as no
      user has a UserName equal to
      NULL. If any of the fields I include in the UserDetails object, I get the
      following error on line 46.

      Unable to cast object of type 'System.DBNull' to type 'System.String' .

      Line 46: UserDetails user = new UserDetails((in t)rdrUsers["UserID"],
      Line 47:
      (string)rdrUser s["UserName"],

      I get the error regardless of what field is set as the <DataTextField> . If I
      make sure all records have a
      UserName not set to NULL the code works great. This can become a big problem
      because in many cases
      not every field will contain a value. I've thought I woudl just set the
      default value of each field
      to a blank space, as this will stop the error from happening, but I'm not so
      sure I want to do this.

      It seems to me this code should work with NULL values. NULL Values frustrate
      me at times. Is there a way
      I can make the code listed below work with NULL values? FYI: One of my
      thoughts was to check for a NULL
      value for each field, but this even doesn't sound like a good solution.

      Can anyone provide some insight as to how I can make the following code work
      when any of the source fields
      may contain a NULL value?

      Users.aspx (Web Page displaying listbox with UserNames)

      <asp:ObjectData Source
      ID="sourceUsers " runat="server"
      TypeName="UserC omponents.UserD B" SelectMethod="G etUsers">
      </asp:ObjectDataS ource>
      <asp:ListBox
      ID="lstUserID" runat="server" DataSourceID="s ourceUsers"
      DataTextField=" UserName" Width="224px">
      </asp:ListBox>

      UserDetails.cs (in App_Code folder)
      namespace UserComponents
      {
      public class UserDetails
      {
      public int UserID
      {
      get { return m_userID; }
      set { m_userID = value; }
      }
      public string UserName
      {
      get { return m_userName; }
      set { m_userName = value; }
      }

      public UserDetails(int userID, string userName)
      {
      this.UserID = userID;
      this.UserName = userName;
      }
      public UserDetails()
      {
      // Default Constructor
      }
      }
      }
      UserDB.cs (in App_Code folder)
      namespace UserComponents
      {
      [DataObject]
      public class UserDB
      {
      [DataObjectMetho d(DataObjectMet hodType.Select, true)]
      public List<UserDetail sGetUsers()
      {
      SqlConnection sqlConn = new SqlConnection(m _strConn);
      SqlCommand cmdUsers = new SqlCommand("sUs ers", sqlConn);
      cmdUsers.Comman dType = CommandType.Sto redProcedure;
      // Create collection of all Users.
      List<UserDetail susers = new List<UserDetail s>();
      try
      {
      sqlConn.Open();
      SqlDataReader rdrUsers = cmdUsers.Execut eReader();

      while (rdrUsers.Read( ))
      {
      UserDetails user = new UserDetails( (int)rdrUsers["UserID"],
      (string)rdrUser s["UserName"]);
      users.Add(user) ;
      }
      rdrUsers.Close( );
      return users;
      }
      catch (SqlException ex)
      {
      throw new ApplicationExce ption ("Error Occured");
      }
      finally
      {
      sqlConn.Close() ;
      }
      }
      }
      }}


      Comment

      • =?Utf-8?B?R3JlZw==?=

        #4
        RE: Null Values stopping code module.

        Chuck,

        I tried something along the line of what you provided. I got the same error
        that I am getting with this sample as well. Here is what I added to my source
        code. In this case, I am checking the value of a field named: "password".

        string password = "";
        if (rdrUsers["Password"] == System.DBNull)
        { password = ""; }
        else
        { password = rdrUsers["Password"]; }

        I get the following error message when I initially start my web application,
        before even getting to the page that uses this code module.

        CS0119: 'System.DBNull' is a 'type', which is not valid in the given context

        Any ideas?


        "Chuck D" wrote:
        typo alert:
        >
        if (rdrUsers["UserID"] == System.DBNull)
        {UserID= "";}
        else
        { UserID = rdrUsers["UserID"];}
        >
        >
        "Chuck D" wrote:
        >
        You need to test the values for DBNull before using

        String UserID = "";

        if (rdrUsers["UserID"] = System.DBNull)
        {UserID= "";}
        else
        { UserID = rdrUsers["UserID"];}

        There is an IsDBNull function on the reader, but I think it only takes an
        ordinal, not a column name. I could be wrong on this. If I'm not wrong, you
        can make your own function that takes the datareader value as input, and
        returns a string using the above logic.

        "Greg" wrote:
        I have the following three files.
        1. Users.aspx is a webpage that uses the <asp:ObjectData Sourcecontrol to
        populate a simple <asp:ListBoxcon trol.
        2. The UserDetails.cs file creates a Namespace named UserComponents and
        creates an object named UserDetails.
        3. The UserDB.cs file retrieves the actual data from the database.
        >
        The code below has been condensed and only includes two fields from my
        datasource. I actually am also
        retrieving FirstName, MiddleName, etc. This code works fine as long as no
        user has a UserName equal to
        NULL. If any of the fields I include in the UserDetails object, I get the
        following error on line 46.
        >
        Unable to cast object of type 'System.DBNull' to type 'System.String' .
        >
        Line 46: UserDetails user = new UserDetails((in t)rdrUsers["UserID"],
        Line 47:
        (string)rdrUser s["UserName"],
        >
        I get the error regardless of what field is set as the <DataTextField> . If I
        make sure all records have a
        UserName not set to NULL the code works great. This can become a big problem
        because in many cases
        not every field will contain a value. I've thought I woudl just set the
        default value of each field
        to a blank space, as this will stop the error from happening, but I'm not so
        sure I want to do this.
        >
        It seems to me this code should work with NULL values. NULL Values frustrate
        me at times. Is there a way
        I can make the code listed below work with NULL values? FYI: One of my
        thoughts was to check for a NULL
        value for each field, but this even doesn't sound like a good solution.
        >
        Can anyone provide some insight as to how I can make the following code work
        when any of the source fields
        may contain a NULL value?
        >
        Users.aspx (Web Page displaying listbox with UserNames)
        >
        <asp:ObjectData Source
        ID="sourceUsers " runat="server"
        TypeName="UserC omponents.UserD B" SelectMethod="G etUsers">
        </asp:ObjectDataS ource>
        <asp:ListBox
        ID="lstUserID" runat="server" DataSourceID="s ourceUsers"
        DataTextField=" UserName" Width="224px">
        </asp:ListBox>
        >
        UserDetails.cs (in App_Code folder)
        namespace UserComponents
        {
        public class UserDetails
        {
        public int UserID
        {
        get { return m_userID; }
        set { m_userID = value; }
        }
        public string UserName
        {
        get { return m_userName; }
        set { m_userName = value; }
        }
        >
        public UserDetails(int userID, string userName)
        {
        this.UserID = userID;
        this.UserName = userName;
        }
        public UserDetails()
        {
        // Default Constructor
        }
        }
        }
        UserDB.cs (in App_Code folder)
        namespace UserComponents
        {
        [DataObject]
        public class UserDB
        {
        [DataObjectMetho d(DataObjectMet hodType.Select, true)]
        public List<UserDetail sGetUsers()
        {
        SqlConnection sqlConn = new SqlConnection(m _strConn);
        SqlCommand cmdUsers = new SqlCommand("sUs ers", sqlConn);
        cmdUsers.Comman dType = CommandType.Sto redProcedure;
        // Create collection of all Users.
        List<UserDetail susers = new List<UserDetail s>();
        try
        {
        sqlConn.Open();
        SqlDataReader rdrUsers = cmdUsers.Execut eReader();
        >
        while (rdrUsers.Read( ))
        {
        UserDetails user = new UserDetails( (int)rdrUsers["UserID"],
        (string)rdrUser s["UserName"]);
        users.Add(user) ;
        }
        rdrUsers.Close( );
        return users;
        }
        catch (SqlException ex)
        {
        throw new ApplicationExce ption ("Error Occured");
        }
        finally
        {
        sqlConn.Close() ;
        }
        }
        }
        }}
        >
        >
        >
        >

        Comment

        • Ralph

          #5
          Re: Null Values stopping code module.

          On Oct 14, 3:49 pm, Greg <AccessVBA...@n ewsgroups.nospa mwrote:
          Chuck,
          >
          I tried something along the line of what you provided. I got the same error
          that I am getting with this sample as well. Here is what I added to my source
          code. In this case, I am checking the value of a field named: "password".
          >
                    string password = "";
                    if (rdrUsers["Password"] == System.DBNull)
                      { password = ""; }
                    else
                      { password = rdrUsers["Password"]; }
          >
          I get the following error message when I initially start my web application,
          before even getting to the page that uses this code module.
          >
          CS0119: 'System.DBNull' is a 'type', which is not valid in the given context
          >
          Any ideas?
          >
          >
          >
          "Chuck D" wrote:
          typo alert:
          >
           if (rdrUsers["UserID"] == System.DBNull)
                       {UserID= "";}
                       else
                       { UserID = rdrUsers["UserID"];}
          >
          "Chuck D" wrote:
          >
          You need to test the values for DBNull before using
          >
                      String UserID = "";
          >
                      if (rdrUsers["UserID"] = System.DBNull)
                      {UserID= "";}
                      else
                      { UserID = rdrUsers["UserID"];}
          >
          There is an IsDBNull function on the reader, but I think it only takes an
          ordinal, not a column name.  I could be wrong on this. If I'm not wrong, you
          can make your own function that takes the datareader value as input, and
          returns a string using the above logic.
          >
          "Greg" wrote:
          >
          I have the following three files.
          1. Users.aspx is a webpage that uses the <asp:ObjectData Sourcecontrol to
          populate a simple <asp:ListBoxcon trol.
          2. The UserDetails.cs file creates a Namespace named UserComponentsa nd
          creates an object named UserDetails.
          3. The UserDB.cs file retrieves the actual data from the database.
          >
          The code below has been condensed and only includes two fields frommy
          datasource. I actually am also
          retrieving FirstName, MiddleName, etc. This code works fine as longas no
          user has a UserName equal to
          NULL. If any of the fields I include in the UserDetails object, I get the
          following error on line 46.
          >
          Unable to cast object of type 'System.DBNull' to type 'System.String' .
          >
          Line 46:           UserDetails user = new UserDetails((in t)rdrUsers["UserID"],
          Line 47:                                              
          (string)rdrUser s["UserName"],
          >
          I get the error regardless of what field is set as the <DataTextField> . If I
          make sure all records have a
          UserName not set to NULL the code works great. This can become a big problem
          because in many cases
          not every field will contain a value. I've thought I woudl just setthe
          default value of each field
          to a blank space, as this will stop the error from happening, but I'm not so
          sure I want to do this.
          >
          It seems to me this code should work with NULL values. NULL Values frustrate
          me at times. Is there a way
          I can make the code listed below work with NULL values? FYI: One ofmy
          thoughts was to check for a NULL
          value for each field, but this even doesn't sound like a good solution.
          >
          Can anyone provide some insight as to how I can make the following code work
          when any of the source fields
          may contain a NULL value?
          >
          Users.aspx (Web Page displaying listbox with UserNames)
          >
            <asp:ObjectData Source
              ID="sourceUsers " runat="server"
              TypeName="UserC omponents.UserD B" SelectMethod="G etUsers">
            </asp:ObjectDataS ource>
            <asp:ListBox
              ID="lstUserID" runat="server" DataSourceID="s ourceUsers"
              DataTextField=" UserName" Width="224px">
            </asp:ListBox>
          >
          UserDetails.cs (in App_Code folder)
          namespace UserComponents
          {
            public class UserDetails
            {
              public int UserID
              {
                get { return m_userID; }
                set { m_userID = value; }
              }
              public string UserName
              {
                get { return m_userName; }
                set { m_userName = value; }
              }
          >
              public UserDetails(int userID, string userName)
              {
                this.UserID = userID;
                this.UserName = userName;
              }
              public UserDetails()
              {
              // Default Constructor
              }
            }
          }
          UserDB.cs (in App_Code folder)
          namespace UserComponents
          {
            [DataObject]
            public class UserDB
            {
              [DataObjectMetho d(DataObjectMet hodType.Select, true)]
              public List<UserDetail sGetUsers()
              {
                SqlConnection sqlConn = new SqlConnection(m _strConn);
                SqlCommand cmdUsers = new SqlCommand("sUs ers", sqlConn);
                cmdUsers.Comman dType = CommandType.Sto redProcedure;    
                // Create collection of all Users.
                List<UserDetail susers = new List<UserDetail s>();
                try
                {
                  sqlConn.Open();
                  SqlDataReader rdrUsers = cmdUsers.Execut eReader();
          >
                  while (rdrUsers.Read( ))
                  {
                    UserDetails user = new UserDetails( (int)rdrUsers["UserID"],
                                                       (string)rdrUser s["UserName"]);
                    users.Add(user) ;
                  }
                  rdrUsers.Close( );
                  return users;
                }
                catch (SqlException ex)
                {
                  throw new ApplicationExce ption ("Error Occured");
                }
                finally
                {
                  sqlConn.Close() ;
                }
              }
            }
          }}- Hide quoted text -
          >
          - Show quoted text -
          System.DBNull.v alue

          Comment

          • gerry

            #6
            Re: Null Values stopping code module.

            use rdrUsers["UserID"] == System.DBNull.V alue
            or use System.DBNull.E quals( rdrUsers["UserID"] )


            "Greg" <AccessVBAnet@n ewsgroups.nospa mwrote in message
            news:8C683E28-993B-4B4B-A6C1-99661C5FEB9E@mi crosoft.com...
            Chuck,
            >
            I tried something along the line of what you provided. I got the same
            error
            that I am getting with this sample as well. Here is what I added to my
            source
            code. In this case, I am checking the value of a field named: "password".
            >
            string password = "";
            if (rdrUsers["Password"] == System.DBNull)
            { password = ""; }
            else
            { password = rdrUsers["Password"]; }
            >
            I get the following error message when I initially start my web
            application,
            before even getting to the page that uses this code module.
            >
            CS0119: 'System.DBNull' is a 'type', which is not valid in the given
            context
            >
            Any ideas?
            >
            >
            "Chuck D" wrote:
            >
            >typo alert:
            >>
            > if (rdrUsers["UserID"] == System.DBNull)
            > {UserID= "";}
            > else
            > { UserID = rdrUsers["UserID"];}
            >>
            >>
            >"Chuck D" wrote:
            >>
            You need to test the values for DBNull before using
            >
            String UserID = "";
            >
            if (rdrUsers["UserID"] = System.DBNull)
            {UserID= "";}
            else
            { UserID = rdrUsers["UserID"];}
            >
            There is an IsDBNull function on the reader, but I think it only takes
            an
            ordinal, not a column name. I could be wrong on this. If I'm not
            wrong, you
            can make your own function that takes the datareader value as input,
            and
            returns a string using the above logic.
            >
            "Greg" wrote:
            >
            I have the following three files.
            1. Users.aspx is a webpage that uses the <asp:ObjectData Source>
            control to
            populate a simple <asp:ListBoxcon trol.
            2. The UserDetails.cs file creates a Namespace named UserComponents
            and
            creates an object named UserDetails.
            3. The UserDB.cs file retrieves the actual data from the database.
            >
            The code below has been condensed and only includes two fields from
            my
            datasource. I actually am also
            retrieving FirstName, MiddleName, etc. This code works fine as long
            as no
            user has a UserName equal to
            NULL. If any of the fields I include in the UserDetails object, I get
            the
            following error on line 46.
            >
            Unable to cast object of type 'System.DBNull' to type
            'System.String' .
            >
            Line 46: UserDetails user = new
            UserDetails((in t)rdrUsers["UserID"],
            Line 47:
            (string)rdrUser s["UserName"],
            >
            I get the error regardless of what field is set as the
            <DataTextField> . If I
            make sure all records have a
            UserName not set to NULL the code works great. This can become a big
            problem
            because in many cases
            not every field will contain a value. I've thought I woudl just set
            the
            default value of each field
            to a blank space, as this will stop the error from happening, but I'm
            not so
            sure I want to do this.
            >
            It seems to me this code should work with NULL values. NULL Values
            frustrate
            me at times. Is there a way
            I can make the code listed below work with NULL values? FYI: One of
            my
            thoughts was to check for a NULL
            value for each field, but this even doesn't sound like a good
            solution.
            >
            Can anyone provide some insight as to how I can make the following
            code work
            when any of the source fields
            may contain a NULL value?
            >
            Users.aspx (Web Page displaying listbox with UserNames)
            >
            <asp:ObjectData Source
            ID="sourceUsers " runat="server"
            TypeName="UserC omponents.UserD B" SelectMethod="G etUsers">
            </asp:ObjectDataS ource>
            <asp:ListBox
            ID="lstUserID" runat="server" DataSourceID="s ourceUsers"
            DataTextField=" UserName" Width="224px">
            </asp:ListBox>
            >
            UserDetails.cs (in App_Code folder)
            namespace UserComponents
            {
            public class UserDetails
            {
            public int UserID
            {
            get { return m_userID; }
            set { m_userID = value; }
            }
            public string UserName
            {
            get { return m_userName; }
            set { m_userName = value; }
            }
            >
            public UserDetails(int userID, string userName)
            {
            this.UserID = userID;
            this.UserName = userName;
            }
            public UserDetails()
            {
            // Default Constructor
            }
            }
            }
            UserDB.cs (in App_Code folder)
            namespace UserComponents
            {
            [DataObject]
            public class UserDB
            {
            [DataObjectMetho d(DataObjectMet hodType.Select, true)]
            public List<UserDetail sGetUsers()
            {
            SqlConnection sqlConn = new SqlConnection(m _strConn);
            SqlCommand cmdUsers = new SqlCommand("sUs ers", sqlConn);
            cmdUsers.Comman dType = CommandType.Sto redProcedure;
            // Create collection of all Users.
            List<UserDetail susers = new List<UserDetail s>();
            try
            {
            sqlConn.Open();
            SqlDataReader rdrUsers = cmdUsers.Execut eReader();
            >
            while (rdrUsers.Read( ))
            {
            UserDetails user = new UserDetails(
            (int)rdrUsers["UserID"],
            >
            (string)rdrUser s["UserName"]);
            users.Add(user) ;
            }
            rdrUsers.Close( );
            return users;
            }
            catch (SqlException ex)
            {
            throw new ApplicationExce ption ("Error Occured");
            }
            finally
            {
            sqlConn.Close() ;
            }
            }
            }
            }}
            >
            >
            >
            >

            Comment

            • =?Utf-8?B?Q2h1Y2sgRA==?=

              #7
              RE: Null Values stopping code module.

              My Bad, should be:

              The compiler is correctly reporting that we were trying to compare a value
              to a type.

              Chuck
              string password = "";
              if (rdrUsers["Password"] == System.DBNull.V alue)
              { password = ""; }
              else
              { password = rdrUsers["Password"]; }

              "Greg" wrote:
              Chuck,
              >
              I tried something along the line of what you provided. I got the same error
              that I am getting with this sample as well. Here is what I added to my source
              code. In this case, I am checking the value of a field named: "password".
              >
              string password = "";
              if (rdrUsers["Password"] == System.DBNull)
              { password = ""; }
              else
              { password = rdrUsers["Password"]; }
              >
              I get the following error message when I initially start my web application,
              before even getting to the page that uses this code module.
              >
              CS0119: 'System.DBNull' is a 'type', which is not valid in the given context
              >
              Any ideas?
              >
              >
              "Chuck D" wrote:
              >
              typo alert:

              if (rdrUsers["UserID"] == System.DBNull)
              {UserID= "";}
              else
              { UserID = rdrUsers["UserID"];}


              "Chuck D" wrote:
              You need to test the values for DBNull before using
              >
              String UserID = "";
              >
              if (rdrUsers["UserID"] = System.DBNull)
              {UserID= "";}
              else
              { UserID = rdrUsers["UserID"];}
              >
              There is an IsDBNull function on the reader, but I think it only takes an
              ordinal, not a column name. I could be wrong on this. If I'm not wrong, you
              can make your own function that takes the datareader value as input, and
              returns a string using the above logic.
              >
              "Greg" wrote:
              >
              I have the following three files.
              1. Users.aspx is a webpage that uses the <asp:ObjectData Sourcecontrol to
              populate a simple <asp:ListBoxcon trol.
              2. The UserDetails.cs file creates a Namespace named UserComponents and
              creates an object named UserDetails.
              3. The UserDB.cs file retrieves the actual data from the database.

              The code below has been condensed and only includes two fields from my
              datasource. I actually am also
              retrieving FirstName, MiddleName, etc. This code works fine as long as no
              user has a UserName equal to
              NULL. If any of the fields I include in the UserDetails object, I get the
              following error on line 46.

              Unable to cast object of type 'System.DBNull' to type 'System.String' .

              Line 46: UserDetails user = new UserDetails((in t)rdrUsers["UserID"],
              Line 47:
              (string)rdrUser s["UserName"],

              I get the error regardless of what field is set as the <DataTextField> . If I
              make sure all records have a
              UserName not set to NULL the code works great. This can become a big problem
              because in many cases
              not every field will contain a value. I've thought I woudl just set the
              default value of each field
              to a blank space, as this will stop the error from happening, but I'm not so
              sure I want to do this.

              It seems to me this code should work with NULL values. NULL Values frustrate
              me at times. Is there a way
              I can make the code listed below work with NULL values? FYI: One of my
              thoughts was to check for a NULL
              value for each field, but this even doesn't sound like a good solution.

              Can anyone provide some insight as to how I can make the following code work
              when any of the source fields
              may contain a NULL value?

              Users.aspx (Web Page displaying listbox with UserNames)

              <asp:ObjectData Source
              ID="sourceUsers " runat="server"
              TypeName="UserC omponents.UserD B" SelectMethod="G etUsers">
              </asp:ObjectDataS ource>
              <asp:ListBox
              ID="lstUserID" runat="server" DataSourceID="s ourceUsers"
              DataTextField=" UserName" Width="224px">
              </asp:ListBox>

              UserDetails.cs (in App_Code folder)
              namespace UserComponents
              {
              public class UserDetails
              {
              public int UserID
              {
              get { return m_userID; }
              set { m_userID = value; }
              }
              public string UserName
              {
              get { return m_userName; }
              set { m_userName = value; }
              }

              public UserDetails(int userID, string userName)
              {
              this.UserID = userID;
              this.UserName = userName;
              }
              public UserDetails()
              {
              // Default Constructor
              }
              }
              }
              UserDB.cs (in App_Code folder)
              namespace UserComponents
              {
              [DataObject]
              public class UserDB
              {
              [DataObjectMetho d(DataObjectMet hodType.Select, true)]
              public List<UserDetail sGetUsers()
              {
              SqlConnection sqlConn = new SqlConnection(m _strConn);
              SqlCommand cmdUsers = new SqlCommand("sUs ers", sqlConn);
              cmdUsers.Comman dType = CommandType.Sto redProcedure;
              // Create collection of all Users.
              List<UserDetail susers = new List<UserDetail s>();
              try
              {
              sqlConn.Open();
              SqlDataReader rdrUsers = cmdUsers.Execut eReader();

              while (rdrUsers.Read( ))
              {
              UserDetails user = new UserDetails( (int)rdrUsers["UserID"],
              (string)rdrUser s["UserName"]);
              users.Add(user) ;
              }
              rdrUsers.Close( );
              return users;
              }
              catch (SqlException ex)
              {
              throw new ApplicationExce ption ("Error Occured");
              }
              finally
              {
              sqlConn.Close() ;
              }
              }
              }
              }}


              Comment

              • Jeff Johnson

                #8
                Re: Null Values stopping code module.

                "Chuck D" <ChuckD@discuss ions.microsoft. comwrote in message
                news:D9D8AE38-7C31-4DE5-B7E5-1A833A09C918@mi crosoft.com...
                string password = "";
                if (rdrUsers["Password"] == System.DBNull.V alue)
                { password = ""; }
                else
                { password = rdrUsers["Password"]; }
                Since you've already set password to the empty string on declaration, why
                not only set it again if you have a value:

                string password = "";
                if (rdrUsers["Password"] != System.DBNull.V alue)
                {
                password = rdrUsers["Password"].ToString();
                }

                And isn't the ToString() required as well?


                Comment

                • =?Utf-8?B?R3JlZw==?=

                  #9
                  RE: Null Values stopping code module.

                  That does the trick. Thanks so much.

                  "Chuck D" wrote:
                  My Bad, should be:
                  >
                  The compiler is correctly reporting that we were trying to compare a value
                  to a type.
                  >
                  Chuck
                  string password = "";
                  if (rdrUsers["Password"] == System.DBNull.V alue)
                  { password = ""; }
                  else
                  { password = rdrUsers["Password"]; }
                  >
                  "Greg" wrote:
                  >
                  Chuck,

                  I tried something along the line of what you provided. I got the same error
                  that I am getting with this sample as well. Here is what I added to my source
                  code. In this case, I am checking the value of a field named: "password".

                  string password = "";
                  if (rdrUsers["Password"] == System.DBNull)
                  { password = ""; }
                  else
                  { password = rdrUsers["Password"]; }

                  I get the following error message when I initially start my web application,
                  before even getting to the page that uses this code module.

                  CS0119: 'System.DBNull' is a 'type', which is not valid in the given context

                  Any ideas?


                  "Chuck D" wrote:
                  typo alert:
                  >
                  if (rdrUsers["UserID"] == System.DBNull)
                  {UserID= "";}
                  else
                  { UserID = rdrUsers["UserID"];}
                  >
                  >
                  "Chuck D" wrote:
                  >
                  You need to test the values for DBNull before using

                  String UserID = "";

                  if (rdrUsers["UserID"] = System.DBNull)
                  {UserID= "";}
                  else
                  { UserID = rdrUsers["UserID"];}

                  There is an IsDBNull function on the reader, but I think it only takes an
                  ordinal, not a column name. I could be wrong on this. If I'm not wrong, you
                  can make your own function that takes the datareader value as input, and
                  returns a string using the above logic.

                  "Greg" wrote:

                  I have the following three files.
                  1. Users.aspx is a webpage that uses the <asp:ObjectData Sourcecontrol to
                  populate a simple <asp:ListBoxcon trol.
                  2. The UserDetails.cs file creates a Namespace named UserComponents and
                  creates an object named UserDetails.
                  3. The UserDB.cs file retrieves the actual data from the database.
                  >
                  The code below has been condensed and only includes two fields from my
                  datasource. I actually am also
                  retrieving FirstName, MiddleName, etc. This code works fine as long as no
                  user has a UserName equal to
                  NULL. If any of the fields I include in the UserDetails object, I get the
                  following error on line 46.
                  >
                  Unable to cast object of type 'System.DBNull' to type 'System.String' .
                  >
                  Line 46: UserDetails user = new UserDetails((in t)rdrUsers["UserID"],
                  Line 47:
                  (string)rdrUser s["UserName"],
                  >
                  I get the error regardless of what field is set as the <DataTextField> . If I
                  make sure all records have a
                  UserName not set to NULL the code works great. This can become a big problem
                  because in many cases
                  not every field will contain a value. I've thought I woudl just set the
                  default value of each field
                  to a blank space, as this will stop the error from happening, but I'm not so
                  sure I want to do this.
                  >
                  It seems to me this code should work with NULL values. NULL Values frustrate
                  me at times. Is there a way
                  I can make the code listed below work with NULL values? FYI: One of my
                  thoughts was to check for a NULL
                  value for each field, but this even doesn't sound like a good solution.
                  >
                  Can anyone provide some insight as to how I can make the following code work
                  when any of the source fields
                  may contain a NULL value?
                  >
                  Users.aspx (Web Page displaying listbox with UserNames)
                  >
                  <asp:ObjectData Source
                  ID="sourceUsers " runat="server"
                  TypeName="UserC omponents.UserD B" SelectMethod="G etUsers">
                  </asp:ObjectDataS ource>
                  <asp:ListBox
                  ID="lstUserID" runat="server" DataSourceID="s ourceUsers"
                  DataTextField=" UserName" Width="224px">
                  </asp:ListBox>
                  >
                  UserDetails.cs (in App_Code folder)
                  namespace UserComponents
                  {
                  public class UserDetails
                  {
                  public int UserID
                  {
                  get { return m_userID; }
                  set { m_userID = value; }
                  }
                  public string UserName
                  {
                  get { return m_userName; }
                  set { m_userName = value; }
                  }
                  >
                  public UserDetails(int userID, string userName)
                  {
                  this.UserID = userID;
                  this.UserName = userName;
                  }
                  public UserDetails()
                  {
                  // Default Constructor
                  }
                  }
                  }
                  UserDB.cs (in App_Code folder)
                  namespace UserComponents
                  {
                  [DataObject]
                  public class UserDB
                  {
                  [DataObjectMetho d(DataObjectMet hodType.Select, true)]
                  public List<UserDetail sGetUsers()
                  {
                  SqlConnection sqlConn = new SqlConnection(m _strConn);
                  SqlCommand cmdUsers = new SqlCommand("sUs ers", sqlConn);
                  cmdUsers.Comman dType = CommandType.Sto redProcedure;
                  // Create collection of all Users.
                  List<UserDetail susers = new List<UserDetail s>();
                  try
                  {
                  sqlConn.Open();
                  SqlDataReader rdrUsers = cmdUsers.Execut eReader();
                  >
                  while (rdrUsers.Read( ))
                  {
                  UserDetails user = new UserDetails( (int)rdrUsers["UserID"],
                  (string)rdrUser s["UserName"]);
                  users.Add(user) ;
                  }
                  rdrUsers.Close( );
                  return users;
                  }
                  catch (SqlException ex)
                  {
                  throw new ApplicationExce ption ("Error Occured");
                  }
                  finally
                  {
                  sqlConn.Close() ;
                  }
                  }
                  }
                  }}
                  >
                  >
                  >
                  >

                  Comment

                  • =?Utf-8?B?R3JlZw==?=

                    #10
                    RE: Null Values stopping code module.

                    In my Code Behind file this code works great. I'm thinking down the road I'd
                    like this to be handled in my Data/Property Class instead, thus I would not
                    have to always worry about these situations on every page. I think in the
                    long run this would be a huge task to have to handle on every single page.

                    So, I tried to do the following, thinking it will return "---" when the
                    value is NULL.

                    public string Prefix
                    {
                    get { return m_prefix; }
                    set
                    {
                    if (value == System.DBNull.V alue)
                    { m_prefix = "---"; }
                    else
                    { m_prefix = value; }
                    }
                    }

                    But, I cannot do this as it reports an error. It states that the
                    System.DBNull.V alue is a field but is treated a a "type"

                    Is it possible for me to handle this situation like I want? Or, do I need to
                    stick with what you've originally proposed?

                    Thanks again for you help.

                    "Chuck D" wrote:
                    My Bad, should be:
                    >
                    The compiler is correctly reporting that we were trying to compare a value
                    to a type.
                    >
                    Chuck
                    string password = "";
                    if (rdrUsers["Password"] == System.DBNull.V alue)
                    { password = ""; }
                    else
                    { password = rdrUsers["Password"]; }
                    >
                    "Greg" wrote:
                    >
                    Chuck,

                    I tried something along the line of what you provided. I got the same error
                    that I am getting with this sample as well. Here is what I added to my source
                    code. In this case, I am checking the value of a field named: "password".

                    string password = "";
                    if (rdrUsers["Password"] == System.DBNull)
                    { password = ""; }
                    else
                    { password = rdrUsers["Password"]; }

                    I get the following error message when I initially start my web application,
                    before even getting to the page that uses this code module.

                    CS0119: 'System.DBNull' is a 'type', which is not valid in the given context

                    Any ideas?


                    "Chuck D" wrote:
                    typo alert:
                    >
                    if (rdrUsers["UserID"] == System.DBNull)
                    {UserID= "";}
                    else
                    { UserID = rdrUsers["UserID"];}
                    >
                    >
                    "Chuck D" wrote:
                    >
                    You need to test the values for DBNull before using

                    String UserID = "";

                    if (rdrUsers["UserID"] = System.DBNull)
                    {UserID= "";}
                    else
                    { UserID = rdrUsers["UserID"];}

                    There is an IsDBNull function on the reader, but I think it only takes an
                    ordinal, not a column name. I could be wrong on this. If I'm not wrong, you
                    can make your own function that takes the datareader value as input, and
                    returns a string using the above logic.

                    "Greg" wrote:

                    I have the following three files.
                    1. Users.aspx is a webpage that uses the <asp:ObjectData Sourcecontrol to
                    populate a simple <asp:ListBoxcon trol.
                    2. The UserDetails.cs file creates a Namespace named UserComponents and
                    creates an object named UserDetails.
                    3. The UserDB.cs file retrieves the actual data from the database.
                    >
                    The code below has been condensed and only includes two fields from my
                    datasource. I actually am also
                    retrieving FirstName, MiddleName, etc. This code works fine as long as no
                    user has a UserName equal to
                    NULL. If any of the fields I include in the UserDetails object, I get the
                    following error on line 46.
                    >
                    Unable to cast object of type 'System.DBNull' to type 'System.String' .
                    >
                    Line 46: UserDetails user = new UserDetails((in t)rdrUsers["UserID"],
                    Line 47:
                    (string)rdrUser s["UserName"],
                    >
                    I get the error regardless of what field is set as the <DataTextField> . If I
                    make sure all records have a
                    UserName not set to NULL the code works great. This can become a big problem
                    because in many cases
                    not every field will contain a value. I've thought I woudl just set the
                    default value of each field
                    to a blank space, as this will stop the error from happening, but I'm not so
                    sure I want to do this.
                    >
                    It seems to me this code should work with NULL values. NULL Values frustrate
                    me at times. Is there a way
                    I can make the code listed below work with NULL values? FYI: One of my
                    thoughts was to check for a NULL
                    value for each field, but this even doesn't sound like a good solution.
                    >
                    Can anyone provide some insight as to how I can make the following code work
                    when any of the source fields
                    may contain a NULL value?
                    >
                    Users.aspx (Web Page displaying listbox with UserNames)
                    >
                    <asp:ObjectData Source
                    ID="sourceUsers " runat="server"
                    TypeName="UserC omponents.UserD B" SelectMethod="G etUsers">
                    </asp:ObjectDataS ource>
                    <asp:ListBox
                    ID="lstUserID" runat="server" DataSourceID="s ourceUsers"
                    DataTextField=" UserName" Width="224px">
                    </asp:ListBox>
                    >
                    UserDetails.cs (in App_Code folder)
                    namespace UserComponents
                    {
                    public class UserDetails
                    {
                    public int UserID
                    {
                    get { return m_userID; }
                    set { m_userID = value; }
                    }
                    public string UserName
                    {
                    get { return m_userName; }
                    set { m_userName = value; }
                    }
                    >
                    public UserDetails(int userID, string userName)
                    {
                    this.UserID = userID;
                    this.UserName = userName;
                    }
                    public UserDetails()
                    {
                    // Default Constructor
                    }
                    }
                    }
                    UserDB.cs (in App_Code folder)
                    namespace UserComponents
                    {
                    [DataObject]
                    public class UserDB
                    {
                    [DataObjectMetho d(DataObjectMet hodType.Select, true)]
                    public List<UserDetail sGetUsers()
                    {
                    SqlConnection sqlConn = new SqlConnection(m _strConn);
                    SqlCommand cmdUsers = new SqlCommand("sUs ers", sqlConn);
                    cmdUsers.Comman dType = CommandType.Sto redProcedure;
                    // Create collection of all Users.
                    List<UserDetail susers = new List<UserDetail s>();
                    try
                    {
                    sqlConn.Open();
                    SqlDataReader rdrUsers = cmdUsers.Execut eReader();
                    >
                    while (rdrUsers.Read( ))
                    {
                    UserDetails user = new UserDetails( (int)rdrUsers["UserID"],
                    (string)rdrUser s["UserName"]);
                    users.Add(user) ;
                    }
                    rdrUsers.Close( );
                    return users;
                    }
                    catch (SqlException ex)
                    {
                    throw new ApplicationExce ption ("Error Occured");
                    }
                    finally
                    {
                    sqlConn.Close() ;
                    }
                    }
                    }
                    }}
                    >
                    >
                    >
                    >

                    Comment

                    • =?Utf-8?B?Q2h1Y2sgRA==?=

                      #11
                      RE: Null Values stopping code module.

                      Hi Greg,

                      Obviously I'm not the sharpest tack in the box, as it took me three posts to
                      get you the right code to do a null test before using a datareader value.

                      I will say this though...

                      System.DBNull.V alue is a data tier concept, and as such should not percolate
                      up into your classes property setters.

                      The safest path is to not pull the trigger on a set for a property at all if
                      the value is System.DBNull.V alue. Going down the road of introducing your own
                      concept of null '---' is not the way to go.

                      Chuck

                      "Greg" wrote:
                      In my Code Behind file this code works great. I'm thinking down the road I'd
                      like this to be handled in my Data/Property Class instead, thus I would not
                      have to always worry about these situations on every page. I think in the
                      long run this would be a huge task to have to handle on every single page.
                      >
                      So, I tried to do the following, thinking it will return "---" when the
                      value is NULL.
                      >
                      public string Prefix
                      {
                      get { return m_prefix; }
                      set
                      {
                      if (value == System.DBNull.V alue)
                      { m_prefix = "---"; }
                      else
                      { m_prefix = value; }
                      }
                      }
                      >
                      But, I cannot do this as it reports an error. It states that the
                      System.DBNull.V alue is a field but is treated a a "type"
                      >
                      Is it possible for me to handle this situation like I want? Or, do I need to
                      stick with what you've originally proposed?
                      >
                      Thanks again for you help.
                      >
                      "Chuck D" wrote:
                      >
                      My Bad, should be:

                      The compiler is correctly reporting that we were trying to compare a value
                      to a type.

                      Chuck
                      string password = "";
                      if (rdrUsers["Password"] == System.DBNull.V alue)
                      { password = ""; }
                      else
                      { password = rdrUsers["Password"]; }

                      "Greg" wrote:
                      Chuck,
                      >
                      I tried something along the line of what you provided. I got the same error
                      that I am getting with this sample as well. Here is what I added to my source
                      code. In this case, I am checking the value of a field named: "password".
                      >
                      string password = "";
                      if (rdrUsers["Password"] == System.DBNull)
                      { password = ""; }
                      else
                      { password = rdrUsers["Password"]; }
                      >
                      I get the following error message when I initially start my web application,
                      before even getting to the page that uses this code module.
                      >
                      CS0119: 'System.DBNull' is a 'type', which is not valid in the given context
                      >
                      Any ideas?
                      >
                      >
                      "Chuck D" wrote:
                      >
                      typo alert:

                      if (rdrUsers["UserID"] == System.DBNull)
                      {UserID= "";}
                      else
                      { UserID = rdrUsers["UserID"];}


                      "Chuck D" wrote:

                      You need to test the values for DBNull before using
                      >
                      String UserID = "";
                      >
                      if (rdrUsers["UserID"] = System.DBNull)
                      {UserID= "";}
                      else
                      { UserID = rdrUsers["UserID"];}
                      >
                      There is an IsDBNull function on the reader, but I think it only takes an
                      ordinal, not a column name. I could be wrong on this. If I'm not wrong, you
                      can make your own function that takes the datareader value as input, and
                      returns a string using the above logic.
                      >
                      "Greg" wrote:
                      >
                      I have the following three files.
                      1. Users.aspx is a webpage that uses the <asp:ObjectData Sourcecontrol to
                      populate a simple <asp:ListBoxcon trol.
                      2. The UserDetails.cs file creates a Namespace named UserComponents and
                      creates an object named UserDetails.
                      3. The UserDB.cs file retrieves the actual data from the database.

                      The code below has been condensed and only includes two fields from my
                      datasource. I actually am also
                      retrieving FirstName, MiddleName, etc. This code works fine as long as no
                      user has a UserName equal to
                      NULL. If any of the fields I include in the UserDetails object, I get the
                      following error on line 46.

                      Unable to cast object of type 'System.DBNull' to type 'System.String' .

                      Line 46: UserDetails user = new UserDetails((in t)rdrUsers["UserID"],
                      Line 47:
                      (string)rdrUser s["UserName"],

                      I get the error regardless of what field is set as the <DataTextField> . If I
                      make sure all records have a
                      UserName not set to NULL the code works great. This can become a big problem
                      because in many cases
                      not every field will contain a value. I've thought I woudl just set the
                      default value of each field
                      to a blank space, as this will stop the error from happening, but I'm not so
                      sure I want to do this.

                      It seems to me this code should work with NULL values. NULL Values frustrate
                      me at times. Is there a way
                      I can make the code listed below work with NULL values? FYI: One of my
                      thoughts was to check for a NULL
                      value for each field, but this even doesn't sound like a good solution.

                      Can anyone provide some insight as to how I can make the following code work
                      when any of the source fields
                      may contain a NULL value?

                      Users.aspx (Web Page displaying listbox with UserNames)

                      <asp:ObjectData Source
                      ID="sourceUsers " runat="server"
                      TypeName="UserC omponents.UserD B" SelectMethod="G etUsers">
                      </asp:ObjectDataS ource>
                      <asp:ListBox
                      ID="lstUserID" runat="server" DataSourceID="s ourceUsers"
                      DataTextField=" UserName" Width="224px">
                      </asp:ListBox>

                      UserDetails.cs (in App_Code folder)
                      namespace UserComponents
                      {
                      public class UserDetails
                      {
                      public int UserID
                      {
                      get { return m_userID; }
                      set { m_userID = value; }
                      }
                      public string UserName
                      {
                      get { return m_userName; }
                      set { m_userName = value; }
                      }

                      public UserDetails(int userID, string userName)
                      {
                      this.UserID = userID;
                      this.UserName = userName;
                      }
                      public UserDetails()
                      {
                      // Default Constructor
                      }
                      }
                      }
                      UserDB.cs (in App_Code folder)
                      namespace UserComponents
                      {
                      [DataObject]
                      public class UserDB
                      {
                      [DataObjectMetho d(DataObjectMet hodType.Select, true)]
                      public List<UserDetail sGetUsers()
                      {
                      SqlConnection sqlConn = new SqlConnection(m _strConn);
                      SqlCommand cmdUsers = new SqlCommand("sUs ers", sqlConn);
                      cmdUsers.Comman dType = CommandType.Sto redProcedure;
                      // Create collection of all Users.
                      List<UserDetail susers = new List<UserDetail s>();
                      try
                      {
                      sqlConn.Open();
                      SqlDataReader rdrUsers = cmdUsers.Execut eReader();

                      while (rdrUsers.Read( ))
                      {
                      UserDetails user = new UserDetails( (int)rdrUsers["UserID"],
                      (string)rdrUser s["UserName"]);
                      users.Add(user) ;
                      }
                      rdrUsers.Close( );
                      return users;
                      }
                      catch (SqlException ex)
                      {
                      throw new ApplicationExce ption ("Error Occured");
                      }
                      finally
                      {
                      sqlConn.Close() ;
                      }
                      }
                      }
                      }}


                      Comment

                      Working...