Click here to Skip to main content
15,901,505 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The issue I am having is that when I use my code it uses the same referenced Row in all the insert into select statement. For instance, if the row is 4097 and I am running my code to insert 4 records it will repeat that same number in the column.

Here is my code:

Dim lastRow = dtc.Rows.Cast(Of DataRow)().LastOrDefault()
       Dim I As Integer = 0
       I += 1

       Dim laststr As String = lastRow.Item("ID").ToString + I

       'This is for personal information input

       Using sqlconn As New SqlConnection("SQLstring")

           Using da As New SqlDataAdapter(String.Empty, sqlconn)

               Dim sql As New StringBuilder("insert into [")
               sql.Append(TreeView1.SelectedNode.Text.Replace("]", "\]"))
               sql.Append("] (")

               Dim sql1 As New StringBuilder("")

               Dim andRequired As Boolean = False

               If datat.Columns.Contains("ID") Then

                   If andRequired Then
                       sql.Append(", ")
                       sql1.Append(", ")
                   End If

                   sql.Append("[Individual_ID]")
                   sql1.Append(laststr)
                   andRequired = True

               End If

               sql.AppendFormat(") Select " + sql1.ToString + " from table2")
               da.SelectCommand.CommandText = sql.ToString()

               MessageBox.Show(sql.ToString)

               da.Fill(datat)

           End Using

       End Using


How can I get the row number of each new row added into the column?

What I have tried:

I tried to iterate using the I as integer.

Dim I As Integer = 0
      I += 1

      Dim laststr As String = lastRow.Item("ID").ToString + I


but it only adds just the first new row number.
Posted
Updated 15-Aug-18 4:33am
Comments
CHill60 15-Aug-18 14:06pm    
Show us the actual sql statement so I can get my head around what you're trying to do. Some sample data and expected results would be useful too
Member 11856456 15-Aug-18 16:12pm    
I have 5 tables for now. each table has an individual_ID column that is used for referring to the ID column of my main table. When I click on the name in datagridview I use the ID to get all records related to this ID from other tables.

Tables:
1. ID, First_name, Last_name, DOB
2. ID, Job_title, pay, Idividual_id
3. ID, Time_of_employeement, Individual_ID
just to give you an idea

CHill60 16-Aug-18 5:51am    
Show us the actual sql statement so I can get my head around what you're trying to do. Some sample data and expected results would be useful too
[no name] 15-Aug-18 15:24pm    
Get the "id"; "look" at it (to see if it actually contains what you expect); then increment it; then convert it to a string.

... instead of converting it to a string; "concatenating" an integer to it; and expecting something meaningful.
Member 11856456 15-Aug-18 16:39pm    
On my main table I want to use the autoincremented ID column to be the reference to the other tables.

1. ID, First_name, Last_name, DOB
2. ID, Job_title, pay, Idividual_id
3. ID, Time_of_employeement, Individual_ID

So, the Invidiual_ID column of table 2 if there is information related to that person should contain the number of the ID column of the main table, table 1. same with the 3rd table. I am importing data into these tables, but I want to make sure that they reference the right person.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900