Click here to Skip to main content
15,882,017 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi guys,

I want to Copy data from two tables and insert it into another table without any duplicate Values.

When I tried the below code, Only the first row is inserting.

What I have tried:

qr = "select emp_code,punch_time,MemberID,MemberName,EndDate from iclock_transaction JOIN Gym_Membership ON (emp_code=MemberID)  ORDER by punch_time  "
        ds = SearchData(qr)
        Dim ID As String
        Dim Count As String
        Dim Days As String
        Dim j As Integer

        Dim MemberID As String
        Dim MemberName As String
        DashBoard.RichTextBox2.Clear()
        DashBoard.PictureBox5.Image = Nothing

        Count = ds.Tables(0).Rows.Count
        DashBoard.Label14.Text = Count

        Try

            For i = 0 To ds.Tables(0).Rows.Count - 1
                Days = DateDiff(DateInterval.Day, Today.Date, ds.Tables(0).Rows(i)("EndDate"))
                MemberID = ds.Tables(0).Rows(i)("MemberID")
                MemberName = ds.Tables(0).Rows(i)("MemberName")
                DashBoard.Label13.Text = Days


                qr = "Select * from Gym_Attendence where ID ='" & MemberID & "' "
                ds = SearchData(qr)

                If ds.Tables(0).Rows.Count > 0 Then
                Else

                    qr = "Insert into Gym_Attendence values('" & MemberID & "','" & MemberName & "','" & Now.ToString("MM/dd/yyyy HH:mm:ss") & "')"
                    Dim logincorrect As Boolean = Convert.ToBoolean(InsertData(qr))

                End If

            Next

        Catch
        End Try
Posted
Updated 2-Feb-23 1:02am
v3
Comments
Richard Deeming 1-Feb-23 7:22am    
Well, fix it so that it is working then.

Seriously, "not working" tells us precisely nothing. You need to click the green "Improve question" link and update your question to include a clear and precise description of the problem, including the full details of any errors, full details of what you have tried and where you are stuck.

AS Richard has said, don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

Doing that through your whole app may fix the problem as well, but if it doesn;t then move to the next serious mistake you are making: exception swallowing.

Your code does this:
VB
Try

    For i = 0 To ds.Tables(0).Rows.Count - 1
         ...
    Next

Catch
End Try
So if anything goes wrong inside the loop, you throw away any error information and exit. That means that you don't know what went wrong, and have no way to find out!
That's called "Swallowing an Exception" and it's easy to fix: Use Catch with an exception parameter. Try...Catch...Finally statement - Visual Basic | Microsoft Learn[^] shows you how.
Now run your code in the debugger and if any exception occurs you can look at the details in the Catch block by putting a breakpoint on it. Those will tell you what happened, and where - which is the first part of trying to fix it!
 
Share this answer
 
Comments
zakariyaptpl 1-Feb-23 11:20am    
Thank you for your advice.

let me check.
OriginalGriff 1-Feb-23 11:30am    
You're welcome!
Check if the InsertData method is executing correctly and returning a boolean value as expected.
Use a different approach to insert the data into the Gym_Attendence table. For example, you could use a DataAdapter and DataSet to insert the data into the table and avoid any duplicates.

qr = "select emp_code,punch_time,MemberID,MemberName,EndDate from iclock_transaction JOIN Gym_Membership ON (emp_code=MemberID)  ORDER by punch_time  "
ds = SearchData(qr)
Dim ID As String
Dim Count As String
Dim Days As String
Dim j As Integer

Dim MemberID As String
Dim MemberName As String
DashBoard.RichTextBox2.Clear()
DashBoard.PictureBox5.Image = Nothing

Count = ds.Tables(0).Rows.Count
DashBoard
 
Share this answer
 

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