mails4vijay 0 Newbie Poster

in one single button click i need to execute one insert query and two update query in three tables. i tried this query but i got error as "There is already an open DataReader associated with this Command which must be closed first."

here is my code please help me!

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
con.Open()
Dim cmd2 As New SqlCommand
Dim custype As String
If tenlt.Checked = True Then
custype = "A"
cmd2 = con.CreateCommand
cmd2.CommandText = "INSERT INTO sale (sal_dt,csn,cus_id,ec_red) values ('" & (DateTimePicker1.Value) & "','" & Trim(cnfrmtxt.Text) & "','" & Trim(idtxt.Text) & "','" & Trim(emptytxt.Text) & "')"
cmd2.ExecuteNonQuery()
ElseIf twentylt.Checked = True Then
custype = "B"
cmd2 = con.CreateCommand
cmd2.CommandText = "INSERT INTO sale (sal_dt,csn,cus_id,ec_red) values ('" & (DateTimePicker1.Value) & "','" & Trim(cnfrmtxt.Text) & "','" & Trim(idtxt.Text) & "','" & Trim(emptytxt.Text) & "')"
'cmd2.ExecuteNonQuery()
check = cmd2.ExecuteReader.RecordsAffected()
dr.Close()

Dim cmd10 As New SqlCommand("SELECT * FROM purchase WHERE vou_dt = '" & Trim(DateTimePicker1.Value) & "'", con)
cmd10.ExecuteScalar()
If (dr.HasRows) Then
dr.Read()
Dim cmd6 As New SqlCommand("update purchase set ts_cn20 = ts_cn20 + 1 where vou_dt= '" & (DateTimePicker1.Value) & "'", con)
cmd6.ExecuteNonQuery()
dr.Close()
Else
Dim cmd8 As New SqlCommand
cmd8 = con.CreateCommand
cmd8.CommandText = "INSERT INTO purchase (ts_cn20) values ('1')"
cmd8.ExecuteNonQuery()
End If


ElseIf twentyfivelt.Checked = True Then
custype = "C"
cmd2 = con.CreateCommand
cmd2.CommandText = "INSERT INTO sale (sal_dt,csn,cus_id,ec_red) values ('" & (DateTimePicker1.Value) & "','" & Trim(cnfrmtxt.Text) & "','" & Trim(idtxt.Text) & "','" & Trim(emptytxt.Text) & "')"
cmd2.ExecuteNonQuery()
End If
Dim dlgRes As DialogResult
dlgRes = MessageBox.Show((cnfrmtxt.Text) & "Details Succesfully Added", "saled ", MessageBoxButtons.OK, MessageBoxIcon.None)
If dlgRes = Windows.Forms.DialogResult.OK Then
idtxt.Text = ""
emptytxt.Text = ""
nametxt.Text = ""
tenlt.Visible = False
twentylt.Visible = False
twentyfivelt.Visible = False
Else
MsgBox(Trim(nametxt.Text) & " Details failed to added please enter Details ", MsgBoxStyle.OkOnly, "New ")
End If
Dim cmd3 As New SqlCommand("update cupon set salflg = @salflg Where (csn = @csn) ", con)
With cmd3.Parameters.Add(New SqlParameter("@salflg", "R"))
End With
With cmd3.Parameters.Add(New SqlParameter("@csn", Me.cnfrmtxt.Text))
End With
cmd3.ExecuteNonQuery()
con.Close()
End Sub
End Class