dim x as integer = 0
        str = "SELECT uname, full_name FROM EMPLOYEE"
        cmd = New MySqlCommand(str, cn)
        ds = New DataSet
        da = New MySqlDataAdapter(str, cn)
        da.Fill(ds, "employee")
        Dim uname As String = ds.Tables(0).Rows(0)("uname").ToString
        Dim emp_name As String = ds.Tables(0).Rows(0)("full_name").ToString
        While x <= ds.Tables(0).Rows.Count
            If ds.Tables(0).Rows.Count <> 0 Then

                ' MsgBox("!")
                str = "SELECT * FROM attendance where date = '" & Label3.Text & "' and uname = '" & uname & "' and emp_name = '" & emp_name & "'"
                cmd = New MySqlCommand(str, cn)
                ds = New DataSet
                da = New MySqlDataAdapter(str, cn)
                da.Fill(ds, "attendance")
                While x <= ds.Tables(0).Rows.Count
                    If ds.Tables(0).Rows.Count = False Then

                        cmd.CommandText = "INSERT INTO ATTENDANCE(date,emp_name,tin1,uname)VALUES('" & Label3.Text & "', '" & emp_name & "', 'ABSENT', '" & uname & "')"
                        cmd.ExecuteNonQuery()



                    End If
                    x = x + 1
                End While
            End If
            x = x + 1
            cn.Close()

        End While

This is my code of inserting multiple records using while loop but it only inserts a single record. I want to insert multiple records at the same using this. How can I do this. Please check my code. Any help is much appreciated.thank you.

Recommended Answers

All 2 Replies

You are setting uname and emp_name outside of the while loop so they become set to the data from the first row of the dataset and they never change. This means that inside your while loop your SELECT and INSERT statements are always using the same values.
Simply move the lines where you set uname and emp_name to inside the while loop and use the x variable as your row indicator

Dim uname As String = ds.Tables(0).Rows(x)("uname").ToString
Dim emp_name As String = ds.Tables(0).Rows(x)("full_name").ToString

tnx for ur help. It works!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.