datagrid data not showing

B

Brent Burkart

I have a datagrid that simply shows readonly data that i pull using sql.
This was working fine and one day it stopped working.

It shows the headers, but no data. Can anyone point me in the right
direction? I am stuck.
 
A

Alvin Bruney

first check to see if all your handlers are mapped correctly
then check to see if data is being returned
 
B

Brent Burkart

I have checked this and I have hooked up Profiler to see if my query is
working. Not sure how to check for the data being returned. I assume it is
since I am checking for present data in a Sqldatareader.

Here is my code

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here


get_data()

Populate_Fields()

End Sub

Sub get_data()


myConnection = New SqlConnection("x")

myConnection.Open()

myCommand = New SqlCommand("select
BLASTNAME,BFIRSTNAME,BSOCSECNO,loan.LOANNUM,APPLTYPE,LOANSTAT,PROPADDR1 from
borrower, propchar, loan where loan.loanid = borrower.loanid and loan.loanid
= propchar.loanid and borrower.borrowercount = 1 and BSOCSECNO = '" &
Trim(Request.QueryString("SSN")) & "'", myConnection)

myDatareader = myCommand.ExecuteReader()

End Sub

Sub Populate_Fields()

If myDatareader.Read Then

myDataGrid.DataSource = myDatareader

myDataGrid.DataBind()

Else

Label1.Text = "No data found corresponding to an SSN of " &
Request.QueryString("ssn")

End If

myDatareader.Close()

myConnection.Close()

myDatareader = Nothing

myConnection = Nothing

myCommand = Nothing

End Sub
 
B

Brent Burkart

I am confused because as you can see in the code, I check to see if there is
data in my datareader, if there is then I databind to the datasource, if not
I display a message. However, the header of the datagrid shows up which
leads me to believe it thinks there is data, but it is just not showing.
 
A

Alvin Bruney

you have a couple of things wrong here
you should be using a dataset here, a datareader is typically used for
speedy access to single values
also, an exception in the datareader will leak memory and result in a
blocked datareader which is why it is better in your circumstance to use a
dataset. there is ample code on the net to show you how to do this. if you
insist on using a datareader, you need at least a try finally block or at
most some exception handling to catch an error and call close on the
datareader, then set it to null
also, to find the values in the datareader, you will need to loop thru the
reader to extract the values
string val1 = myDatareader[0].ToString()
string val2 = myDatareader[1].ToString() etc...
 
A

Alex

you should be using a dataset here, a datareader is typically used for
speedy access to single values
NOT true! DataReader is simply forward-only access to the data. What you're
talking about (single values) is ExecuteScalar!
Most of the time DataReader is the way to go if you only want to display
data.
 
A

Alvin Bruney

Yes, that's correct. My bad.

This makes my post technically inaccurate. Hope OP is aware of that.
 
B

ben

might not be a techie solution but i have had this problem a few times

-make sure the datagrid is declared(protected System.Web.UI.... datagrid1)
-make sure that all the components are initialized in the web form generated code

hope this helps..am a newbie...
Ben
 
B

Brent Burkart

I am an newbie also, but I am also kind of an idiot. The reason data was
not showing was because I was checking for data before I displayed the
datagrid. When I did the check (datareader.read), I used up the first and
only record, which left no more records for my datagrid.
 

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

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top