Problem OledbDataAdapter

  • Thread starter Thread starter explode
  • Start date Start date
E

explode

I made a oledbdataadapter with this code

Public Sub Novo(ByVal nova1 As String, ByVal nova2 As String)
Dim i As Integer
Dim nova As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter
Dim veza As OleDb.OleDbConnection = New
OleDb.OleDbConnection(Global.Diplomski1.My.MySettings.Default.Studenti1Connection)
Dim tableMapping As System.Data.Common.DataTableMapping = New
System.Data.Common.DataTableMapping("Marko", "Table1")


For i = 0 To Form2.Studenti1Data.Tables.Item(0).Columns.Count - 1
tableMapping.ColumnMappings.Add(Form2.Studenti1Data.Tables.Item(0).Columns.Item(i).ColumnName,
Form2.Studenti1Data.Tables.Item(0).Columns.Item(i).ColumnName)
Next i

nova.TableMappings.Add(tableMapping)

nova.MissingMappingAction = MissingMappingAction.Passthrough
nova.MissingSchemaAction = MissingSchemaAction.AddWithKey
nova.AcceptChangesDuringUpdate = True

nova.SelectCommand.Connection = veza
nova.SelectCommand.CommandType = CommandType.Text
nova.SelectCommand.CommandText = "SELECT * FROM " & nova1
nova.SelectCommand.Parameters.Add("Broj iksice",
OleDb.OleDbType.Double, 0, "Broj iksice")
nova.SelectCommand.Parameters.Add("Prezime", OleDb.OleDbType.WChar,
0, "Prezime")
nova.SelectCommand.Parameters.Add("Ime", OleDb.OleDbType.WChar, 0,
"Ime")
nova.SelectCommand.Parameters.Add("Broj indeksa",
OleDb.OleDbType.WChar, 0, "Broj indeksa")
nova.SelectCommand.Parameters.Add("Broj pohadjanja",
OleDb.OleDbType.WChar, 0, "Broj pohadjanja")


nova.UpdateCommand.Connection = veza
nova.UpdateCommand.CommandType = CommandType.Text
nova.UpdateCommand.CommandText = "UPDATE " & nova1 & " SET Broj
iksice=?, Prezime=?, Ime=?, Broj indeksa=?, Broj pohadjanja=? " & _
"WHERE Broj iksice=?"


nova.InsertCommand.Connection = veza
nova.InsertCommand.CommandType = CommandType.Text
nova.InsertCommand.CommandText = "INSERT INTO " & nova1 & " (Broj
iksice, Prezime, Ime, Broj indeksa, Broj pohadjanja) VALUES (?,?,?,?,?)"

nova.DeleteCommand.Connection = veza
nova.DeleteCommand.CommandType = CommandType.Text
nova.DeleteCommand.CommandText = "UPDATE " & nova1 & " SET Broj
iksice=?, Prezime=?, Ime=?, Broj indeksa=?, Broj pohadjanja=? " & _
"WHERE Broj iksice=?"

If nova2 = "Fill" Then

nova.Fill(Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)))

End If
End Sub

I have an access database and connected it to my project, but when I execute
it and call this procedure program stops at nova.SelectCommand.Connection =
veza
with this exception

System.NullReferenceException was unhandled
Message="Object reference not set to an instance of an object."
Source="Diplomski1"
StackTrace:
at Diplomski1.Form1.Novo(String nova1, String nova2) in
D:\OdMomka\Diplomski\Diplomski1\Diplomski1\Form1.vb:line 201
at Diplomski1.Form2.Form2_Load(Object sender, EventArgs e) in
D:\OdMomka\Diplomski\Diplomski1\Diplomski1\Form2.vb:line 12
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&
m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef hWnd,
Int32 nCmdShow)
at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
at System.Windows.Forms.Control.Show()
at Diplomski1.Form1.Button1_Click(Object sender, EventArgs e) in
D:\OdMomka\Diplomski\Diplomski1\Diplomski1\Form1.vb:line 8
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons
button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&
m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&
msg)
at
System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32
reason, ApplicationContext context)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.Application.Run(ApplicationContext context)
at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[]
commandLine)
at Diplomski1.My.MyApplication.Main(String[] Args) in
17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[]
args)
at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)
at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()
at
System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext
activationContext, String[] activationCustomData)
at
System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext
activationContext)
at
Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
 
Where are you opening the connection? Try adding this:

veza.Open()

And at the end, be sure to close it again. Or wrap the whole
thing in a Using statement.

