We help IT Professionals succeed at work.

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
Comment
Watch Question

Luis PérezSoftware Architect in .Net
CERTIFIED EXPERT

Commented:
Try this:

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

Hope that helps.

Author

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?
Developer User Interface
CERTIFIED EXPERT
Commented:
Why is this line required?

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

rdr.Read().ToString() shouldn't return u anything.
Éric MoreauSenior .Net Consultant
CERTIFIED EXPERT
Top Expert 2016
Commented:
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)

Author

Commented:
I mis-understood how "rdr.Read()" works, now have better understanding.

Thank you :)

Author

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

Explore More ContentExplore courses, solutions, and other research materials related to this topic.