I am using a sqldatareader inside another sqldatareader declaring MultipleActiveResultSets=True. But it is giving an exception "invalid attempt to call read when reader is closed"
con.ConnectionString = "Data Source=HELLO-PC; Initial Catalog=hrmdb; Integrated Security=True; MultipleActiveResultSets=True"
con.Open()
Dim cmd2 As New SqlCommand("select count(*) from empsal", con)
Dim dr2 As SqlDataReader = cmd2.ExecuteReader()
While dr2.Read()
Dim count As Integer = dr2(0).ToString
End While
dr2.Close()
Dim cmd3 As New SqlCommand("select emplycode from empsal", con)
Dim dr3 As SqlDataReader = cmd3.ExecuteReader()
While dr3.Read()
code1 = dr3(i2).ToString
Dim cmd1 As New SqlCommand("select total from empsal where emplycode='" + code1 + "'", con)
Dim dr10 As SqlDataReader = cmd1.ExecuteReader()
While dr10.Read()
tot = dr10(0).ToString
End While
dr10.Close()
pTax = professionalTax(tot)
ESI(tot)
PF(code1, tot)
adDebits = addDebits()
eDebits = extraDebits(code1)
iTax = IncomeTax(tot)
txfrm = taxForm(code1, tot)
abf = absentFine(code1, tot)
totsal1 = tot - (Convert.ToDouble(pTax.ToString) + Convert.ToDouble(adDebits.ToString) + Convert.ToDouble(eDebits.ToString) + Convert.ToDouble(iTax.ToString) + Convert.ToDouble(txfrm.ToString) + Convert.ToDouble(abf.ToString) + Convert.ToDouble(esi1.ToString) + Convert.ToDouble(esi2.ToString) + Convert.ToDouble(pf1.ToString) + Convert.ToDouble(pf2.ToString))
com = New SqlCommand("INSERT INTO fnlreport (enumbr,pcd,pnm,dcd,dnm,totsal,pt,it,pf,cpf,esi,cesi,debits,edebits,absent,txfrm,tot,mnth1,year1) VALUES ('" + code1 + "','" + TextBox7.Text + "','" + ComboBox7.Text + "','" + TextBox8.Text + "','" + ComboBox8.Text + "','" + tot.ToString + "','" + pTax.ToString + "','" + iTax.ToString + "','" + pf1.ToString + "','" + pf2.ToString + "','" + esi1.ToString + "','" + esi2.ToString + "','" + adDebits.ToString + "','" + eDebits.ToString + "','" + abf.ToString + "','" + txfrm.ToString + "','" + totsal1.ToString + "','" + TextBox9.Text + "','" + TextBox10.Text + "')", con)
com.ExecuteNonQuery()
i2 = i2 + 1
End While
dr3.Close()
con.Close()
First row in the table gets inserted and exception occurs at "while dr3.Read()"
Usingkeyword on all theIDisposableobjects.