champ_010
asked on
DataReader Not Reading Right
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].ToStrin g();
if(displayYesNo=="T")
{
Post="UNPOST";
}
else if (displayYesNo=="F")
{
Post="POST";
}
Listings.DataSource=dr;
Listings.DataBind();
}
dr.Close();
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].ToStrin
if(displayYesNo=="T")
{
Post="UNPOST";
}
else if (displayYesNo=="F")
{
Post="POST";
}
Listings.DataSource=dr;
Listings.DataBind();
}
dr.Close();
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
string displayYesNo = ds.Tables[0].Rows[0][3].To String();
ASKER
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.
foreach(DataRow dr in ds.Tables[0].Rows)
// code
// code
ASKER
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.Tab
Listings.DataBind();
}
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.Tab les[0].Def aultView;
Listings.DataBind();
}
{
string displayYesNo = dr[3].ToString();
if(displayYesNo=="T")
{
Post="UNPOST";
}
else if (displayYesNo=="F")
{
Post="POST";
}
Listings.DataSource=ds.Tab
Listings.DataBind();
}
ASKER
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(displayYesN o); 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.
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(displayYesN
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.Tab les[0].Def aultView; // 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
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.Tab
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
Thanks! Your feedback made my Day -Ajit
ASKER
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.Tab
Listings.DataBind();