Hi everyone, can you explain me why I can't use two datareader in on procedure?
Here is the sample code:
Private Sub Do_Execute()
Dim conx as SqlConnection
Dim cmd1 as SqlCommand
Dim cmd2 as SqlCommand
Dim drd1 as SqlDataReader
Dim drd2 as SqlDataReader
conx = new SqlConnection("connection string")
conx.Open()
cmd1 = new SqlCommand("SELECT * FROM Category" , conx)
drd1 = cmd1.ExecuteReader()
While (drd1.Read())
{
Reading Data From drd1
}
cmd2 = new SqlCommand("SELECT * FROM Stock" , conx)
drd2 = cmd2.ExecuteReader()
While (drd2.Read())
{
Reading Data From drd2
}
End Sub
When I execute that program, It throws to the exception message : " There is already an open DataReader associates with this Command which must be closed first! "
When I closed drd1 before drd2 is initialized. It works.
Why I can't use like above code? Please explain me. Thanks in advance!