New columns and update problem...!

E

explode

I made a procedure Public Sub Novo(ByVal nova1 As String, ByVal nova2 As
String) that creates a new oledbDataAdapter with insert update select and
delete commads. I also added that commands can change depending how many
columns are in a Table.
I add a new column with this code:

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button4.Click
Try
Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Add("TEST
Marko", GetType(Integer))
Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).AcceptChanges()
Form2.Table1DataGridView.AutoGenerateColumns = True
Novo(Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).TableName,
"Fill")

Catch ex As Exception
MsgBox("Vec postoji dati stupac!", MsgBoxStyle.Exclamation, "Upozorenje!")
End Try

End Sub


Function index() gets the index of the Table. The column shows in the
datagridview but whe I try
to save changes I get this exception:


System.Data.OleDb.OleDbException was unhandled
ErrorCode=-2147217887
Message="Cannot update 'TEST Marko'; field not updateable."
Source="Microsoft JET Database Engine"
StackTrace:
at
System.Data.Common.DbDataAdapter.UpdatedRowStatusErrors(RowUpdatedEventArgs
rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)
at
System.Data.Common.DbDataAdapter.UpdatedRowStatus(RowUpdatedEventArgs
rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)
at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows,
DataTableMapping tableMapping)
at System.Data.Common.DbDataAdapter.UpdateFromDataTable(DataTable
dataTable, DataTableMapping tableMapping)
at System.Data.Common.DbDataAdapter.Update(DataTable dataTable)
at Diplomski1.Form1.Novo(String nova1, String nova2) in
D:\OdMomka\Diplomski\Diplomski1\Diplomski1\Form1.vb:line 305
at Diplomski1.Form2.Table1BindingNavigatorSaveItem_Click(Object
sender, EventArgs e) in
D:\OdMomka\Diplomski\Diplomski1\Diplomski1\Form2.vb:line 8
at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key,
EventArgs e)
at System.Windows.Forms.ToolStripButton.OnClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs
e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e,
ToolStripItemEventType met)
at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons
button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ToolStrip.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()


