Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET .NET ADO.NET
Hi
 
I have a requirement where i have to display a image based on database value. i have a column in database which stores true or false and im retriving data from database and storing in a dataset , now i need to display image in datagrid column when data from database is true and shouldnot display the image when its false...
 
i have written some code which is not working... i dont where i went wrong...
for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
        {
            Boolean abc = Convert.ToBoolean(ds1.Tables[0].Rows[i][2]);
          if (abc == true)
            {
               System.Web.UI.WebControls.Image img = (System.Web.UI.WebControls.Image)gridonlinestud.Rows[i].FindControl("imgOnline");
                img.ImageUrl = "../images/dot.jpg";
            }
            else if(abc == false)
           {
                System.Web.UI.WebControls.Image img = (System.Web.UI.WebControls.Image)gridonlinestud.Rows[i].FindControl("imgOnline");
                img.Visible = false;
            }
        }
please can anyone helpme with this issue...
 
Thanks
Vidya
Posted 7-Jun-12 20:53pm
viddlov399
Edited 7-Jun-12 22:14pm
v2
Comments
Sandeep Mewara at 8-Jun-12 4:16am
   
The way you have coded has coupled your grid data and the dataset from database - assuming that is what you intend to do, what is the issue? Whats the workflow and final result?
viddlov at 8-Jun-12 5:15am
   
Hi Sandeep,
 
The following error is been displayed when im running the program..
 
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
at System.Web.UI.WebControls.Image img = (System.Web.UI.WebControls.Image)gridonlinestud.Rows[i].FindControl("imgOnline");
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I think its the index which refers to Grid header is causing the issue. 0th index always refers to Header data. Hence it might not be able to find the image in that row. Try looping from 1st row of the grid.
  Permalink  
Comments
viddlov at 8-Jun-12 5:26am
   
I have tried looping from 1st row but still the same error is displayed...
Sandeep Mewara at 8-Jun-12 7:49am
   
Added my answer. Implementation was way wrong based on the requirement.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi Sandeep, The following error is been displayed when im running the program.. Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index at System.Web.UI.WebControls.Image img = (System.Web.UI.WebControls.Image)gridonlinestud.Rows[i].FindControl("imgOnline");
clearly you did not read my comment properly. Now, based on the error you share, it is clear that the dataset row count based on which you are looping is more than the number of rows you have in your grid.
 
Use two for loops one in other, find the row of dataset that presents data in grid and then use your logic. Currently, your mapping looks wrong and the dataset does not hold same data as grid has.
 
UPDATED:
my requirement is simple chat application.. when users logs in green image has to be displayed and when user logsout image has to disappear... so in my database, i have column online.. and when user logs in it is updated as true and when he logs out it is updated as false
When you fetch the data from database, you have something like:
Usernames IsOnline
Abc       True
Xyz       False
..
..
QWE       False
 
Now, you can tie your image source to the boolean field like:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:Image ID="imgOnline" ImageUrl='<%# ChooseImage(Eval("IsOnline")) %>' runat="server" />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
myGrid.DataSource = currentDataFromDataBaseInaDataTable;
myGrid.DataBind();
 
protected string ChooseImage(string isOnlineValue)
{
    bool isOnline = Convert.ToBoolean(isOnlinevalue); 
	if (isOnline) {
		return "~/images/Green.gif";
	} else {
		return "~/images/Red.gif";
	}
}
  Permalink  
v2
Comments
viddlov at 8-Jun-12 6:12am
   
thanks for ur reply sandeep, but i couldn't understand what you are trying to say..i couldn't understand what the second loop should contain.. my requirement is simple chat application.. when users logs in green image has to be displayed and when user logsout image has to disappear... so in my database, i have column online.. and when user logs in it is updated as true and when he logs out it is updated as false... so can u help me with some sample code...
Sandeep Mewara at 8-Jun-12 6:46am
   
It matters a lot and makes a difference once you explain your whole problem and what you are trying to do.
 
Based on what you say, updated my answer.
viddlov at 8-Jun-12 7:44am
   
only one change i had to make is
protected string ChooseImage(Object isOnlineValue)..
Thank you for all your replies.. it worked for me...
Sandeep Mewara at 8-Jun-12 7:49am
   
Good to know.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 8 Jun 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100