Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dropping Record Writing to DB

Status
Not open for further replies.

gdkz

Programmer
Nov 21, 2002
44
0
0
US
My problem is I am dropping the first record in my Dataset when writing to the DB.
Code:
Dim MyDataSet As New DataSet
MyDataAdaptor.Fill(MyDataSet)
Dim MyDataRow As DataRow
Dim strSQL As String = ""
Dim sqlConnection As SqlClient.SqlConnection
Dim sqlCommand As SqlClient.SqlCommand
Dim sqlCommand2 As SqlClient.SqlCommand
For Each MyDataRow In MyDataTable.Rows
  sqlConnection = New SqlClient.SqlConnectionstrConnection)
  sqlConnection.Open()
  strSQL = "INSERT INTO  dbo.inv1000_bar_code_uploaded_schools VALUES('" & _
                     MyDataRow(6) & "','" & _
                     MyDataRow(1) & "','" & _
                     MyDataRow(2) & "','" & _
                     MyDataRow(3) & "','','" & _
                     MyDataRow(4) & "','" & _
                     MyDataRow(5) & "','" & _
                     MyDataRow(0) & "')"
sqlCommand2 = New SqlClient.SqlCommand(strSQL,sqlConnection)
            sqlCommand2.ExecuteNonQuery()
            sqlConnection.Close()
Next

Can't figure out why I am not writing the first record to the DB. I have written out the Dataset and all records are included there. But, when I insert into the DB, the first record is absent.
Thanks

Thanks,
Greg
 
could you do a loop for each row of the dataset?

Code:
dim i as int = 0
do while i <= myDataTable.Rows.Count

 mydatatable.Rows(i)(6) & "','" & _
 etc...
 i = i + 1
loop

not sure if it's good practice to open the connection each time, or to leave it open then close it when you're done and close it if you catch an error...
 
Thanks for the help on the connection. I have tried looping through dataset, but I am still dropping the very first record.
When I display the dataset in a datagrid it shows all records. myDataTable.Rows.Count results is minus the first record when I check this value.
I dont understand why I am losing the very first record, like it does not exist?

Thanks,
Greg
 
Thanks, solved. Was reading from a text file into the data set. Did not take into consideration that the first row should contain headers and I was not supplying this.
Thanks again for the tips.

Thanks,
Greg
 
Thanks, solved. Was reading from a text file into the data set. Did not take into consideration that the first row should contain headers and I was not supplying this.
Thanks again for the tips.


Thanks,
Greg
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top