da.update and Bool --> yes/no type

  • Thread starter Stephen Plotnick
  • Start date
S

Stephen Plotnick

In a data grid using Access as the data source..

The da.update is failing with
"object reference not set to an instance of anobject"

It looks like everything is ok and I was wondering if there is a problem
that I have a bool column mapping to a yes/no type.

__________________________________
Here is the code:

Private Sub PopulateProductGrid()

Dim conn As New
System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
source='C:\Program Files\MyProgramArea\Pricing\Pricing.mdb';Persist Security
Info=False")

Dim sSQL As String

sSQL = "select * from ProductNames"

conn.Open()

Dim da As New System.Data.OleDb.OleDbDataAdapter(sSQL, conn)

Dim cb As New OleDb.OleDbCommandBuilder(da)

Try

da.Fill(myDS, "ProductNames")

DataGrid1.DataSource = myDS

DataGrid1.DataMember = "ProductNames"

Dim irow As Integer

Dim icol As Integer

irow = 0

DataGrid1.Select(irow)

DataGrid1.SetDataBinding(myDS, "ProductNames")

RowCount = DataGrid1.BindingContext(myDS, "ProductNames").Count()

Catch ex As Exception

MessageBox.Show("Failed to connect to data source")

Finally

'conn.Close()

End Try

End Sub

Private Sub ProductTypeSelection_Load(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyBase.Load

PopulateProductGrid()

StoreNameOut.Text = StoreName

VolumeDiscountOut.Text = VolumeDiscount

'

' Create a Grid Table Style. Map it to the "Product Names" Table.

'

Dim aGridTableStyle As New DataGridTableStyle

aGridTableStyle.MappingName = "ProductNames"

aGridTableStyle.AlternatingBackColor = Color.Lavender

'

' Create GridColumnStyle objects for the grid columns

'

Dim aCol1 As New DataGridTextBoxColumn

Dim aCol2 As New DataGridTextBoxColumn

Dim aCol3 As New DataGridBoolColumn

'

With aCol1

..HeaderText = "Code"

..MappingName = "CODE_VALUE"

..Width = 50

..Alignment = HorizontalAlignment.Center

..TextBox.Enabled = False

End With

'

' Set column's caption, width and disable editing.

'

With aCol2

..MappingName = "CODE_MEANING"

..HeaderText = "Prodcut Type"

..Width = 515

..Alignment = HorizontalAlignment.Left

..TextBox.Enabled = False

End With

With aCol3

..MappingName = "CODE_Selected"

..HeaderText = "Select"

..Width = 50

..Alignment = HorizontalAlignment.Center

End With



'

' Add the GridColumnStyles to the DataGrid's Column Styles collection.

'

With aGridTableStyle.GridColumnStyles

..Add(aCol1)

..Add(aCol2)

..Add(aCol3)

End With

'

' Add the GridColumnStyles to the aGridTableStyle.

'

DataGrid1.TableStyles.Add(aGridTableStyle)

End Sub

Private Sub DataGrid1_Navigate(ByVal sender As System.Object, ByVal ne As
System.Windows.Forms.NavigateEventArgs) Handles DataGrid1.Navigate

End Sub

Private Sub NextButton_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles NextButton.Click

DataGrid1.DataSource = myDS

DataGrid1.DataMember = "ProductNames"

Try

da.Update(myDS, "ProductNames")

Catch ex As Exception

MsgBox(ex.Message)

End Try

PricingForm.StoreName = StoreName

PricingForm.VolumeDiscount = VolumeDiscount

PricingForm.Show()

End Sub
 
S

Scott M.

You need to give us the line that is failing. "Object reference not set to
an instance of an object:." is a well-known error that indicates that you
have an object variable that does not point to an actual instance of a class
and you are attempting to use that object variable as if it did point to an
actual instance. If we know what line is producing that error, we can help
pinpoint the problem.
 
G

Guest

I see that your DataAdapter is constructed in one subroutine and used in
another. And that all Subs are private. My guess is that it is not static -
ie doesn't exist as a global to all routines in the application - and it is
being destroyed as the execution leaves PopulateProductGrid. I just don't
know how VB handles undeclared names - in NextButton_Click, for example.
Would the compiler object to "da" as an undefined item or simple assume its a
null reference?

This problem occurs much more often in C++ in which you can define a
pointer, but not initialize it to an object. Is that possible in your code?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top