Results 1 to 6 of 6

Thread: VB .net program to accept Null Values

  1. #1
    Join Date
    Aug 2006
    Posts
    222

    VB .net program to accept Null Values

    I am new to the VB .NET programming language. Actually I want to ask that is there any way to write a program that can accept the null values. Even if possible tell me how to do in SQL also. I am having the following value :
    CheckString(RemoveCarriageReturns(dreader.GetString(2)))
    And I want to insert this value into a table. Also I want that this value should accept the Null Values. Can someone help me in solving my query.?? Please help me soon.
    Extremely Thanks in Advance.
    Just a reply to say thank you for these links and posts I have a lot to read and learn now!



  2. #2
    Join Date
    May 2008
    Posts
    2,297

    Re: VB .net program to accept Null Values

    When the application expects a data value and gets nothing or unexpected values then the Null or nonexistent data gets complicated. So you will have to perform coding that properly handles these situations to avoid application failure. The default value of reference-type variables is null. A null value is a value that doesn't refer to any object. That's why this requires a close examination of the data wherever and whenever it's needed. There's no VB.NET equivalent of the null keyword like in another programming language like C#. VB.NET uses the Nothing keyword. The following code is an example of the Null value :

    Dim sTest As String
    If (sTest Is Nothing) Then
    Console.WriteLine("sTest is Null")
    End If

    Also the VB.NET doesn't treat null and Nothing as equals. So the programmer that is writing a code in VB .NET will have to check the possibility for both the values.

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: VB .net program to accept Null Values

    As you said that you want the code that your value can accept the Null Value. And CheckString(RemoveCarriageReturns(dreader.GetStrin g(2))) was your value. So the code in VB .NET will be :

    dbconnection2.Open()
    dbcommand2.CommandText = "select orderitemid,qty,description,despatched from tblorderitem where orderid = " & gloOrderID
    dreader = dbcommand2.ExecuteReader
    While dreader.Read
    dbconnection.Open()
    dbcommand.CommandText = "insert into " & tmptable & " (orderitemid,qty,description,despatched,outstanding,thisdelivery) values(" & dreader.GetInt32(0) & "," & dreader.GetInt32(1) & ",'" & CheckString(RemoveCarriageReturns(dreader.GetString(2))) & "'," & dreader.GetInt32(3) & "," & dreader.GetInt32(1) - dreader.GetInt32(3) & ",0)"
    dbcommand.ExecuteNonQuery()
    dbconnection.Close()
    End While

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: VB .net program to accept Null Values

    If you want to work with null values, then the Microsoft provides a uniform method for working with null values: The base System.Convert namespace includes the DBNull object. The absence of a known value is indicated by the use of the DBNull class. This class differentiates between a null value (a null object) and an uninitialised value (the DBNull.Value instance). A null object is a valid value for a field in database applications. A table can have records with uninitialised fields. These uninitialised fields have the DBNull value. The VB.NET sample for the DBNull value will be like this :

    If (sTest.Equals(System.DBNull.Value) Then
    Console.WriteLine("sTest is Null")
    End If

  5. #5
    Join Date
    Apr 2008
    Posts
    1,948

    Re: VB .net program to accept Null Values

    You do system.dbnull, if the SQL data type is NOT a string type or numeric type. If it is numeric then 0 and if it's a string type (char, text, formatted text,) you do "". The null value applies to a very restricted group. Also before passing the value to SqlDataReader, check whether the value is "" . If it is, pass System.DBNull.Value and check. NULL values require special handling in SQL queries. NULL represents an unknown value, and strictly speaking NULL is never equal to NULL. In SQL, the Null values should be treated cautiously, particularly in ALL, ANY, IN and EXISTS queries.

  6. #6
    Join Date
    May 2008
    Posts
    2,389

    Re: VB .net program to accept Null Values

    The following code can be used to check the Null values in VB .NET :

    Public Function CheckDBNull(ByVal obj As Object, _
    Optional ByVal ObjectType As enumObjectType = enumObjectType.StrType) As Object
    Dim objReturn As Object
    objReturn = obj
    If ObjectType = enumObjectType.StrType And IsDBNull(obj) Then
    objReturn = ""
    ElseIf ObjectType = enumObjectType.IntType And IsDBNull(obj) Then
    objReturn = 0
    ElseIf ObjectType = enumObjectType.DblType And IsDBNull(obj) Then
    objReturn = 0.0
    End If
    Return objReturn
    End Function

    Before doing this main program you will have to declare the following values :

    Enum enumObjectType
    StrType = 0
    IntType = 1
    DblType = 2
    End Enum

Similar Threads

  1. W32 registry values are not getting matched by the default values
    By Angrzej in forum Networking & Security
    Replies: 5
    Last Post: 19-05-2011, 12:23 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. What is null value?
    By Swati in forum Software Development
    Replies: 3
    Last Post: 14-02-2009, 04:39 PM
  5. Handling NULL in ASP.NET
    By Techie in forum Software Development
    Replies: 3
    Last Post: 21-09-2005, 06:16 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,714,102,005.83247 seconds with 17 queries