Visual Basic Development Bookmark and Share   
 Home > Visual Basic Power Packs > DataRepeater AddNew blowing up
 

DataRepeater AddNew blowing up

I've got a DataRepeater with no bindings that is blowing up when I try to add a new item in the my user control's Load event.

Code:

Code Snippet

Private Sub ctrl_533_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

drHistory.AllowUserToAddItems = True

drHistory.AddNew()

End Sub

Resulting error:

ERROR MESSAGE:
Object reference not set to an instance of an object.

STACK TRACE:
at Microsoft.VisualBasic.PowerPacks.NullDataManager.CancelCurrentEdit()
at Microsoft.VisualBasic.PowerPacks.DataRepeater.CancelEdit()
at Microsoft.VisualBasic.PowerPacks.DataRepeater.EndCurrentItemEdit()
at Microsoft.VisualBasic.PowerPacks.DataRepeater.AddNew()
at RPM.Physical.Main.ctrl_533.ctrl_533_Load(Object sender, EventArgs e) in C:\Code\DotNet05\RPM 4\usercontrols\ctrl_533.vb:line 862
at System.Windows.Forms.UserControl.OnLoad(EventArgs e)
at System.Windows.Forms.UserControl.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.ControlCollection.Add(Control value)

...

Any ideas as to what is going on and how to fix it?

Zloth  Monday, February 25, 2008 9:15 PM

This is not a bug. If DataSourcepropertyis Nothing, any database operation will cause NullReferenceException. Please set DataSource property first if there is no data bound control on DataRepeater.

yfdong_MSFT  Tuesday, February 26, 2008 3:15 AM

I just repeated this in VB 2008. Steps to reproduce:

1. Start a VB 2008 Windows Forms project. Add the PowerPack reference.

2. Put a DataRepeater on the form. Leave all defaults. Add a couple of labels and a DateTimePicker to the repeater.

3. In the form's load event, put the following line: DataRepeater1.AddNew.

4. Run. NullReferenceException down in ..PowerPacks.NullDataManager.CancelCurrentEdit.

Maybe I should have done this as a bug topic?

Zloth  Monday, February 25, 2008 9:59 PM

This is not a bug. If DataSourcepropertyis Nothing, any database operation will cause NullReferenceException. Please set DataSource property first if there is no data bound control on DataRepeater.

yfdong_MSFT  Tuesday, February 26, 2008 3:15 AM

I'm not doing a database operation, I'm just adding a row to the control. So I bound it to a Data.DataTable and tried to just populate. That wouldn't work because the DataRepeater.SelectedItem would remain Nothing no matter what I did.

However, I found the note in the 'using unbound data' help for the DataRepeater that says at least one control must be bound or the DataRepeater won't work. So I bound something that didn't need much formatting and now all is well.

But what happens if, say, my data for the control is completely hard coded? Can I put the data for each DataRepeater item into a class, put the classes into a collection, and bind to that? I'm sure in VB 2008 I could do a LINQ query on the collection and bind the DataRepeater to that just fine, but what about 2005?

Zloth  Tuesday, February 26, 2008 4:47 PM

You can use "Virtual Mode" by setting DataRepeater.VirtualMode = True in Properties Window or Code Editor. DataRepeater will raise ItemValueNeeded event when it needs data and it will raise ItemValuePushed event when the value of control is changed.

yfdong_MSFT  Wednesday, February 27, 2008 2:14 AM
I keep seeing claims that when VirtualMode = True then the ItemValueNeeded and ItemValuePushed events will be fired when values are needed for a control, or a when a contol's value is changed. I've done exactly that, and these events NEVER fire. The control that I've placed in the datarepeater template is a simple custom control with several public properties. What exactly does it mean "when a control's value is changed"? Thanks.
Lance77035  Monday, September 28, 2009 12:39 AM
For those interested, these are the missing puzzle pieces:

When implementing a custom usercontrol in your datarepeater, it must:

1. Define a DefaultBindingProperty for the custom class (This will trigger the datarepeater'sItemValueNeeded event)
2. Implement INotifyPropertyChanged and invoke the ProperChanged event when your property's value changes.

Code Sample:

<Bindable(

True), DefaultBindingProperty("TransactionName")> _

Public

Class RegisterEntry

Implements INotifyPropertyChanged

Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged

Private Sub NotifyPropertyChanged(ByVal info As String)

RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))

End Sub




Lance77035  Monday, September 28, 2009 4:32 AM

You can use google to search for other answers

Custom Search

More Threads

• embedded Media Player repeat playlist
• When i draw Rubberband selection on panel VisualBasic powerpack Shapecontrols are not displayed
• Assigning the system default printer to 'Printform1.PrinterSettings.PrinterName' (Visual Basic 2008)
• help! regarding textbox control
• Printing reports
• Incompatibility with Visual Studio 2010 Ultimate Beta 2
• How to change font name and size text printing using dos mode
• Combobox in a datarepeater
• No drawing controls in toolbox after install
• HAving forms in bigger size than what it is usually