Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET 2002/2003 Basics 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
 
Old June 20th, 2005, 06:02 AM
Authorized User
 
Join Date: Apr 2005
Location: , , .
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
Default NULL reference exception

Hey,
I keep getting the error NullReferenceException at runtime when trying to save to a database, on the database I've set the fields to accept Null values.
I've tried using the Try ... Catch ... Finally statement but no luck,
is there another way to resolve this problem?
The database is MS Access 2000.
NOTE if the field has values then it saves to the database no problem. The problem only arises when some fields are left null.

As always all help is greatly appreciated.

D

 
Old June 20th, 2005, 08:01 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Can you post your table structure and the relevant lines of code for the insert?

-Peter
 
Old June 20th, 2005, 08:28 AM
Authorized User
 
Join Date: Apr 2005
Location: , , .
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Table Structure is as follows

QuoteID AutoNumber Allow Null = False
Server Text(20) Allow Null = False
NoCPU Integer Allow Null = False
RAID Text Allow Null = True
RAIDType Text Allow Null = True

Code for Inserting into MS Access DB
Code:
Function ServerSpec( _
                ByVal server As String,  _
                ByVal noCPU As Integer,  _
                ByVal rAID As String,  _
                ByVal rAIDType As String,  _
                ) As Integer
        Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data Source=C:\Documents and Settings\quote.mdb"
        Dim dbConnection As System.Data.IDbConnection = New System.Data.OleDb.OleDbConnection(connectionString)

        Dim queryString As String = "INSERT INTO [Quotes] ([Server], [NoCPU], [RAID], [RAIDType]) VALUES (@Server, @NoCPU, @RAID, @RAIDType)"
        Dim dbCommand As System.Data.IDbCommand = New System.Data.OleDb.OleDbCommand
        dbCommand.CommandText = queryString
        dbCommand.Connection = dbConnection

        Dim dbParam_server As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
        dbParam_server.ParameterName = "@Server"
        dbParam_server.Value = server
        dbParam_server.DbType = System.Data.DbType.String
        dbCommand.Parameters.Add(dbParam_server)
        Dim dbParam_noCPU As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
        dbParam_noCPU.ParameterName = "@NoCPU"
        dbParam_noCPU.Value = noCPU
        dbParam_noCPU.DbType = System.Data.DbType.Int32
        dbCommand.Parameters.Add(dbParam_noCPU)
        Dim dbParam_rAID As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
        dbParam_rAID.ParameterName = "@RAID"
        dbParam_rAID.Value = rAID
        dbParam_rAID.DbType = System.Data.DbType.String
        dbCommand.Parameters.Add(dbParam_rAID)
        Dim dbParam_rAIDType As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
        dbParam_rAIDType.ParameterName = "@RAIDType"
        dbParam_rAIDType.Value = rAIDType
        dbParam_rAIDType.DbType = System.Data.DbType.String
        dbCommand.Parameters.Add(dbParam_rAIDType)

        Dim rowsAffected As Integer = 0
        dbConnection.Open
        Try
            rowsAffected = dbCommand.ExecuteNonQuery
        Finally
            dbConnection.Close
        End Try

        Return rowsAffected
    End Function

Sub ImageButton1_Click(sender As Object, e As ImageClickEventArgs)

ServerSpec( _
                (DropDownList1.SelectedItem.Value), _
                (DropDownList2.SelectedItem.Value), _
                (DropDownList3.SelectedItem.Value), _
                (DropDownList4.SelectedItem.Value), _
                msgbox("Quote Saved Successfully, QuoteID =" & Quote)

End Sub
 
Old June 21st, 2005, 07:15 AM
Authorized User
 
Join Date: May 2005
Location: Berlin, CT, USA.
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Looking at your table definition, the only field that is ALLOWED to be a NULL is the RAID field. If any other field is NULL value, this will cause an error when attempting to insert a record. Try setting the REST of the fields to "Allow NULL = TRUE" if you wish to allow null values.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Null reference exception was unhandled labby C# 2005 0 April 24th, 2007 06:14 AM
Null Reference Exception was unhandled labby C# 2005 1 February 9th, 2007 03:15 PM
Null reference exception when using DataAdapter Samantha Karen Webb C# 1 July 21st, 2006 09:12 AM
null exception error abhit_kumar JSP Basics 4 January 5th, 2005 05:26 PM
Null Exception while reading ajindal Classic ASP XML 3 December 14th, 2004 06:20 AM





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