Procedur for the oledbDataAdapter is:


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 SelectCom As OleDb.OleDbCommand = New OleDb.OleDbCommand
Dim UpdateCom As OleDb.OleDbCommand = New OleDb.OleDbCommand
Dim InsertCom As OleDb.OleDbCommand = New OleDb.OleDbCommand
Dim DeleteCom As OleDb.OleDbCommand = New OleDb.OleDbCommand
Dim tableMapping As System.Data.Common.DataTableMapping = New
System.Data.Common.DataTableMapping("Marko", ComboBox1.Text)
veza.Open()
For i = 0 To
Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Count - 1
tableMapping.ColumnMappings.Add(Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Item(i).ColumnName,
Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Item(i).ColumnName)
Next i
nova.TableMappings.Add(tableMapping)
nova.MissingMappingAction = MissingMappingAction.Passthrough
nova.MissingSchemaAction = MissingSchemaAction.AddWithKey
nova.AcceptChangesDuringUpdate = True
SelectCom.Connection = veza
SelectCom.CommandType = CommandType.Text
SelectCom.CommandText = "SELECT * FROM " & nova1
UpdateCom.Connection = veza
UpdateCom.CommandType = CommandType.Text
UpdateCom.CommandText = "UPDATE " & nova1 & " SET " & InsSql(ComboBox1.Text,
"Treci") & " WHERE [Broj iksice]=?" '[Broj iksice]=?, [Prezime]=?, [Ime]=?,
[Broj indeksa]=?, [Broj pohadjanja]=? & _
UpdateCom.Parameters.Add(New OleDb.OleDbParameter("Broj iksice",
OleDb.OleDbType.Double, 0, System.Data.ParameterDirection.Input, CType(20,
Byte), CType(0, Byte), "Broj iksice", DataRowVersion.Current, False,
Nothing))
UpdateCom.Parameters.Add(New OleDb.OleDbParameter("Prezime",
OleDb.OleDbType.WChar, 0, System.Data.ParameterDirection.Input, CType(0,
Byte), CType(0, Byte), "Prezime", DataRowVersion.Current, False, Nothing))
UpdateCom.Parameters.Add(New OleDb.OleDbParameter("Ime",
OleDb.OleDbType.WChar, 0, System.Data.ParameterDirection.Input, CType(0,
Byte), CType(0, Byte), "Ime", DataRowVersion.Current, False, Nothing))
UpdateCom.Parameters.Add(New OleDb.OleDbParameter("Broj indeksa",
OleDb.OleDbType.WChar, 0, System.Data.ParameterDirection.Input, CType(0,
Byte), CType(0, Byte), "Broj indeksa", DataRowVersion.Current, False,
Nothing))
UpdateCom.Parameters.Add(New OleDb.OleDbParameter("Broj pohadjanja",
OleDb.OleDbType.Integer, 0, System.Data.ParameterDirection.Input, CType(0,
Byte), CType(0, Byte), "Broj pohadjanja", DataRowVersion.Current, False,
Nothing))
For i = 5 To
Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Count - 1
UpdateCom.Parameters.Add(New
OleDb.OleDbParameter(Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Item(i).ColumnName,
_
OleDb.OleDbType.Integer, 0, System.Data.ParameterDirection.Input, CType(0,
Byte), CType(0, Byte),
Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Item(i).ColumnName,
DataRowVersion.Current, False, Nothing))
Next i
UpdateCom.Parameters.Add(New OleDb.OleDbParameter("stari Broj iksice",
OleDb.OleDbType.Double, 0, System.Data.ParameterDirection.Input, CType(20,
Byte), CType(0, Byte), "Broj iksice", DataRowVersion.Original, False,
Nothing))
UpdateCom.Parameters.Add(New OleDb.OleDbParameter("stari Prezime",
OleDb.OleDbType.WChar, 0, System.Data.ParameterDirection.Input, CType(0,
Byte), CType(0, Byte), "Prezime", DataRowVersion.Original, False, Nothing))
UpdateCom.Parameters.Add(New OleDb.OleDbParameter("stari Ime",
OleDb.OleDbType.WChar, 0, System.Data.ParameterDirection.Input, CType(0,
Byte), CType(0, Byte), "Ime", DataRowVersion.Original, False, Nothing))
UpdateCom.Parameters.Add(New OleDb.OleDbParameter("stari Broj indeksa",
OleDb.OleDbType.WChar, 0, System.Data.ParameterDirection.Input, CType(0,
Byte), CType(0, Byte), "Broj indeksa", DataRowVersion.Original, True,
Nothing))
UpdateCom.Parameters.Add(New OleDb.OleDbParameter("stari Broj pohadjanja",
OleDb.OleDbType.Integer, 0, System.Data.ParameterDirection.Input, CType(0,
Byte), CType(0, Byte), "Broj pohadjanja", DataRowVersion.Original, True,
Nothing))
For i = 5 To
Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Count - 1
UpdateCom.Parameters.Add(New OleDb.OleDbParameter("stari " &
Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Item(i).ColumnName,
_
OleDb.OleDbType.Integer, 0, System.Data.ParameterDirection.Input, CType(0,
Byte), CType(0, Byte),
Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Item(i).ColumnName,
DataRowVersion.Original, True, Nothing))
Next i
InsertCom.Connection = veza
InsertCom.CommandType = CommandType.Text
InsertCom.CommandText = "INSERT INTO " & nova1 & " (" &
InsSql(ComboBox1.Text, "Prvi") & ")" & " VALUES " & "(" &
InsSql(ComboBox1.Text, "Drugi") & ")" '" ([Broj iksice], [Prezime], [Ime],
[Broj indeksa], " & _
'"[Broj pohadjanja]) VALUES (?,?,?,?,?)"
InsertCom.Parameters.Add("Broj iksice", OleDb.OleDbType.Double, 0, "Broj
iksice")
InsertCom.Parameters.Add("Prezime", OleDb.OleDbType.WChar, 0, "Prezime")
InsertCom.Parameters.Add("Ime", OleDb.OleDbType.WChar, 0, "Ime")
InsertCom.Parameters.Add("Broj indeksa", OleDb.OleDbType.WChar, 0, "Broj
indeksa")
InsertCom.Parameters.Add("Broj pohadjanja", OleDb.OleDbType.Integer, 0,
"Broj pohadjanja")
For i = 5 To
Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Count - 1
InsertCom.Parameters.Add(Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Item(i).ColumnName,
_
OleDb.OleDbType.Integer, 0,
Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Item(i).ColumnName)
Next i
DeleteCom.Connection = veza
DeleteCom.CommandType = CommandType.Text
DeleteCom.CommandText = "DELETE * FROM " & nova1 & " WHERE [Broj iksice]=?"
DeleteCom.Parameters.Add(New OleDb.OleDbParameter("Broj iksice",
OleDb.OleDbType.Double, 0, System.Data.ParameterDirection.Input, False,
CType(20, Byte), CType(0, Byte), "Broj iksice", DataRowVersion.Original,
Nothing))
DeleteCom.Parameters.Add(New OleDb.OleDbParameter("Prezime",
OleDb.OleDbType.WChar, 0, System.Data.ParameterDirection.Input, False,
CType(0, Byte), CType(0, Byte), "Prezime", DataRowVersion.Original,
Nothing))
DeleteCom.Parameters.Add(New OleDb.OleDbParameter("Ime",
OleDb.OleDbType.WChar, 0, System.Data.ParameterDirection.Input, False,
CType(0, Byte), CType(0, Byte), "Ime", DataRowVersion.Original, Nothing))
DeleteCom.Parameters.Add(New OleDb.OleDbParameter("Broj indeksa",
OleDb.OleDbType.WChar, 0, System.Data.ParameterDirection.Input, True,
CType(0, Byte), CType(0, Byte), "Broj indeksa", DataRowVersion.Original,
Nothing))
DeleteCom.Parameters.Add(New OleDb.OleDbParameter("Broj pohadjanja",
OleDb.OleDbType.Integer, 0, System.Data.ParameterDirection.Input, True,
CType(0, Byte), CType(0, Byte), "Broj pohadjanja", DataRowVersion.Original,
Nothing))
For i = 5 To
Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Count - 1
DeleteCom.Parameters.Add(Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Item(i).ColumnName,
_
OleDb.OleDbType.Integer, 0,
Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Item(i).ColumnName)
Next i

nova.SelectCommand = SelectCom
nova.UpdateCommand = UpdateCom
nova.DeleteCommand = DeleteCom
nova.InsertCommand = InsertCom


If nova2 = "Fill" Then
nova.Fill(Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)))
End If
If nova2 = "Update" Then
nova.Update(Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)))
End If
veza.Close()
End Sub


InsSql(ComboBox1.Text, string) creates new sql commands with new column. The
code is:


Public Function InsSql(ByVal imeTab As String, ByVal tipSql As String) As
String 'Generira nastavak sql komande za update i insert
Dim i As Integer
Dim sql As String = "[Broj iksice]"
If tipSql = "Prvi" Then
For i = 1 To Form2.Studenti1Data.Tables.Item(Index(imeTab)).Columns.Count -
1
sql = sql & ", [" &
Form2.Studenti1Data.Tables.Item(Index(imeTab)).Columns.Item(i).ColumnName &
"]"
Next i
End If
If tipSql = "Drugi" Then
sql = "?"
For i = 0 To Form2.Studenti1Data.Tables.Item(Index(imeTab)).Columns.Count -
2
sql = sql & ", ?" 'Ove dvije if petlje za insert komandu
Next i
End If
If tipSql = "Treci" Then
sql = "[Broj iksice]=?"
For i = 1 To Form2.Studenti1Data.Tables.Item(Index(imeTab)).Columns.Count -
1
sql = sql & ", [" &
Form2.Studenti1Data.Tables.Item(Index(imeTab)).Columns.Item(i).ColumnName &
"]" & "=?" 'Za Update komandu
Next i
End If
Return sql
End Function

Dataset is Studenti1data, I made it by the new data source
wizard,and made datagridview and bindingsource draging Table1 to Form2. I
use Access database.
Please help....!
 
C

Cor Ligthert [MVP]

Explode,

Do you really believe that there is one person on earth who will have a look
at your code.

If you want help, than make a simple sample from absolute no more than 20
lines.

Cor

