Question I need to correct this code !

ebrahim

Member
Joined
Jul 13, 2011
Messages
8
Programming Experience
Beginner
hi all

i am trying to write code to insert selected rows from Table1 to Table2 ,, so i find code do that with DataGradView and i try to Modifies it to work with Multi records " Without DataGradView"

Dim connectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\DellXPS\Desktop\DBtest.accdb"

Using connection As New OleDbConnection(connectionString)

Dim cmdText As String = "INSERT INTO Tbl2 (F1, F2) VALUES (@F1TextBox, @F2TextBox)"
Dim command As New OleDbCommand(cmdText, connection)
command.Parameters.Add(New OleDbParameter("@F1TextBox", OleDbType.LongVarWChar))
command.Parameters.Add(New OleDbParameter("@F2TextBox", OleDbType.LongVarWChar))


connection.Open()
Dim transaction As OleDbTransaction = connection.BeginTransaction()
command.Transaction = transaction

Try

For i As Integer = 0 To DBtestDataSet.Tbl1.Rows.Count - 1
command.Parameters("@F1TextBox").Value = DBtestDataSet.Tables[0].Rows[0].ToString()
command.Parameters("@F2TextBox").Value = DBtestDataSet.Tables[0].Rows[1].ToString()

command.ExecuteNonQuery()
Next i

transaction.Commit()
Catch ex As Exception

Try
transaction.Rollback()

Catch rollBackEx As Exception
MessageBox.Show(rollBackEx.Message)
End Try

End Try

End Using


the error it was in that line :
command.Parameters("@F1TextBox").Value = DBtestDataSet.Tables[0].Rows[0].ToString()

look at my example :
WindowsApplication1.zip
 
Last edited:
Back
Top