Problem with datareader and stored procedure

G

Guest

Hello Every one!

In my web form I am accessing a stored procedure. I assign resulting data from stored procedure to datareader and with datareader I try to fill up DropDown List. But when I try to run code I am getting en error "Invalid attempt to read when no data is present." My stored procedure dont return null result.
Following is the code. Plz guide me how to solve this problem!

Sub fillControl()
Dim conSelect As SqlConnection
Dim cmdSelect As SqlCommand
Dim drSelect As SqlDataReader

conSelect = New SqlConnection(ConfigurationSettings.AppSettings("strcon"))
cmdselect = New SqlCommand("GetData", conSelect)
cmdselect.CommandType = CommandType.StoredProcedure

Try
conSelect.Open()
drSelect = cmdSelect.ExecuteReader()
ddlMediproID.DataSource = drSelect
ddlMediproID.DataTextField = drSelect.Item("MediproID") '***
ddlMediproID.DataValueField = drSelect.Item("ClientID")
ddlMediproID.DataBind()
drSelect.Close()
conSelect.Close()
Catch ex As Exception
lblErr.Text = ex.Message
Exit Sub
End Try
end sub

I am getting error at line with '***

Thanking you in advance.
Shail
 
G

Guest

Hi
You have to specify the column name instead of column value as you have done.

So changing

ddlMediproID.DataTextField = drSelect.Item("MediproID") '***
ddlMediproID.DataValueField = drSelect.Item("ClientID")

to


ddlMediproID.DataTextField = "MediproID"
ddlMediproID.DataValueField = "ClientID"

should work fine.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top