Robin S.
---------------------------
explode said:
I made a oledbdataadapter with this code

Public Sub Novo(ByVal nova1 As String, ByVal nova2 As String)
Dim i As Integer
Dim nova As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter
Dim veza As OleDb.OleDbConnection = New
OleDb.OleDbConnection(Global.Diplomski1.My.MySettings.Default.Studenti1Connection)
Dim tableMapping As System.Data.Common.DataTableMapping = New
System.Data.Common.DataTableMapping("Marko", "Table1")


For i = 0 To Form2.Studenti1Data.Tables.Item(0).Columns.Count -
1

tableMapping.ColumnMappings.Add(Form2.Studenti1Data.Tables.Item(0).Columns.Item(i).ColumnName,
Form2.Studenti1Data.Tables.Item(0).Columns.Item(i).ColumnName)
Next i

nova.TableMappings.Add(tableMapping)

nova.MissingMappingAction = MissingMappingAction.Passthrough
nova.MissingSchemaAction = MissingSchemaAction.AddWithKey
nova.AcceptChangesDuringUpdate = True

nova.SelectCommand.Connection = veza
nova.SelectCommand.CommandType = CommandType.Text
nova.SelectCommand.CommandText = "SELECT * FROM " & nova1
nova.SelectCommand.Parameters.Add("Broj iksice",
OleDb.OleDbType.Double, 0, "Broj iksice")
nova.SelectCommand.Parameters.Add("Prezime",
OleDb.OleDbType.WChar, 0, "Prezime")
nova.SelectCommand.Parameters.Add("Ime", OleDb.OleDbType.WChar,
0, "Ime")
nova.SelectCommand.Parameters.Add("Broj indeksa",
OleDb.OleDbType.WChar, 0, "Broj indeksa")
nova.SelectCommand.Parameters.Add("Broj pohadjanja",
OleDb.OleDbType.WChar, 0, "Broj pohadjanja")


nova.UpdateCommand.Connection = veza
nova.UpdateCommand.CommandType = CommandType.Text
nova.UpdateCommand.CommandText = "UPDATE " & nova1 & " SET Broj
iksice=?, Prezime=?, Ime=?, Broj indeksa=?, Broj pohadjanja=? " & _
"WHERE Broj iksice=?"


nova.InsertCommand.Connection = veza
nova.InsertCommand.CommandType = CommandType.Text
nova.InsertCommand.CommandText = "INSERT INTO " & nova1 & "
(Broj iksice, Prezime, Ime, Broj indeksa, Broj pohadjanja) VALUES
(?,?,?,?,?)"

nova.DeleteCommand.Connection = veza
nova.DeleteCommand.CommandType = CommandType.Text
nova.DeleteCommand.CommandText = "UPDATE " & nova1 & " SET Broj
iksice=?, Prezime=?, Ime=?, Broj indeksa=?, Broj pohadjanja=? " & _
"WHERE Broj iksice=?"

If nova2 = "Fill" Then


nova.Fill(Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)))

End If
End Sub

I have an access database and connected it to my project, but when I
execute it and call this procedure program stops at
nova.SelectCommand.Connection = veza
with this exception

System.NullReferenceException was unhandled
Message="Object reference not set to an instance of an object."
Source="Diplomski1"
StackTrace:
at Diplomski1.Form1.Novo(String nova1, String nova2) in
D:\OdMomka\Diplomski\Diplomski1\Diplomski1\Form1.vb:line 201
at Diplomski1.Form2.Form2_Load(Object sender, EventArgs e) in
D:\OdMomka\Diplomski\Diplomski1\Diplomski1\Form2.vb:line 12
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean
fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr
hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef
hWnd, Int32 nCmdShow)
at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
at System.Windows.Forms.Control.Show()
at Diplomski1.Form1.Button1_Click(Object sender, EventArgs e) in
D:\OdMomka\Diplomski\Diplomski1\Diplomski1\Form1.vb:line 8
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m,
MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at
System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr
hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at
System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at
System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32
reason, ApplicationContext context)
at
System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32
reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(ApplicationContext
context)
at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[]
commandLine)
at Diplomski1.My.MyApplication.Main(String[] Args) in
17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[]
args)
at System.Runtime.Hosting.ManifestRunner.Run(Boolean
checkAptModel)
at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()
at
System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext
activationContext, String[] activationCustomData)
at
System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext
activationContext)
at
Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
at System.Threading.ThreadHelper.ThreadStart_Context(Object
state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
 
Back
Top