Wrox Programmer Forums
|
BOOK: Beginning Visual Basic 2005 Databases ISBN: 978-0-7645-8894-5
This is the forum to discuss the Wrox book Beginning Visual Basic 2005 Databases by Thearon Willis; ISBN: 9780764588945
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Visual Basic 2005 Databases ISBN: 978-0-7645-8894-5 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 May 6th, 2009, 11:31 AM
Authorized User
 
Join Date: Aug 2005
Posts: 88
Thanks: 4
Thanked 0 Times in 0 Posts
Default DataGrid and the WDA Base Class

I am trying to use the WDABase class with the DataGrid example in chapter 5 but am having a problem getting it to work. The following example is slightly different then the example in chapter 5. The code throws the error "Object reference not set to an instance of an object." at the line "objData.Command.CommandText = "Query1" in the event NonParameterQuery"

Code:
 
PublicClass Form1
Private Sub PopulateGrid()
'Initialize a new instance of the WDABase data class
Using objData As New WDABase
Try
'Populate the DataSet
objData.DataAdapter.Fill(objData.DataSet, "DataGrid")
'Bind the data to the dataset
dgrd1.DataSource = objData.DataSet
'The data member is the table name
dgrd1.DataMember = "tGrid"
'Set the Alternating RowsDefaultCellStyle.BackColor
dgrd1.AlternatingRowsDefaultCellStyle.BackColor = Color.WhiteSmoke
'Set theCellBorderStyle property
dgrd1.CellBorderStyle = DataGridViewCellBorderStyle.Raised
'Set the SelectionMode property
dgrd1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
'Set the AutoSizeColumnsMode property
dgrd1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
'Center align the State columnn
dgrd1.Columns("State").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Using
End Sub
Private Sub NonParameterQuery()
'Initialize a new instance of the WDABase data class
Using objData As New WDABase
objData.Command.CommandText = "Query1"
objData.Command.CommandType = CommandType.StoredProcedure
objData.Command.Connection = objData.Connection
'PopulateGrid the data grid
PopulateGrid()
End Using
End Sub
Private Sub ParameterQuery()
'Initialize a new instance of the WDABase data class
Using objData As New WDABase
objData.Command.CommandText = "Query2"
objData.Command.CommandType = CommandType.StoredProcedure
'PopulateGrid the data grid
PopulateGrid()
End Using
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
NonParameterQuery()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
ParameterQuery()
End Sub
EndClass







 
Old May 9th, 2009, 07:20 AM
Thearon's Avatar
Wrox Author
 
Join Date: Dec 2003
Posts: 396
Thanks: 0
Thanked 8 Times in 8 Posts
Default

Does your WDABase class initialize the Command object when the class is initialized? From the code that you have posted that is inferred.

Thearon
 
Old May 13th, 2009, 01:49 PM
Authorized User
 
Join Date: Aug 2005
Posts: 88
Thanks: 4
Thanked 0 Times in 0 Posts
Default DataGrid and the WDA Base Class

Thearon, thank you for the reply.

Unless I misunderstood thr code, this initializes the Command object in the EDABase class:

PublicSub InitializeCommand()
If Command IsNothingThen
Try
Command = New OleDbCommand(SQL, Connection)
'See if this is a stored procedure
IfNot SQL.ToUpper.StartsWith("SELECT ") _
AndNot SQL.ToUpper.StartsWith("INSERT ") _
AndNot SQL.ToUpper.StartsWith("UPDATE ") _
AndNot SQL.ToUpper.StartsWith("DELETE ") Then
Command.CommandType = CommandType.StoredProcedure
EndIf
Catch OleDbExceptionErr As OleDbException
ThrowNew System.Exception(OleDbExceptionErr.Message, _
OleDbExceptionErr.InnerException)
EndTry
EndIf
EndSub

I get the error at this line of code:
objData.Command.CommandText = "Query1"
 
Old June 23rd, 2009, 06:49 AM
Thearon's Avatar
Wrox Author
 
Join Date: Dec 2003
Posts: 396
Thanks: 0
Thanked 8 Times in 8 Posts
Default

Have you tried setting a breakpoint in the InitializeCommand procedure to ensure that the Command object is being initialized. If so then step through the code from that point until you set it to your query to make sure no other call is being made to set that object to Nothing.

Thearon





Similar Threads
Thread Thread Starter Forum Replies Last Post
Base Class for Code-Behind Problem groupmatch BOOK: ASP.NET Website Programming Problem-Design-Solution 3 September 8th, 2004 05:06 AM
classes :: base class is inaccessible .... Kaliste C# 3 July 16th, 2004 04:56 AM
base class inheritance krunch97 VB.NET 2002/2003 Basics 6 April 23rd, 2004 11:48 AM
Page_Load override with base page class nebrown1 BOOK: ASP.NET Website Programming Problem-Design-Solution 1 August 31st, 2003 04:45 AM
Help with DBObject base class projectedNexus BOOK: ASP.NET Website Programming Problem-Design-Solution 1 August 12th, 2003 06:36 PM





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