explode said:
I made a procedure Public Sub Novo(ByVal nova1 As String, ByVal nova2 As
String) that creates a new oledbDataAdapter with insert update select and
delete commads. I also added that commands can change depending how many
columns are in a Table.
I add a new column with this code:

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button4.Click
Try
Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Add("TEST
Marko", GetType(Integer))
Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).AcceptChanges()
Form2.Table1DataGridView.AutoGenerateColumns = True
Novo(Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).TableName,
"Fill")

Catch ex As Exception
MsgBox("Vec postoji dati stupac!", MsgBoxStyle.Exclamation, "Upozorenje!")
End Try

End Sub


Function index() gets the index of the Table. The column shows in the
datagridview but whe I try
to save changes I get this exception:


System.Data.OleDb.OleDbException was unhandled
ErrorCode=-2147217887
Message="Cannot update 'TEST Marko'; field not updateable."
Source="Microsoft JET Database Engine"
StackTrace:
at
System.Data.Common.DbDataAdapter.UpdatedRowStatusErrors(RowUpdatedEventArgs
rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)
at
System.Data.Common.DbDataAdapter.UpdatedRowStatus(RowUpdatedEventArgs
rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)
at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows,
DataTableMapping tableMapping)
at System.Data.Common.DbDataAdapter.UpdateFromDataTable(DataTable
dataTable, DataTableMapping tableMapping)
at System.Data.Common.DbDataAdapter.Update(DataTable dataTable)
at Diplomski1.Form1.Novo(String nova1, String nova2) in
D:\OdMomka\Diplomski\Diplomski1\Diplomski1\Form1.vb:line 305
at Diplomski1.Form2.Table1BindingNavigatorSaveItem_Click(Object
sender, EventArgs e) in
D:\OdMomka\Diplomski\Diplomski1\Diplomski1\Form2.vb:line 8
at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key,
EventArgs e)
at System.Windows.Forms.ToolStripButton.OnClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs
e)
at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs
e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e,
ToolStripItemEventType met)
at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons
button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ToolStrip.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()


