|
|
 |
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 p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.
|
 |

May 6th, 2009, 12:31 PM
|
|
Authorized User
|
|
Join Date: Aug 2005
Location: , , .
Posts: 44
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
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
|

May 9th, 2009, 08:20 AM
|
 |
Wrox Author
|
|
Join Date: Dec 2003
Location: Fuquay Varina, NC, USA.
Posts: 381
Thanks: 0
Thanked 6 Times in 6 Posts
|
|
Does your WDABase class initialize the Command object when the class is initialized? From the code that you have posted that is inferred.
Thearon
|

May 13th, 2009, 02:49 PM
|
|
Authorized User
|
|
Join Date: Aug 2005
Location: , , .
Posts: 44
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
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"
|

June 23rd, 2009, 07:49 AM
|
 |
Wrox Author
|
|
Join Date: Dec 2003
Location: Fuquay Varina, NC, USA.
Posts: 381
Thanks: 0
Thanked 6 Times in 6 Posts
|
|
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
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |