0

My home page is displaying images slideshow using jquery,all these images are retrieving from mysql during the run time.I have created 9 images inside the division which are moving by jquery and i am giving the value of image url from codebehind.please see my code below,if database contains lesser than 9 images,null image should be hidden.how can i do it?

 <a href="#">

  DataTable dt = Db.ids("home_table");
Image1.ImageUrl = "~/Handler1.ashx?id=" + Convert.ToInt16(dt.Rows[0]["id"].ToString());


Image2.ImageUrl = "~/Handler1.ashx?id=" + Convert.ToInt16(dt.Rows[1]["id"].ToString());

This method is followed for all 9 images...

1
  • Just a comment. I don't see why you are converting a string to an Int16 and then back to a string. Just concatenate the database ToString() value straight on to the URL part. Commented Nov 26, 2012 at 11:29

3 Answers 3

2

You get an error because you directly access rows that do not exist. You need to count the numbers of rows in your DT.

numberOfRows = dt.Rows.Count 

And after that

if ( numberOfRows >= 2 )
{
   Image2.ImageUrl = "~/Handler1.ashx?id=" + ...    }

if ( numberOfRows >= 3 )
{
   Image3.ImageUrl = "~/Handler1.ashx?id=" + ....    }

etc.

Sign up to request clarification or add additional context in comments.

3 Comments

yea..but now i want to add 50 images,some time database may contain less than 50 images,so what to do?
That's a different question. So your real question is that you want a flexible number of images in your DIV? In that case you cannot hardcode them in the page like you have done now with image1, image2 etc. You should use a repeater or dynamically add image controls to the page
yea..i added dynamic images,but,jquery is not moving the images if create image dynamically.All images should be inside a div,that div should not be used runat="server"
2

You can't concatenate a string and an integer:

Image1.ImageUrl = "~/Handler1.ashx?id=" + Convert.ToInt16(dt.Rows[0]["id"]);

Convert ToString() to resolve:

Image1.ImageUrl = "~/Handler1.ashx?id=" + Convert.ToInt16(dt.Rows[0]["id"]).ToString();

Comments

0

Two ways to solve.

Easy way:

You can create a static array of your Image controls (an array with 9 elements) then in a loop from zero to number of images-1. You then de-reference into that array to change the url. Should look like this (example code, non-compiled, might have bugs or typos)

Decl:

Image imageArray[] = { Image1, Image2 ... , Image9};

In the loop:

imageArray[index].ImageUrl = "~/Handler1.ashx?id=" + Convert.ToInt16(dt.Rows[index]["id"].ToString());

Or the hard way:

Dynamically add image controls to the page.

1 Comment

i have already tried this,but jquery is not moving the images when i add images codebehind,i must be able to add images inside the div without using "runtime=server" tag,then only jquery can move these images

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.