what is wrong with my sql reader, doest display any records.

I have the following code in asp.net using c#. My sqldatareader doesnt display data from the table, when there is data in it.

            conn.Open();
            SqlCommand comm = new SqlCommand("SELECT * FROM system_clientBenefitList", conn);
            SqlDataReader rdr = comm.ExecuteReader();

            //lblOutput2.Text += rdr["pensionID"].ToString() + "OUT";
            //lblOutput2.Text += rdr["lifeAssuranceID"].ToString() + "OUT";

            while (rdr.Read())
            {
                if (rdr.Read() == false)
                {
                    lblOutput2.Text += rdr["factfindID"].ToString();
                    lblOutput2.Text += rdr["pensionID"].ToString();

                    if (rdr["pensionID"].ToString() == "Add")
                    {
                        divPension.Style["display"] = "block";
                    }
                    else
                    {
                        divPension.Style["display"] = "none";
                    }
                    if (rdr["lifeAssuranceID"].ToString() == "Add")
                    {
                        divPension.Style["display"] = "block";
                    }
                    else
                    {
                        divPension.Style["display"] = "none";
                    }
                }
                else
                {
                    lblOutput2.Text += rdr.Read().ToString();
                }
            }

                rdr.Close();

Open in new window



The table is below picture
sql-table.JPG
dipster307Asked:
Who is Participating?
 
Obadiah ChristopherCommented:
Why is this line required?

if (rdr.Read() == false)
                {

rdr.Read().ToString() shouldn't return u anything.
0
 
Luis PérezSoftware Architect in .NetCommented:
Try this:

SqlCommand comm = new SqlCommand("SELECT * FROM system_clientBenefitList", conn);
comm.CommandType = CommandType.Text;
SqlDataReader rdr = comm.ExecuteReader();

Hope that helps.
0
 
dipster307Author Commented:
sorry that didnt work, and why would it, its not doing anything.

The thing is this works in Visual Studio 2008 but when I used the coding in Visual Studio 2010. It doesnt, is it my coding, or am I going mad?
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Éric MoreauSenior .Net ConsultantCommented:
for sure you are skipping the first row because every time you call the .Read method, you change row:

  while (rdr.Read())
            {
                if (rdr.Read() == false)
0
 
dipster307Author Commented:
I mis-understood how "rdr.Read()" works, now have better understanding.

Thank you :)
0
 
dipster307Author Commented:
I just need the following for it to work

if (rdr.Read())
            

Open in new window


removed...

  while (rdr.Read())
            {
                if (rdr.Read() == false) 

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.