Procedur for the oledbDataAdapter is:


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 SelectCom As OleDb.OleDbCommand = New OleDb.OleDbCommand
Dim UpdateCom As OleDb.OleDbCommand = New OleDb.OleDbCommand
Dim InsertCom As OleDb.OleDbCommand = New OleDb.OleDbCommand
Dim DeleteCom As OleDb.OleDbCommand = New OleDb.OleDbCommand
Dim tableMapping As System.Data.Common.DataTableMapping = New
System.Data.Common.DataTableMapping("Marko", ComboBox1.Text)
veza.Open()
For i = 0 To
Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Count - 1
tableMapping.ColumnMappings.Add(Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Item(i).ColumnName,
Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Item(i).ColumnName)
Next i
nova.TableMappings.Add(tableMapping)
nova.MissingMappingAction = MissingMappingAction.Passthrough
nova.MissingSchemaAction = MissingSchemaAction.AddWithKey
nova.AcceptChangesDuringUpdate = True
SelectCom.Connection = veza
SelectCom.CommandType = CommandType.Text
SelectCom.CommandText = "SELECT * FROM " & nova1
UpdateCom.Connection = veza
UpdateCom.CommandType = CommandType.Text
UpdateCom.CommandText = "UPDATE " & nova1 & " SET " &
InsSql(ComboBox1.Text, "Treci") & " WHERE [Broj iksice]=?" '[Broj
iksice]=?, [Prezime]=?, [Ime]=?, [Broj indeksa]=?, [Broj pohadjanja]=? & _
UpdateCom.Parameters.Add(New OleDb.OleDbParameter("Broj iksice",
OleDb.OleDbType.Double, 0, System.Data.ParameterDirection.Input, CType(20,
Byte), CType(0, Byte), "Broj iksice", DataRowVersion.Current, False,
Nothing))
UpdateCom.Parameters.Add(New OleDb.OleDbParameter("Prezime",
OleDb.OleDbType.WChar, 0, System.Data.ParameterDirection.Input, CType(0,
Byte), CType(0, Byte), "Prezime", DataRowVersion.Current, False, Nothing))
UpdateCom.Parameters.Add(New OleDb.OleDbParameter("Ime",
OleDb.OleDbType.WChar, 0, System.Data.ParameterDirection.Input, CType(0,
Byte), CType(0, Byte), "Ime", DataRowVersion.Current, False, Nothing))
UpdateCom.Parameters.Add(New OleDb.OleDbParameter("Broj indeksa",
OleDb.OleDbType.WChar, 0, System.Data.ParameterDirection.Input, CType(0,
Byte), CType(0, Byte), "Broj indeksa", DataRowVersion.Current, False,
Nothing))
UpdateCom.Parameters.Add(New OleDb.OleDbParameter("Broj pohadjanja",
OleDb.OleDbType.Integer, 0, System.Data.ParameterDirection.Input, CType(0,
Byte), CType(0, Byte), "Broj pohadjanja", DataRowVersion.Current, False,
Nothing))
For i = 5 To
Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Count - 1
UpdateCom.Parameters.Add(New
OleDb.OleDbParameter(Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Item(i).ColumnName,
_
OleDb.OleDbType.Integer, 0, System.Data.ParameterDirection.Input, CType(0,
Byte), CType(0, Byte),
Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Item(i).ColumnName,
DataRowVersion.Current, False, Nothing))
Next i
UpdateCom.Parameters.Add(New OleDb.OleDbParameter("stari Broj iksice",
OleDb.OleDbType.Double, 0, System.Data.ParameterDirection.Input, CType(20,
Byte), CType(0, Byte), "Broj iksice", DataRowVersion.Original, False,
Nothing))
UpdateCom.Parameters.Add(New OleDb.OleDbParameter("stari Prezime",
OleDb.OleDbType.WChar, 0, System.Data.ParameterDirection.Input, CType(0,
Byte), CType(0, Byte), "Prezime", DataRowVersion.Original, False,
Nothing))
UpdateCom.Parameters.Add(New OleDb.OleDbParameter("stari Ime",
OleDb.OleDbType.WChar, 0, System.Data.ParameterDirection.Input, CType(0,
Byte), CType(0, Byte), "Ime", DataRowVersion.Original, False, Nothing))
UpdateCom.Parameters.Add(New OleDb.OleDbParameter("stari Broj indeksa",
OleDb.OleDbType.WChar, 0, System.Data.ParameterDirection.Input, CType(0,
Byte), CType(0, Byte), "Broj indeksa", DataRowVersion.Original, True,
Nothing))
UpdateCom.Parameters.Add(New OleDb.OleDbParameter("stari Broj pohadjanja",
OleDb.OleDbType.Integer, 0, System.Data.ParameterDirection.Input, CType(0,
Byte), CType(0, Byte), "Broj pohadjanja", DataRowVersion.Original, True,
Nothing))
For i = 5 To
Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Count - 1
UpdateCom.Parameters.Add(New OleDb.OleDbParameter("stari " &
Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Item(i).ColumnName,
_
OleDb.OleDbType.Integer, 0, System.Data.ParameterDirection.Input, CType(0,
Byte), CType(0, Byte),
Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Item(i).ColumnName,
DataRowVersion.Original, True, Nothing))
Next i
InsertCom.Connection = veza
InsertCom.CommandType = CommandType.Text
InsertCom.CommandText = "INSERT INTO " & nova1 & " (" &
InsSql(ComboBox1.Text, "Prvi") & ")" & " VALUES " & "(" &
InsSql(ComboBox1.Text, "Drugi") & ")" '" ([Broj iksice], [Prezime], [Ime],
[Broj indeksa], " & _
'"[Broj pohadjanja]) VALUES (?,?,?,?,?)"
InsertCom.Parameters.Add("Broj iksice", OleDb.OleDbType.Double, 0, "Broj
iksice")
InsertCom.Parameters.Add("Prezime", OleDb.OleDbType.WChar, 0, "Prezime")
InsertCom.Parameters.Add("Ime", OleDb.OleDbType.WChar, 0, "Ime")
InsertCom.Parameters.Add("Broj indeksa", OleDb.OleDbType.WChar, 0, "Broj
indeksa")
InsertCom.Parameters.Add("Broj pohadjanja", OleDb.OleDbType.Integer, 0,
"Broj pohadjanja")
For i = 5 To
Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Count - 1
InsertCom.Parameters.Add(Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Item(i).ColumnName,
_
OleDb.OleDbType.Integer, 0,
Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Item(i).ColumnName)
Next i
DeleteCom.Connection = veza
DeleteCom.CommandType = CommandType.Text
DeleteCom.CommandText = "DELETE * FROM " & nova1 & " WHERE [Broj
iksice]=?"
DeleteCom.Parameters.Add(New OleDb.OleDbParameter("Broj iksice",
OleDb.OleDbType.Double, 0, System.Data.ParameterDirection.Input, False,
CType(20, Byte), CType(0, Byte), "Broj iksice", DataRowVersion.Original,
Nothing))
DeleteCom.Parameters.Add(New OleDb.OleDbParameter("Prezime",
OleDb.OleDbType.WChar, 0, System.Data.ParameterDirection.Input, False,
CType(0, Byte), CType(0, Byte), "Prezime", DataRowVersion.Original,
Nothing))
DeleteCom.Parameters.Add(New OleDb.OleDbParameter("Ime",
OleDb.OleDbType.WChar, 0, System.Data.ParameterDirection.Input, False,
CType(0, Byte), CType(0, Byte), "Ime", DataRowVersion.Original, Nothing))
DeleteCom.Parameters.Add(New OleDb.OleDbParameter("Broj indeksa",
OleDb.OleDbType.WChar, 0, System.Data.ParameterDirection.Input, True,
CType(0, Byte), CType(0, Byte), "Broj indeksa", DataRowVersion.Original,
Nothing))
DeleteCom.Parameters.Add(New OleDb.OleDbParameter("Broj pohadjanja",
OleDb.OleDbType.Integer, 0, System.Data.ParameterDirection.Input, True,
CType(0, Byte), CType(0, Byte), "Broj pohadjanja",
DataRowVersion.Original, Nothing))
For i = 5 To
Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Count - 1
DeleteCom.Parameters.Add(Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Item(i).ColumnName,
_
OleDb.OleDbType.Integer, 0,
Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Item(i).ColumnName)
Next i

nova.SelectCommand = SelectCom
nova.UpdateCommand = UpdateCom
nova.DeleteCommand = DeleteCom
nova.InsertCommand = InsertCom


If nova2 = "Fill" Then
nova.Fill(Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)))
End If
If nova2 = "Update" Then
nova.Update(Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)))
End If
veza.Close()
End Sub


InsSql(ComboBox1.Text, string) creates new sql commands with new column.
The code is:


Public Function InsSql(ByVal imeTab As String, ByVal tipSql As String) As
String 'Generira nastavak sql komande za update i insert
Dim i As Integer
Dim sql As String = "[Broj iksice]"
If tipSql = "Prvi" Then
For i = 1 To
Form2.Studenti1Data.Tables.Item(Index(imeTab)).Columns.Count - 1
sql = sql & ", [" &
Form2.Studenti1Data.Tables.Item(Index(imeTab)).Columns.Item(i).ColumnName
& "]"
Next i
End If
If tipSql = "Drugi" Then
sql = "?"
For i = 0 To
Form2.Studenti1Data.Tables.Item(Index(imeTab)).Columns.Count - 2
sql = sql & ", ?" 'Ove dvije if petlje za insert komandu
Next i
End If
If tipSql = "Treci" Then
sql = "[Broj iksice]=?"
For i = 1 To
Form2.Studenti1Data.Tables.Item(Index(imeTab)).Columns.Count - 1
sql = sql & ", [" &
Form2.Studenti1Data.Tables.Item(Index(imeTab)).Columns.Item(i).ColumnName
& "]" & "=?" 'Za Update komandu
Next i
End If
Return sql
End Function

Dataset is Studenti1data, I made it by the new data source
wizard,and made datagridview and bindingsource draging Table1 to Form2. I
use Access database.
Please help....!
 
R

RobinS

This is your primary error:
Message="Cannot update 'TEST Marko'; field not updateable."

Is the field an AutoNumber field? If so, you can not update it.

Robin S.

