?
Solved

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

Posted on 2012-08-13
6
Medium Priority
?
308 Views
Last Modified: 2012-08-14
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
0
Comment
Question by:dipster307
6 Comments
 
LVL 25

Expert Comment

by:Luis Pérez
ID: 38288300
Try this:

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

Hope that helps.
0
 

Author Comment

by:dipster307
ID: 38288551
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
 
LVL 20

Accepted Solution

by:
informaniac earned 1000 total points
ID: 38289096
Why is this line required?

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

rdr.Read().ToString() shouldn't return u anything.
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 70

Assisted Solution

by:Éric Moreau
Éric Moreau earned 1000 total points
ID: 38290014
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
 

Author Closing Comment

by:dipster307
ID: 38291393
I mis-understood how "rdr.Read()" works, now have better understanding.

Thank you :)
0
 

Author Comment

by:dipster307
ID: 38291397
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

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Screencast - Getting to Know the Pipeline

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question