B
Barret Bonden
I get "An unhandled exception of type "system.nullreferenceException.
Object reference did not set to an instance of an object"
while trying to save back to the database - relating to : da.Update(ds)
, the last line of code in this listing. Just starting tyring to learn data
in vb.net- wanted to hand code data access from a form, and it all works
fine , but for saving back to the source.
Trying to learn ...
Imports System
Imports System.Data
Imports System.Data.OleDb
Public Class Form1
Inherits System.Windows.Forms.Form
Private recordsread As Long
Private strconnect As String
Private mytable As String
Private mydataset As DataSet
Public ds As New DataSet
Public ot As DataTable
Public mytab As String '2nd way
Public da As OleDb.OleDbDataAdapter
' Public objconn As OleDb.OleDbDataAdapter
' Dim objConn As New OleDb.OleDbConnection(con)
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents btnnext As System.Windows.Forms.Button
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents Button3 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.btnnext = New System.Windows.Forms.Button
Me.Button1 = New System.Windows.Forms.Button
Me.TextBox2 = New System.Windows.Forms.TextBox
Me.Button2 = New System.Windows.Forms.Button
Me.Button3 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(72, 88)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(128, 20)
Me.TextBox1.TabIndex = 0
Me.TextBox1.Text = "TextBox1"
'
'btnnext
'
Me.btnnext.Location = New System.Drawing.Point(32, 168)
Me.btnnext.Name = "btnnext"
Me.btnnext.Size = New System.Drawing.Size(56, 24)
Me.btnnext.TabIndex = 1
Me.btnnext.Text = "next"
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(176, 160)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(48, 32)
Me.Button1.TabIndex = 2
Me.Button1.Text = "load "
'
'TextBox2
'
Me.TextBox2.Location = New System.Drawing.Point(72, 120)
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.Size = New System.Drawing.Size(128, 20)
Me.TextBox2.TabIndex = 3
Me.TextBox2.Text = "TextBox2"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(32, 208)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(56, 24)
Me.Button2.TabIndex = 4
Me.Button2.Text = "prior"
'
'Button3
'
Me.Button3.Location = New System.Drawing.Point(176, 208)
Me.Button3.Name = "Button3"
Me.Button3.Size = New System.Drawing.Size(48, 24)
Me.Button3.TabIndex = 5
Me.Button3.Text = "save "
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.Button3)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.TextBox2)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.btnnext)
Me.Controls.Add(Me.TextBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub btnnext_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnnext.Click
Me.BindingContext(ds, mytab).Position += 1
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim con As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data
Source=F:\old d\splash\SPLASH.MDB;Persist Security Info=False "
Dim sSQL As String
sSQL = "select * from company"
Dim objConn As New OleDb.OleDbConnection(con)
' objconn = New OleDb.OleDbConnection(con)
mytab = "company" '2nd way
Dim da As New OleDb.OleDbDataAdapter(sSQL, con)
' Dim ds As New DataSet("test")
' Dim ds As New DataSet
Dim dv As DataView
Try
objConn.Open()
Catch myexception As System.Exception
Windows.Forms.MessageBox.Show(myexception.Message)
End Try
If objConn.State = ConnectionState.Open Then
Try
' da.Fill(ds, "company")
da.Fill(ds, mytab)
objConn.Close()
' Dim ot As DataTable
ot = ds.Tables("company")
'dim dr as DataRow = ot.Rows.
'Dim dc As DataColumn
Dim s1 As Single
s1 = ot.Rows.Count
Dim dr As DataRow
For Each dr In ds.Tables("company").Rows
' TextBox3.Text = dr("company")
' ComboBox1.Items.Add(dr("company"))
Console.WriteLine(dr("company"))
Next
s1 = ot.Columns.Count
' TextBox2.Text = s1
dv = ot.DefaultView
setcontbind()
TextBox1.DataBindings.Add("text", dv, "company")
Catch myexception As System.Exception
Windows.Forms.MessageBox.Show(myexception.Message)
End Try
End If
End Sub
Private Sub setcontbind()
TextBox2.DataBindings.Add("text", ds, "company.company")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Me.BindingContext(ds, mytab).Position -= 1
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
da.Update(ds)
End Sub
End Class
Object reference did not set to an instance of an object"
while trying to save back to the database - relating to : da.Update(ds)
, the last line of code in this listing. Just starting tyring to learn data
in vb.net- wanted to hand code data access from a form, and it all works
fine , but for saving back to the source.
Trying to learn ...
Imports System
Imports System.Data
Imports System.Data.OleDb
Public Class Form1
Inherits System.Windows.Forms.Form
Private recordsread As Long
Private strconnect As String
Private mytable As String
Private mydataset As DataSet
Public ds As New DataSet
Public ot As DataTable
Public mytab As String '2nd way
Public da As OleDb.OleDbDataAdapter
' Public objconn As OleDb.OleDbDataAdapter
' Dim objConn As New OleDb.OleDbConnection(con)
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents btnnext As System.Windows.Forms.Button
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents Button3 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.btnnext = New System.Windows.Forms.Button
Me.Button1 = New System.Windows.Forms.Button
Me.TextBox2 = New System.Windows.Forms.TextBox
Me.Button2 = New System.Windows.Forms.Button
Me.Button3 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(72, 88)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(128, 20)
Me.TextBox1.TabIndex = 0
Me.TextBox1.Text = "TextBox1"
'
'btnnext
'
Me.btnnext.Location = New System.Drawing.Point(32, 168)
Me.btnnext.Name = "btnnext"
Me.btnnext.Size = New System.Drawing.Size(56, 24)
Me.btnnext.TabIndex = 1
Me.btnnext.Text = "next"
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(176, 160)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(48, 32)
Me.Button1.TabIndex = 2
Me.Button1.Text = "load "
'
'TextBox2
'
Me.TextBox2.Location = New System.Drawing.Point(72, 120)
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.Size = New System.Drawing.Size(128, 20)
Me.TextBox2.TabIndex = 3
Me.TextBox2.Text = "TextBox2"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(32, 208)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(56, 24)
Me.Button2.TabIndex = 4
Me.Button2.Text = "prior"
'
'Button3
'
Me.Button3.Location = New System.Drawing.Point(176, 208)
Me.Button3.Name = "Button3"
Me.Button3.Size = New System.Drawing.Size(48, 24)
Me.Button3.TabIndex = 5
Me.Button3.Text = "save "
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.Button3)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.TextBox2)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.btnnext)
Me.Controls.Add(Me.TextBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub btnnext_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnnext.Click
Me.BindingContext(ds, mytab).Position += 1
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim con As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data
Source=F:\old d\splash\SPLASH.MDB;Persist Security Info=False "
Dim sSQL As String
sSQL = "select * from company"
Dim objConn As New OleDb.OleDbConnection(con)
' objconn = New OleDb.OleDbConnection(con)
mytab = "company" '2nd way
Dim da As New OleDb.OleDbDataAdapter(sSQL, con)
' Dim ds As New DataSet("test")
' Dim ds As New DataSet
Dim dv As DataView
Try
objConn.Open()
Catch myexception As System.Exception
Windows.Forms.MessageBox.Show(myexception.Message)
End Try
If objConn.State = ConnectionState.Open Then
Try
' da.Fill(ds, "company")
da.Fill(ds, mytab)
objConn.Close()
' Dim ot As DataTable
ot = ds.Tables("company")
'dim dr as DataRow = ot.Rows.
'Dim dc As DataColumn
Dim s1 As Single
s1 = ot.Rows.Count
Dim dr As DataRow
For Each dr In ds.Tables("company").Rows
' TextBox3.Text = dr("company")
' ComboBox1.Items.Add(dr("company"))
Console.WriteLine(dr("company"))
Next
s1 = ot.Columns.Count
' TextBox2.Text = s1
dv = ot.DefaultView
setcontbind()
TextBox1.DataBindings.Add("text", dv, "company")
Catch myexception As System.Exception
Windows.Forms.MessageBox.Show(myexception.Message)
End Try
End If
End Sub
Private Sub setcontbind()
TextBox2.DataBindings.Add("text", ds, "company.company")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Me.BindingContext(ds, mytab).Position -= 1
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
da.Update(ds)
End Sub
End Class