-----------------------------------
explode said:
I made a procedure Public Sub Novo(ByVal nova1 As String, ByVal nova2
As String) that creates a new oledbDataAdapter with insert update
select and delete commads. I also added that commands can change
depending how many columns are in a Table.
I add a new column with this code:

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button4.Click
Try
Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Add("TEST
Marko", GetType(Integer))
Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).AcceptChanges()
Form2.Table1DataGridView.AutoGenerateColumns = True
Novo(Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).TableName,
"Fill")

Catch ex As Exception
MsgBox("Vec postoji dati stupac!", MsgBoxStyle.Exclamation,
"Upozorenje!")
End Try

End Sub


Function index() gets the index of the Table. The column shows in the
datagridview but whe I try
to save changes I get this exception:


System.Data.OleDb.OleDbException was unhandled
ErrorCode=-2147217887
Message="Cannot update 'TEST Marko'; field not updateable."
Source="Microsoft JET Database Engine"
StackTrace:
at
System.Data.Common.DbDataAdapter.UpdatedRowStatusErrors(RowUpdatedEventArgs
rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)
at
System.Data.Common.DbDataAdapter.UpdatedRowStatus(RowUpdatedEventArgs
rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)
at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows,
DataTableMapping tableMapping)
at
System.Data.Common.DbDataAdapter.UpdateFromDataTable(DataTable
dataTable, DataTableMapping tableMapping)
at System.Data.Common.DbDataAdapter.Update(DataTable dataTable)
at Diplomski1.Form1.Novo(String nova1, String nova2) in
D:\OdMomka\Diplomski\Diplomski1\Diplomski1\Form1.vb:line 305
at Diplomski1.Form2.Table1BindingNavigatorSaveItem_Click(Object
sender, EventArgs e) in
D:\OdMomka\Diplomski\Diplomski1\Diplomski1\Form2.vb:line 8
at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key,
EventArgs e)
at System.Windows.Forms.ToolStripButton.OnClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
at
System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
at
System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e,
ToolStripItemEventType met)
at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e,
ToolStripItemEventType met)
at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.Control.WmMouseUp(Message& m,
MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ToolStrip.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()


Procedur for the oledbDataAdapter is:


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 SelectCom As OleDb.OleDbCommand = New OleDb.OleDbCommand
Dim UpdateCom As OleDb.OleDbCommand = New OleDb.OleDbCommand
Dim InsertCom As OleDb.OleDbCommand = New OleDb.OleDbCommand
Dim DeleteCom As OleDb.OleDbCommand = New OleDb.OleDbCommand
Dim tableMapping As System.Data.Common.DataTableMapping = New
System.Data.Common.DataTableMapping("Marko", ComboBox1.Text)
veza.Open()
For i = 0 To
Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Count -
1
tableMapping.ColumnMappings.Add(Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Item(i).ColumnName,
Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Item(i).ColumnName)
Next i
nova.TableMappings.Add(tableMapping)
nova.MissingMappingAction = MissingMappingAction.Passthrough
nova.MissingSchemaAction = MissingSchemaAction.AddWithKey
nova.AcceptChangesDuringUpdate = True
SelectCom.Connection = veza
SelectCom.CommandType = CommandType.Text
SelectCom.CommandText = "SELECT * FROM " & nova1
UpdateCom.Connection = veza
UpdateCom.CommandType = CommandType.Text
UpdateCom.CommandText = "UPDATE " & nova1 & " SET " &
InsSql(ComboBox1.Text, "Treci") & " WHERE [Broj iksice]=?" '[Broj
iksice]=?, [Prezime]=?, [Ime]=?, [Broj indeksa]=?, [Broj pohadjanja]=?
& _
UpdateCom.Parameters.Add(New OleDb.OleDbParameter("Broj iksice",
OleDb.OleDbType.Double, 0, System.Data.ParameterDirection.Input,
CType(20, Byte), CType(0, Byte), "Broj iksice",
DataRowVersion.Current, False, Nothing))
UpdateCom.Parameters.Add(New OleDb.OleDbParameter("Prezime",
OleDb.OleDbType.WChar, 0, System.Data.ParameterDirection.Input,
CType(0, Byte), CType(0, Byte), "Prezime", DataRowVersion.Current,
False, Nothing))
UpdateCom.Parameters.Add(New OleDb.OleDbParameter("Ime",
OleDb.OleDbType.WChar, 0, System.Data.ParameterDirection.Input,
CType(0, Byte), CType(0, Byte), "Ime", DataRowVersion.Current, False,
Nothing))
UpdateCom.Parameters.Add(New OleDb.OleDbParameter("Broj indeksa",
OleDb.OleDbType.WChar, 0, System.Data.ParameterDirection.Input,
CType(0, Byte), CType(0, Byte), "Broj indeksa",
DataRowVersion.Current, False, Nothing))
UpdateCom.Parameters.Add(New OleDb.OleDbParameter("Broj pohadjanja",
OleDb.OleDbType.Integer, 0, System.Data.ParameterDirection.Input,
CType(0, Byte), CType(0, Byte), "Broj pohadjanja",
DataRowVersion.Current, False, Nothing))
For i = 5 To
Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Count -
1
UpdateCom.Parameters.Add(New
OleDb.OleDbParameter(Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Item(i).ColumnName,
_
OleDb.OleDbType.Integer, 0, System.Data.ParameterDirection.Input,
CType(0, Byte), CType(0, Byte),
Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Item(i).ColumnName,
DataRowVersion.Current, False, Nothing))
Next i
UpdateCom.Parameters.Add(New OleDb.OleDbParameter("stari Broj iksice",
OleDb.OleDbType.Double, 0, System.Data.ParameterDirection.Input,
CType(20, Byte), CType(0, Byte), "Broj iksice",
DataRowVersion.Original, False, Nothing))
UpdateCom.Parameters.Add(New OleDb.OleDbParameter("stari Prezime",
OleDb.OleDbType.WChar, 0, System.Data.ParameterDirection.Input,
CType(0, Byte), CType(0, Byte), "Prezime", DataRowVersion.Original,
False, Nothing))
UpdateCom.Parameters.Add(New OleDb.OleDbParameter("stari Ime",
OleDb.OleDbType.WChar, 0, System.Data.ParameterDirection.Input,
CType(0, Byte), CType(0, Byte), "Ime", DataRowVersion.Original, False,
Nothing))
UpdateCom.Parameters.Add(New OleDb.OleDbParameter("stari Broj
indeksa", OleDb.OleDbType.WChar, 0,
System.Data.ParameterDirection.Input, CType(0, Byte), CType(0, Byte),
"Broj indeksa", DataRowVersion.Original, True, Nothing))
UpdateCom.Parameters.Add(New OleDb.OleDbParameter("stari Broj
pohadjanja", OleDb.OleDbType.Integer, 0,
System.Data.ParameterDirection.Input, CType(0, Byte), CType(0, Byte),
"Broj pohadjanja", DataRowVersion.Original, True, Nothing))
For i = 5 To
Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Count -
1
UpdateCom.Parameters.Add(New OleDb.OleDbParameter("stari " &
Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Item(i).ColumnName,
_
OleDb.OleDbType.Integer, 0, System.Data.ParameterDirection.Input,
CType(0, Byte), CType(0, Byte),
Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Item(i).ColumnName,
DataRowVersion.Original, True, Nothing))
Next i
InsertCom.Connection = veza
InsertCom.CommandType = CommandType.Text
InsertCom.CommandText = "INSERT INTO " & nova1 & " (" &
InsSql(ComboBox1.Text, "Prvi") & ")" & " VALUES " & "(" &
InsSql(ComboBox1.Text, "Drugi") & ")" '" ([Broj iksice], [Prezime],
[Ime], [Broj indeksa], " & _
'"[Broj pohadjanja]) VALUES (?,?,?,?,?)"
InsertCom.Parameters.Add("Broj iksice", OleDb.OleDbType.Double, 0,
"Broj iksice")
InsertCom.Parameters.Add("Prezime", OleDb.OleDbType.WChar, 0,
"Prezime")
InsertCom.Parameters.Add("Ime", OleDb.OleDbType.WChar, 0, "Ime")
InsertCom.Parameters.Add("Broj indeksa", OleDb.OleDbType.WChar, 0,
"Broj indeksa")
InsertCom.Parameters.Add("Broj pohadjanja", OleDb.OleDbType.Integer,
0, "Broj pohadjanja")
For i = 5 To
Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Count -
1
InsertCom.Parameters.Add(Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Item(i).ColumnName,
_
OleDb.OleDbType.Integer, 0,
Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Item(i).ColumnName)
Next i
DeleteCom.Connection = veza
DeleteCom.CommandType = CommandType.Text
DeleteCom.CommandText = "DELETE * FROM " & nova1 & " WHERE [Broj
iksice]=?"
DeleteCom.Parameters.Add(New OleDb.OleDbParameter("Broj iksice",
OleDb.OleDbType.Double, 0, System.Data.ParameterDirection.Input,
False, CType(20, Byte), CType(0, Byte), "Broj iksice",
DataRowVersion.Original, Nothing))
DeleteCom.Parameters.Add(New OleDb.OleDbParameter("Prezime",
OleDb.OleDbType.WChar, 0, System.Data.ParameterDirection.Input, False,
CType(0, Byte), CType(0, Byte), "Prezime", DataRowVersion.Original,
Nothing))
DeleteCom.Parameters.Add(New OleDb.OleDbParameter("Ime",
OleDb.OleDbType.WChar, 0, System.Data.ParameterDirection.Input, False,
CType(0, Byte), CType(0, Byte), "Ime", DataRowVersion.Original,
Nothing))
DeleteCom.Parameters.Add(New OleDb.OleDbParameter("Broj indeksa",
OleDb.OleDbType.WChar, 0, System.Data.ParameterDirection.Input, True,
CType(0, Byte), CType(0, Byte), "Broj indeksa",
DataRowVersion.Original, Nothing))
DeleteCom.Parameters.Add(New OleDb.OleDbParameter("Broj pohadjanja",
OleDb.OleDbType.Integer, 0, System.Data.ParameterDirection.Input,
True, CType(0, Byte), CType(0, Byte), "Broj pohadjanja",
DataRowVersion.Original, Nothing))
For i = 5 To
Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Count -
1
DeleteCom.Parameters.Add(Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Item(i).ColumnName,
_
OleDb.OleDbType.Integer, 0,
Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Item(i).ColumnName)
Next i

