troubleshooting Question

Select data from multiple rows from SQL

Avatar of cbrick04
cbrick04 asked on
Microsoft SQL Server
7 Comments1 Solution304 ViewsLast Modified:
I have a Table(PicturesTable) with some URLS for some images,:

Image ID       PicURL            Page
----------        -----------       ------
1                  Image1             1
2                  Image2             1
3                  Image3             2
4                  Image4             2


I would like to pull the data and assign the URLS to some image buttons where the page column is = 1;

This is what I have so far, it works for one image , but when I add the second, I get an error "Index was outside the bounds of the array."

//Get Pic info from database
        SqlConnection cn1 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["PicturesConnectionString"].ConnectionString);
        SqlCommand cmd1 = new SqlCommand("Select PicURL  from [PicturesTable] where Page = '" + 1 + "'", cn1);
        cn1.Open();
        SqlDataReader reader1 = cmd1.ExecuteReader();
        while (reader1.Read())
        {
            Picture1  = reader1.GetValue(0).ToString();
            Picture2 = reader1.GetValue(1).ToString();
        }
        reader1.Close();
        cn1.Close();
        ImageButton1.ImageUrl =  Picture1;
        ImageButton2.ImageUrl =  Picture2;
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 7 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 7 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros