0

hello quys, gota a problem here. i have two datareaders.reade1 reads data from one table and read2 from another.i want to loop through reader1 and reader2 so that records that do not exist in reader2 are inserted to the database table and if they exist they are updated.
here is my cord that runs but nothing is writen to database

If con.State = ConnectionState.Open Then
            con.Close()
        End If
        con = New SqlConnection(cs1)
        con.Open()
        Dim cmd2 = New SqlCommand("SELECT * FROM tblExamResults WHERE class='" & cmbClass.Text & "'and examType = '" & cmbExamType.Text & "' and term = '" & cmbTerm.Text & "' and yr= '" & cmbYr.Text & "'")
        cmd2.Connection = con
        Dim rdr1 As SqlDataReader
        rdr1 = cmd2.ExecuteReader

        Dim sname, adm, class1, yr, term, examType, eng, kisw, math, bio, chem, phy, hist, cre, geo, agr, biz, total, pos, grade As String
        While rdr1.HasRows
            sname = rdr1.Item("sname")
            adm = rdr1.Item("adm")
            class1 = rdr1.Item("class")
            yr = rdr1.Item("yr")
            examType = rdr1.Item("examType")
            term = rdr1.Item("term")
            math = rdr1.Item("math")
            eng = rdr1.Item("eng")
            kisw = rdr1.Item("kisw")
            bio = rdr1.Item("bio")
            chem = rdr1.Item("chem")
            phy = rdr1.Item("phy")
            hist = rdr1.Item("hist")
            cre = rdr1.Item("cre")
            geo = rdr1.Item("geo")
            agr = rdr1.Item("agr")
            biz = rdr1.Item("biz")
            total = rdr1.Item("total")
            pos = rdr1.Item("pos")
            grade = rdr1.Item("grade")
            For Each row1 As DataRow In rdr

                For Each row2 As DataRow In rdr1

                    If row1.ToString <> row2.ToString Then

                        con.Open()
                        Dim comm = New SqlCommand("insert into tblExamResults(sname,adm,class,examType,yr,math,eng,kisw,bio,chem,phy,hist,cre,geo,biz,total,agr,term,pos) values('" & sname & "','" & adm & "','" & class1 & "','" & examType & "','" & yr & "','" & math & "','" & eng & "','" & kisw & "','" & bio & "','" & chem & "','" & phy & "','" & hist & "','" & cre & "','" & geo & "','" & biz & "','" & total & "','" & agr & "','" & term & "','" & pos & "')")
                        comm.ExecuteNonQuery()
                        MsgBox("record updated successfully", MsgBoxStyle.Information)
                    Else
                        Dim comm = New SqlCommand("update tblExamresults set adm='" + adm + "',class='" + class1 + "',sname='" + Name + "',examType='" + examType + "',yr='" + yr + "',math='" + math + "',eng='" + eng + "',kisw='" + kisw + "',bio='" + bio + "',chem='" + chem + "',phy='" + phy + "',hist='" + hist + "',cre='" + cre + "',geo='" + geo + "',agr='" + agr + "',biz='" + biz + "',total='" + total + "',pos='" + pos + "',term='" + term + "'")

                        comm.Connection = con
                        comm.ExecuteNonQuery()

                    End If

                Next
            Next
        End While
        MsgBox("record saved successfully", MsgBoxStyle.Information)
        'End If
        con.Close()




    End Sub
2
Contributors
1
Reply
9
Views
2 Years
Discussion Span
Last Post by cgeier
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.