nova.SelectCommand = SelectCom
nova.UpdateCommand = UpdateCom
nova.DeleteCommand = DeleteCom
nova.InsertCommand = InsertCom


If nova2 = "Fill" Then
nova.Fill(Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)))
End If
If nova2 = "Update" Then
nova.Update(Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)))
End If
veza.Close()
End Sub


InsSql(ComboBox1.Text, string) creates new sql commands with new
column. The code is:


Public Function InsSql(ByVal imeTab As String, ByVal tipSql As String)
As String 'Generira nastavak sql komande za update i insert
Dim i As Integer
Dim sql As String = "[Broj iksice]"
If tipSql = "Prvi" Then
For i = 1 To
Form2.Studenti1Data.Tables.Item(Index(imeTab)).Columns.Count - 1
sql = sql & ", [" &
Form2.Studenti1Data.Tables.Item(Index(imeTab)).Columns.Item(i).ColumnName
& "]"
Next i
End If
If tipSql = "Drugi" Then
sql = "?"
For i = 0 To
Form2.Studenti1Data.Tables.Item(Index(imeTab)).Columns.Count - 2
sql = sql & ", ?" 'Ove dvije if petlje za insert komandu
Next i
End If
If tipSql = "Treci" Then
sql = "[Broj iksice]=?"
For i = 1 To
Form2.Studenti1Data.Tables.Item(Index(imeTab)).Columns.Count - 1
sql = sql & ", [" &
Form2.Studenti1Data.Tables.Item(Index(imeTab)).Columns.Item(i).ColumnName
& "]" & "=?" 'Za Update komandu
Next i
End If
Return sql
End Function

Dataset is Studenti1data, I made it by the new data source
wizard,and made datagridview and bindingsource draging Table1 to
Form2. I use Access database.
Please help....!
 
R

RobinS

Apparently there is *one*. But in all fairness, I didn't scrutinize it
that closely. A little dizzying, isn't it?

Robin S.
-----------------------------------------

Cor Ligthert said:
Explode,

Do you really believe that there is one person on earth who will have
a look at your code.

If you want help, than make a simple sample from absolute no more than
20 lines.

