Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Object reference not set to an instance... ado.net 2008 xpress 1

Status
Not open for further replies.

TheMadHater

Programmer
Jun 25, 2009
11
US
Hello all,

I getting this error "Object reference not set to an instance of an object." when I hit the next button on my form all code is below Visual Studio 2008 express, sql express. controls are bound to a data set. The form loads and the first record is displayed but there are two issues.

1. The lblPosition doesn't display any information after the load

2. When I use the button click event I get the above referenced error. "Object reference not set to an instance of an object.

Can anyone shed any light on this? I have reason to believe that the error is generated somewhere on this line.. artists is the database name.

bmbArtists = Me.BindingContext(DsArtistDisplay1, "artists")




Public Class Form1
Inherits System.Windows.Forms.Form

Dim bmbArtists As BindingManagerBase

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'DsArtistDisplay1.DataTable1' table. You can move, or remove it, as needed.
DataTable1TableAdapter.Fill(DsArtistDisplay1.DataTable1)
'bmbArtists = Me.BindingContext(DsArtistDisplay1, "artists")
DisplayPosition()

End Sub

Private Sub DisplayPosition()
lblPos.Text = bmbArtists.Position + 1 & "of" & bmbArtists.Count
End Sub
Private Sub NavigationButtons_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click, btnLast.Click, btnNext.Click, btnPrevious.Click
Select sender.name
Case "btnFirst"
bmbArtists.Position = 0
Case "btnLast"
bmbArtists.Position = bmbArtists.Count - 1
Case "btnNext"
bmbArtists.Position += 1
Case "btnPrevious"
bmbArtists.Position -= 1
End Select
DisplayPosition()
End Sub

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Close()
End Sub
End Class
 
Code:
bmbArtists = New Me.BindingContext(DsArtistDisplay1, "artists")

Zameer Abdulla
 
I appreciate the assistance but that gives me too many arguments for the public sub new??
 
Oops forgot to tell you that is when I remove the Me. keyword if I leave it in me.bindingContext... then I get Keyword does not name a type error. Very stange indeed.
 
This is still open please if you know the solution I have an interview this week and I need to know this stuff and I'm stuck here. Any help would be appreciated.
 
Still trying to get an answer on this one bumping the thread.
 
To do this, use a CurrencyManager instead of BindingManagerBase.

Code:
Public Class Form1

    Dim cmArtists As CurrencyManager

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'DsArtistDisplay1.DataTable1' table. You can move, or remove it, as needed.
        Me.DataTable1TableAdapter.Fill(Me.DsArtistDisplay1.DataTable1)

        cmArtists = Me.BindingContext(DsArtistDisplay1, "DataTable1") 'DataMember should be the name of the DataTable you want

        DisplayPosition()

    End Sub

    Private Sub DisplayPosition()

        lblPos.Text = cmArtists.Position + 1 & " of " & cmArtists.Count

    End Sub

    Private Sub NavigationButtons_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click, btnLast.Click, btnNext.Click, btnPrevious.Click
        Select Case sender.name
            Case "btnFirst"
                cmArtists.Position = 0
            Case "btnLast"
                cmArtists.Position = cmArtists.Count - 1
            Case "btnNext"
                cmArtists.Position += 1
            Case "btnPrevious"
                cmArtists.Position -= 1
        End Select
        DisplayPosition()
    End Sub
End Class

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Thanks for the assistance but when I try it that way I get the same error. To make my issue more clear I'm getting that error when I hit either the next, previous or any of the buttons (Not exit of course). I have reason to suspect it is because the values of the buttons are null. I still can't get the lblPos.txt to display any value when the form is loaded as well. when I try with the currency manager the form doesn't even fill for some reason. when I try with bindingmanagerbase the initial record loads but the labelPos does not populate and then when I hit either next, previous, last or first button is when I get the error that the object reference is not set to an instance. Hope that clears the issue up of course any assistance is definitely appreciated I have an interview tomorrow at 8:30 am and the job is for this kind of work.
 
Sorry guys I still don't have an answer to this so I'm bumping the thread.
 
You really need to break and step through the code until you get the line that is causing the error or double check the error as often it will give the line that caused the error. What they have given you is correct as long as what you have given them is correct. I’m not saying you left something out on purpose just that it happens.

The error you are getting would sort of translate as "I can't do that because [red]it[/red] isn't there". "It" can be anything. It seems most likely that cmArtists is set to nothing when you try to click one of the buttons so you might try checking that first. Check both that it is getting set on form load and that it is still set when you click the button.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
You are right it is set to nothing so how do I get the value? It should be the position of the record number. In addition the label is not showing any text which leads me to believe that for some reason the position is a null value. The error line is the line for the button that I click for example if I hit the next button then I get the error on that line, the code for that line is

cmArtists.Position += 1

but again the label doesn't show a value upon the form load event and it should be there already so I think that is the problem there. For some reason the position value is not being calculated.
 
The label thing is strange and should be causing an error as well if it is nothing rather than being blank. The code is right so if we can figure out why cmArtists is nothing then it should fix itself.

Did you check then form load event? Does:
Code:
cmArtists = Me.BindingContext(DsArtistDisplay1, "DataTable1") 'DataMember should be the name of the DataTable you want
When changed to the correct Table name does it set cmArtists or is it nothing. If it is nothing then check that DsArtistDisplay1 is not nothing.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Update when I changed the above line of code
cmartists = me.bindingContext(dsArtistdisplay1.datatable1)
The label works correctly and when I hit the previous or next button the label displays the proper data now the problem is the records don't cycle through from one to the next so if I hit the last button it will say record 3 of 3 but show you the first record that gets filled upon the form load. I am no longer getting any errors but the records are not being changed from one to the next however the displayPosition() method is working fine. Any clues???
 
You have the controls bound to a different source. Here is where I can't help you. I prefer not to bind directly to a data source so I'm not totally up on all the ways to bind and I've never used a CurrencyManager object so I can't tell you how/if you can bind it directly to the control.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Bind the label.text property in the designer to the field you want, then in the label's textChanged event add the extra text you want. Then as the records change it will do the same.
 
To clarify for your needs:
Code:
lblPos.Text = bmbArtists.Position + 1 & " of " & bmbArtists.Count
This goes into your label's text changed event to erase what the field text that would be there, you need it bound so it will change with the records, but then just overwrite the text.

 

Sorwen,

What do you use instead of the built in databinding? I've seen a few people on the forums who don't use it and I'm just curious about what the other options are. I haven't had many problems, but that doesn't mean it's the best way to go about it.

pmegan
 
Databinding is the suggested .Net way of doing it. I just prefer to have more control over how the output looks and acts so I just loop through the object and write to the text method of whatever controls I'm working with. One way isn't better or worse than the other. Generally I'm only allowing a user to look at one record and so I prefer that way, but if I was using something with a DataGridView then I would bind to it as it is just easier.

Then too with most of the projects I work with now they are meant to only have user interaction if something goes wrong. So I rarely even output data to the screen. It usually goes directly to a text file, excel, access or sql database.

If you just want something until you can bind then something like this is what you can do.
Code:
        Dim drv As DataRowView = bmbArtists.Current
        For Count As Integer = 0 To drv.Row.ItemArray.Length - 1
            'Set controls text to drv.Item(Count)
        Next
You don't show what you need bmbArtists for, but if you didn't need it then you could just do something like this if your form has a BindingSource (which I assume it does) then just put this in your next button.
Code:
Me.BindingSource1.MoveNext()
That forces the source to the next row which will automatically change any bound control to the display the new row as well.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top