问题描述:

i am getting image urls path from sql.

now my code to display Image in asp.net asp:Image is

con = new SqlConnection(connect);

cmd = new SqlCommand();

cmd.Connection = con;

cmd.CommandText = "select_performer_Gold";

cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.AddWithValue("@Performance_Category", Performance_Category);

con.Open();

SqlDataReader dr = cmd.ExecuteReader();

while (dr.Read())

{

goldimg1.ImageUrl = "User_Images/" + dr["User_image"].ToString();

goldimg2.ImageUrl = "User_Images/" + dr["User_image"].ToString();

}

problem is while loop runs 2 time as i have 2 rows in DB

so in 1st loop both the image urls are given 1st row Image path

and in 2nd loop both image urls are given 2nd row Image path

So only 2nd row User_Image are displayed in both goldimg1 and goldimg2.

How to display respective image in goldimg1 and goldimg2

网友答案:

For better result you have to use SqlDataAdapter

First :retrns result in Dataset

then bind it with goldimg1.ImageUrl

Example:

goldimg1.ImageUrl=ds.Table[0].Rows[0]["User_image"].ToString();
goldimg2.ImageUrl=ds.Table[0].Rows[1]["User_image"].ToString();    //you can also use forech loop
网友答案:

This Code help you to get all the image path from database.

before using of for loop you have to take <image> control in the GridView or ListView

 if(ds.Tables[0].Rows.Count>0)
    {
      for(int i=0;i<=ds.Rows.Count-1;i++)
         {
           goldimg1.ImageUrl=ds.Table[0].Rows[i]["User_image"].ToString();
         }
    }

Some Example:

http://www.aspdotnet-suresh.com/2011/03/how-to-save-images-into-folder-and.html http://www.aspdotnet-suresh.com/2011/01/how-to-insert-images-into-database-and.html

相关阅读:
Top