Cor

explode said:
I made a procedure Public Sub Novo(ByVal nova1 As String, ByVal nova2
As String) that creates a new oledbDataAdapter with insert update
select and delete commads. I also added that commands can change
depending how many columns are in a Table.
I add a new column with this code:

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button4.Click
Try
Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Add("TEST
Marko", GetType(Integer))
Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).AcceptChanges()
Form2.Table1DataGridView.AutoGenerateColumns = True
Novo(Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).TableName,
"Fill")

Catch ex As Exception
MsgBox("Vec postoji dati stupac!", MsgBoxStyle.Exclamation,
"Upozorenje!")
End Try

End Sub


Function index() gets the index of the Table. The column shows in the
datagridview but whe I try
to save changes I get this exception:


System.Data.OleDb.OleDbException was unhandled
ErrorCode=-2147217887
Message="Cannot update 'TEST Marko'; field not updateable."
Source="Microsoft JET Database Engine"
StackTrace:
at
System.Data.Common.DbDataAdapter.UpdatedRowStatusErrors(RowUpdatedEventArgs
rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32
commandCount)
at
System.Data.Common.DbDataAdapter.UpdatedRowStatus(RowUpdatedEventArgs
rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32
commandCount)
at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows,
DataTableMapping tableMapping)
at
System.Data.Common.DbDataAdapter.UpdateFromDataTable(DataTable
dataTable, DataTableMapping tableMapping)
at System.Data.Common.DbDataAdapter.Update(DataTable dataTable)
at Diplomski1.Form1.Novo(String nova1, String nova2) in
D:\OdMomka\Diplomski\Diplomski1\Diplomski1\Form1.vb:line 305
at Diplomski1.Form2.Table1BindingNavigatorSaveItem_Click(Object
sender, EventArgs e) in
D:\OdMomka\Diplomski\Diplomski1\Diplomski1\Form2.vb:line 8
at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key,
EventArgs e)
at System.Windows.Forms.ToolStripButton.OnClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
at
System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
at
System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e,
ToolStripItemEventType met)
at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e,
ToolStripItemEventType met)
at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.Control.WmMouseUp(Message& m,
MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ToolStrip.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()


Procedur for the oledbDataAdapter is:


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 SelectCom As OleDb.OleDbCommand = New OleDb.OleDbCommand
Dim UpdateCom As OleDb.OleDbCommand = New OleDb.OleDbCommand
Dim InsertCom As OleDb.OleDbCommand = New OleDb.OleDbCommand
Dim DeleteCom As OleDb.OleDbCommand = New OleDb.OleDbCommand
Dim tableMapping As System.Data.Common.DataTableMapping = New
System.Data.Common.DataTableMapping("Marko", ComboBox1.Text)
veza.Open()
For i = 0 To
Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Count
- 1
tableMapping.ColumnMappings.Add(Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Item(i).ColumnName,
Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Item(i).ColumnName)
Next i
nova.TableMappings.Add(tableMapping)
nova.MissingMappingAction = MissingMappingAction.Passthrough
nova.MissingSchemaAction = MissingSchemaAction.AddWithKey
nova.AcceptChangesDuringUpdate = True
SelectCom.Connection = veza
SelectCom.CommandType = CommandType.Text
SelectCom.CommandText = "SELECT * FROM " & nova1
UpdateCom.Connection = veza
UpdateCom.CommandType = CommandType.Text
UpdateCom.CommandText = "UPDATE " & nova1 & " SET " &
InsSql(ComboBox1.Text, "Treci") & " WHERE [Broj iksice]=?" '[Broj
iksice]=?, [Prezime]=?, [Ime]=?, [Broj indeksa]=?, [Broj
pohadjanja]=? & _
UpdateCom.Parameters.Add(New OleDb.OleDbParameter("Broj iksice",
OleDb.OleDbType.Double, 0, System.Data.ParameterDirection.Input,
CType(20, Byte), CType(0, Byte), "Broj iksice",
DataRowVersion.Current, False, Nothing))
UpdateCom.Parameters.Add(New OleDb.OleDbParameter("Prezime",
OleDb.OleDbType.WChar, 0, System.Data.ParameterDirection.Input,
CType(0, Byte), CType(0, Byte), "Prezime", DataRowVersion.Current,
False, Nothing))
UpdateCom.Parameters.Add(New OleDb.OleDbParameter("Ime",
OleDb.OleDbType.WChar, 0, System.Data.ParameterDirection.Input,
CType(0, Byte), CType(0, Byte), "Ime", DataRowVersion.Current, False,
Nothing))
UpdateCom.Parameters.Add(New OleDb.OleDbParameter("Broj indeksa",
OleDb.OleDbType.WChar, 0, System.Data.ParameterDirection.Input,
CType(0, Byte), CType(0, Byte), "Broj indeksa",
DataRowVersion.Current, False, Nothing))
UpdateCom.Parameters.Add(New OleDb.OleDbParameter("Broj pohadjanja",
OleDb.OleDbType.Integer, 0, System.Data.ParameterDirection.Input,
CType(0, Byte), CType(0, Byte), "Broj pohadjanja",
DataRowVersion.Current, False, Nothing))
For i = 5 To
Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Count
- 1
UpdateCom.Parameters.Add(New
OleDb.OleDbParameter(Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Item(i).ColumnName,
_
OleDb.OleDbType.Integer, 0, System.Data.ParameterDirection.Input,
CType(0, Byte), CType(0, Byte),
Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Item(i).ColumnName,
DataRowVersion.Current, False, Nothing))
Next i
UpdateCom.Parameters.Add(New OleDb.OleDbParameter("stari Broj
iksice", OleDb.OleDbType.Double, 0,
System.Data.ParameterDirection.Input, CType(20, Byte), CType(0,
Byte), "Broj iksice", DataRowVersion.Original, False, Nothing))
UpdateCom.Parameters.Add(New OleDb.OleDbParameter("stari Prezime",
OleDb.OleDbType.WChar, 0, System.Data.ParameterDirection.Input,
CType(0, Byte), CType(0, Byte), "Prezime", DataRowVersion.Original,
False, Nothing))
UpdateCom.Parameters.Add(New OleDb.OleDbParameter("stari Ime",
OleDb.OleDbType.WChar, 0, System.Data.ParameterDirection.Input,
CType(0, Byte), CType(0, Byte), "Ime", DataRowVersion.Original,
False, Nothing))
UpdateCom.Parameters.Add(New OleDb.OleDbParameter("stari Broj
indeksa", OleDb.OleDbType.WChar, 0,
System.Data.ParameterDirection.Input, CType(0, Byte), CType(0, Byte),
"Broj indeksa", DataRowVersion.Original, True, Nothing))
UpdateCom.Parameters.Add(New OleDb.OleDbParameter("stari Broj
pohadjanja", OleDb.OleDbType.Integer, 0,
System.Data.ParameterDirection.Input, CType(0, Byte), CType(0, Byte),
"Broj pohadjanja", DataRowVersion.Original, True, Nothing))
For i = 5 To
Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Count
- 1
UpdateCom.Parameters.Add(New OleDb.OleDbParameter("stari " &
Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Item(i).ColumnName,
_
OleDb.OleDbType.Integer, 0, System.Data.ParameterDirection.Input,
CType(0, Byte), CType(0, Byte),
Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Item(i).ColumnName,
DataRowVersion.Original, True, Nothing))
Next i
InsertCom.Connection = veza
InsertCom.CommandType = CommandType.Text
InsertCom.CommandText = "INSERT INTO " & nova1 & " (" &
InsSql(ComboBox1.Text, "Prvi") & ")" & " VALUES " & "(" &
InsSql(ComboBox1.Text, "Drugi") & ")" '" ([Broj iksice], [Prezime],
[Ime], [Broj indeksa], " & _
'"[Broj pohadjanja]) VALUES (?,?,?,?,?)"
InsertCom.Parameters.Add("Broj iksice", OleDb.OleDbType.Double, 0,
"Broj iksice")
InsertCom.Parameters.Add("Prezime", OleDb.OleDbType.WChar, 0,
"Prezime")
InsertCom.Parameters.Add("Ime", OleDb.OleDbType.WChar, 0, "Ime")
InsertCom.Parameters.Add("Broj indeksa", OleDb.OleDbType.WChar, 0,
"Broj indeksa")
InsertCom.Parameters.Add("Broj pohadjanja", OleDb.OleDbType.Integer,
0, "Broj pohadjanja")
For i = 5 To
Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Count
- 1
InsertCom.Parameters.Add(Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Item(i).ColumnName,
_
OleDb.OleDbType.Integer, 0,
Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Item(i).ColumnName)
Next i
DeleteCom.Connection = veza
DeleteCom.CommandType = CommandType.Text
DeleteCom.CommandText = "DELETE * FROM " & nova1 & " WHERE [Broj
iksice]=?"
DeleteCom.Parameters.Add(New OleDb.OleDbParameter("Broj iksice",
OleDb.OleDbType.Double, 0, System.Data.ParameterDirection.Input,
False, CType(20, Byte), CType(0, Byte), "Broj iksice",
DataRowVersion.Original, Nothing))
DeleteCom.Parameters.Add(New OleDb.OleDbParameter("Prezime",
OleDb.OleDbType.WChar, 0, System.Data.ParameterDirection.Input,
False, CType(0, Byte), CType(0, Byte), "Prezime",
DataRowVersion.Original, Nothing))
DeleteCom.Parameters.Add(New OleDb.OleDbParameter("Ime",
OleDb.OleDbType.WChar, 0, System.Data.ParameterDirection.Input,
False, CType(0, Byte), CType(0, Byte), "Ime",
DataRowVersion.Original, Nothing))
DeleteCom.Parameters.Add(New OleDb.OleDbParameter("Broj indeksa",
OleDb.OleDbType.WChar, 0, System.Data.ParameterDirection.Input, True,
CType(0, Byte), CType(0, Byte), "Broj indeksa",
DataRowVersion.Original, Nothing))
DeleteCom.Parameters.Add(New OleDb.OleDbParameter("Broj pohadjanja",
OleDb.OleDbType.Integer, 0, System.Data.ParameterDirection.Input,
True, CType(0, Byte), CType(0, Byte), "Broj pohadjanja",
DataRowVersion.Original, Nothing))
For i = 5 To
Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Count
- 1
DeleteCom.Parameters.Add(Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Item(i).ColumnName,
_
OleDb.OleDbType.Integer, 0,
Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)).Columns.Item(i).ColumnName)
Next i

