I have a stored procedure that returns multiple recordsets. It can be 1 recordset, 2 recordsets, or more. I don’t know how many RS will come back.
Here at stackoverflow I found the below sample
Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("myConnection").ConnectionString)
Dim cmd As New SqlCommand
Dim Reader As SqlDataReader
cmd.Connection = conn
cmd.Parameters.AddWithValue("@ACTNbr", tbACTNbr.Text.ToString.Trim)
cmd.Parameters.AddWithValue("@WTHNbr", tbWTHNbr.Text.ToString.Trim)
cmd.CommandText = "sp_search_def"
cmd.CommandType = CommandType.StoredProcedure
conn.Open()
Reader = cmd.ExecuteReader()
'The next part is what I found here at stackoverflow
While Reader.Read() OrElse (Reader.NextResult() And Reader.Read())
For i As Integer = 0 To Reader.FieldCount - 1
Response.Write(Reader(i).ToString())
Response.Write(" ")
Next
Response.Write("<br />")
End While
The above response.write‘s show the data I need perfectly. But I need to put that into a Gridview. i.e. I need to put the results of the stored procedure (and all of its result sets) into one gridview.
My gridview is set to AutoGenerateColumns = "true".
I’ve tried:
myGridview.DataSource = Reader
myGridview.DataBind()
And of course I only get one of the recordsets.
The results of the stored procedure are all formatted the same – same number of columns, headers, and so on.
Can somebody point me in the right direction? I’ve been trying to figure this out, but give up and now I am asking here.
I am new to this.
Thank you.
Use DataAdapter to fill DataSet instead of DataReader, that you can easily bind to your gridview as shown below:
How to call SQL Server stored procedures in ASP.NET by using Visual Basic .NET