Results 1 to 4 of 4

Thread: Handling NULL in ASP.NET

  1. #1
    Join Date
    Aug 2004
    Location
    Ahmedabad , Gujarat
    Posts
    6

    Handling NULL in ASP.NET

    HI

    I am using PgSQL as Database. Front End is asp.net.

    I am reading values from database using Reader classes. For example

    Dim x as string
    x= memberReader.getString(0)

    Now if the value of memberReader.getString(0) is null then its throwing an error.

    Error is like "Object cannot be cast from DBNull to other types". Obviously it will give an error.

    So how do I check whether the incoming value is null or not.

    Not getting much help from Gooooogle.

    Help me as soon as possible

    thnx in advance

  2. #2
    Join Date
    May 2004
    Location
    127.0.0.1:chennai
    Posts
    37
    Handling null values returned from database resultsets is different in .NET from VB6.

    this function returns that the value os null
    Code:
    Public Function IsDBNull(ByVal dbvalue) As Boolean
         Return dbvalue Is DBNull.Value 
    End Function
    this function changes thevalue from null to ""
    Code:
    Public Function FixNull(ByVal dbvalue) As String
            If dbvalue Is DBNull.Value Then
                Return ""
            Else
                'NOTE: This will cast value to string if
                 'it isn't a string.
    
                Return dbvalue.ToString
            End If
    End Function
    Keep Clicking,
    Bosky

  3. #3
    Join Date
    Aug 2004
    Location
    Ahmedabad , Gujarat
    Posts
    6

    question

    Hi

    Still I have not got the solution.

    I created the function as you suggested

    Code:
    Public Function IsDBNull(ByVal dbvalue) As Boolean
       Return dbvalue Is DBNull.Value 
    End Function
    And I am using that function to check whether the data that I have read is NULL or not

    Code:
            Dim BirthDate as new Date
            Dim memberConnection As New NpgsqlConnection(StudentSpace.Global.ConString)
            Dim memberCommand As NpgsqlCommand
            Dim memberReader As NpgsqlDataReader
    
            id = uId
    
            memberCommand = New NpgsqlCommand("select BirthDate from Student where UserID=:ID", memberConnection)
            memberCommand.Parameters.Add(":ID", NpgsqlTypes.NpgsqlDbType.Integer).Value = id
            memberConnection.Open()
            memberReader = memberCommand.ExecuteReader()
            memberReader.Read()
    
            If Not IsDBNull(memberReader.GetDateTime(0)) Then ' Ur Function Call
                BirthDate = memberReader.GetDateTime(0)
            End If
    Then also I am getting the error

    Error is Object cannot be cast from DBNull to other types.

    What should I do

  4. #4
    Join Date
    Sep 2005
    Location
    India
    Posts
    3

    Re: Handling NULL in ASP.NET

    Hello,

    i am also a lover for asp.net and happy to meet friends like you.

    I suggest you to ask few questions on www.asp.net

    if you are seriously planning to study, you should atleast visit here twice
    a week.

    plus i also suggest you to go for www.aspalliance.com

    just watch out for asp.net 2 on 7th november. It will change your life
    and the way you have been programming so far.

    dotnetguy

Similar Threads

  1. Dereferencing NULL pointer in C++
    By TO-Phir in forum Software Development
    Replies: 4
    Last Post: 26-06-2011, 07:42 PM
  2. How to Use NULL in Microsoft Access?
    By Soumen in forum Software Development
    Replies: 4
    Last Post: 04-02-2010, 05:17 AM
  3. How to Check Null Value in C#?
    By sivaranjan in forum Software Development
    Replies: 4
    Last Post: 04-02-2010, 04:49 AM
  4. Assigning null value to value type in c#: Is it possible?
    By Sheenas in forum Software Development
    Replies: 5
    Last Post: 27-11-2009, 08:35 AM
  5. What is null value?
    By Swati in forum Software Development
    Replies: 3
    Last Post: 14-02-2009, 04:39 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,506,588.75402 seconds with 16 queries