nova.SelectCommand = SelectCom
nova.UpdateCommand = UpdateCom
nova.DeleteCommand = DeleteCom
nova.InsertCommand = InsertCom


If nova2 = "Fill" Then
nova.Fill(Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)))
End If
If nova2 = "Update" Then
nova.Update(Form2.Studenti1Data.Tables.Item(Index(ComboBox1.Text)))
End If
veza.Close()
End Sub


InsSql(ComboBox1.Text, string) creates new sql commands with new
column. The code is:


Public Function InsSql(ByVal imeTab As String, ByVal tipSql As
String) As String 'Generira nastavak sql komande za update i insert
Dim i As Integer
Dim sql As String = "[Broj iksice]"
If tipSql = "Prvi" Then
For i = 1 To
Form2.Studenti1Data.Tables.Item(Index(imeTab)).Columns.Count - 1
sql = sql & ", [" &
Form2.Studenti1Data.Tables.Item(Index(imeTab)).Columns.Item(i).ColumnName
& "]"
Next i
End If
If tipSql = "Drugi" Then
sql = "?"
For i = 0 To
Form2.Studenti1Data.Tables.Item(Index(imeTab)).Columns.Count - 2
sql = sql & ", ?" 'Ove dvije if petlje za insert komandu
Next i
End If
If tipSql = "Treci" Then
sql = "[Broj iksice]=?"
For i = 1 To
Form2.Studenti1Data.Tables.Item(Index(imeTab)).Columns.Count - 1
sql = sql & ", [" &
Form2.Studenti1Data.Tables.Item(Index(imeTab)).Columns.Item(i).ColumnName
& "]" & "=?" 'Za Update komandu
Next i
End If
Return sql
End Function

Dataset is Studenti1data, I made it by the new data source
wizard,and made datagridview and bindingsource draging Table1 to
Form2. I use Access database.
Please help....!
 

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

Similar Threads


Top