I had Repeater control which had image control sometimes this image control hadnot data and I want to hide it when it hadnot data I did the code but it displayed as image without data (Not avaliable ) please any one help me.
Note : That data come from data base so Img Had Id from database but without value (NULL)
protected void DLHome_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
Image Img = e.Item.FindControl("ModelLogo") as Image;
using (SqlConnection con = Connection.GetConnection())
{
string Sql = "Select Logo From Model";
SqlCommand com = new SqlCommand(Sql, con);
com.CommandType = CommandType.Text;
SqlDataReader dr = com.ExecuteReader();
if (dr.Read())
{
string Img2 = dr["Logo"].ToString();
if (Img2 == System.DBNull.Value.ToString())
{
Img.Visible = false;
}
}
}
}
A better way to do it would be to retrieve the Logo data in the original query that is used to populate your Repeater. Then you will be able to set the Visible property on your image based on that data, rather than having to perform a new datbase Query when each row is bound. This will boost performance of your page a lot.
Concerning the code that you have posted, you are not passing an image id to the SQLCommand