Click here to Skip to main content
15,895,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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...
C#
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
Updated 7-Jun-12 22:14pm
v2
Comments
Sandeep Mewara 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 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");

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:
XML
<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();

C#
protected string ChooseImage(string isOnlineValue)
{
    bool isOnline = Convert.ToBoolean(isOnlinevalue); 
	if (isOnline) {
		return "~/images/Green.gif";
	} else {
		return "~/images/Red.gif";
	}
}
 
Share this answer
 
v2
Comments
viddlov 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 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 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 8-Jun-12 7:49am    
Good to know.
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.
 
Share this answer
 
Comments
viddlov 8-Jun-12 5:26am    
I have tried looping from 1st row but still the same error is displayed...
Sandeep Mewara 8-Jun-12 7:49am    
Added my answer. Implementation was way wrong based on the requirement.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900