Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

DataReader Not Reading Right

Posted on 2004-09-17
12
Medium Priority
?
443 Views
Last Modified: 2012-05-05
Here is some data retrieved to DataReader.

Problem is it doesn't return the first row of retrieved data.  I am missing one Listing.

Also, the if/else statement seems to stop at the first if...meaning if displayYesNo=="T" all the rows will display "POST" even if displayYesNo=="F" in subsequent rows.
 
string Post;

SqlDataReader dr=cmd.ExecuteReader();
while (dr.Read())
{
string displayYesNo=dr[3].ToString();

            if(displayYesNo=="T")
      {
           Post="UNPOST";
      }
              else if (displayYesNo=="F")
      {
           Post="POST";
      }
Listings.DataSource=dr;
Listings.DataBind();
}
dr.Close();

0
Comment
Question by:champ_010
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 15

Assisted Solution

by:Timbo87
Timbo87 earned 800 total points
ID: 12088421
It's missing the first record because you used dr.Read() to read it, then you databind. The DataReader is a forward only, non-caching reader so it databinds the rest of the record set, but not the first record.
0
 
LVL 1

Author Comment

by:champ_010
ID: 12088611
So are you suggesting I use a DataSet instead?? If so , how can I refer to the column values that I need to perform the if/else? Or is there a solution for the existing DataReader?

DataSet ds=new DataSet();
SqlDataAdapter da=new SqlDataAdapter();
da.SelectCommand=cmd;
da.Fill(ds,"myList");

//How do I access the values of the third column of the retrieved DataTable so I can do the if/else?

      if(displayYesNo=="T")
      {
           Post="UNPOST";
      }
               else if (displayYesNo=="F")
      {
           Post="POST";
      }
Listings.DataSource=ds.Tables[0].DefaultView;
Listings.DataBind();
0
 
LVL 15

Expert Comment

by:Timbo87
ID: 12088688
string displayYesNo = ds.Tables[0].Rows[0][3].ToString();
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Author Comment

by:champ_010
ID: 12088751

o.k. this obviously only returns the first row only

increase Points to 200 if you can show me the foreach row to get the data for the if/else so that i can output a different value each time.
0
 
LVL 15

Expert Comment

by:Timbo87
ID: 12088810
foreach(DataRow dr in ds.Tables[0].Rows)
     // code
0
 
LVL 1

Author Comment

by:champ_010
ID: 12088859

o.k. I'm getting this in bits and pieces that i'm not sure how to put together because I'm not getting the output I need.

Can you please clarify:

foreach(DataRow dr in ds.Tables[0].Rows)
{
   string displayYesNo = //.....THIRD Column VALUES???

     if(displayYesNo=="T")
     {
          Post="UNPOST";
     }
               else if (displayYesNo=="F")
     {
          Post="POST";
     }
Listings.DataSource=ds.Tables[0].DefaultView;
Listings.DataBind();

}
0
 
LVL 15

Expert Comment

by:Timbo87
ID: 12088985
foreach(DataRow dr in ds.Tables[0].Rows)
{
   string displayYesNo = dr[3].ToString();

     if(displayYesNo=="T")
     {
          Post="UNPOST";
     }
               else if (displayYesNo=="F")
     {
          Post="POST";
     }
Listings.DataSource=ds.Tables[0].DefaultView;
Listings.DataBind();

}
0
 
LVL 1

Author Comment

by:champ_010
ID: 12089070
Something is obviously still wrong as I don't get the effect of the if/else showing up in my DataList.  if the first row is "F" i get "POST" for all output and vice versa.

Any ideas?

In my DataList I am trying to output the text for the LinkButton as "POST" or "UNPOST" but I only get one or the other.

<asp:DataList ID="Listings" OnItemCommand="myCommand"  runat="server">
      <ItemTemplate>
      <asp:LinkButton ID="PostStatus" CommandName="PostYesNo" Text="<%#Post%>" runat="server"/>
      </ItemTemplate>
  </asp:DataList>

Points to 250 if you can help me out here otherwise 200 points to close the question because when I Response.Write(displayYesNo); in the code I do see TTFFT so yes your code will loop the table but it won't output to the LinkButton with the correct values.  

0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 12089652
Hi there,
use RTrim function before comparing.

foreach(DataRow dr in ds.Tables[0].Rows)
{
   string displayYesNo = RTrim(dr[3].ToString();) // Here

     if(displayYesNo=="T")
     {
          Post="UNPOST";
     }
               else if (displayYesNo=="F")
     {
          Post="POST";
     }
}
Listings.DataSource=ds.Tables[0].DefaultView;  // and keep it outside foreach... you are binding it for every loop.
Listings.DataBind();

*** I am pretty doubtful about your logic. You are changing the value of Post with every row. Finally you are gonna end up with the final(very last) row value. So running a foreach loop is no good. What are you trying to do here?

-Baan
0
 
LVL 5

Accepted Solution

by:
ajitanand earned 1000 total points
ID: 12090271
Hello,

I believe you want to show "POST" / "UNPOST" in the datalist depending on whether you have T/F in your Database.

If yes then you are not following the correct Logic. I have a better, alternative logic, that will help you achieve this.

Here is the solution:
--------------------------

Step 1. In C# Code behind, no need to loop. Just do this:

//After Populating the DataSet, just Bind it with the List.

DataSet ds=new DataSet();
SqlDataAdapter da=new SqlDataAdapter();
da.SelectCommand=cmd;
da.Fill(ds,"myList");

Listings.DataSource=ds.Tables[0].DefaultView;
Listings.DataBind();

//And... Thats all!

Step 2. In your ASPX File, do this (replace the 3rdColumnName with your actual column name to cmpare with "T" or "F":

<asp:DataList ID="Listings" OnItemCommand="myCommand"  runat="server">
   <ItemTemplate>
     <asp:LinkButton ID="PostStatus" CommandName="PostYesNo"
        Text='<%# DataBinder.Eval(Container.DataItem, "3rdColumnName").ToString() == "T" ? "POST" : "UNPOST" %>'
        runat="server"/>
   </ItemTemplate>
</asp:DataList>


The key here is that we can use Ternory Operator in your ASPX file directly and get maximum efficiency, and above all, the correct logic. If you want more help on this, let me know I will explain.

warm regards,
Ajit Anand
0
 
LVL 1

Author Comment

by:champ_010
ID: 12090545

Thanks Ajit!  Clear simple and elegant.

I didn't think I was going to need to spend 450 points on this question since it seemed so straight forward but I had already promised 200 to Timbo87 for helping me out.  I decided to up the points and give you the 250 I offered him for a full solution because you've shown me something that I will come in handy for other things.  You read my question, understood what I was trying to get at and provided a clear and simple solution without me having to come back again and again to ask for the next step. I see you are new to EE--hope you stay around. Answers like the one you provided are the main reason I come back to EE time and again to learn.

0
 
LVL 5

Expert Comment

by:ajitanand
ID: 12090571
Thanks! Your feedback made my Day -Ajit
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

715 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