hide RadImageGallery if Datasource is empty

4 posts, 0 answers
  1. moegal
    moegal avatar
    276 posts
    Member since:
    Jul 2007

    Posted 17 Mar 2014 Link to this post

    How do I hide the control if the SqlDataSource does not have any records?

    I tried to get the items.count but it does not seem to work in PreRender or DataBound.

  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 18 Mar 2014 in reply to moegal Link to this post

    Hi moegal,

    Please try the following C# code snippet which works fine at my end.

    protected void RadImageGallery1_DataBound(object sender, EventArgs e)
        SqlConnection connection= new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
        SqlCommand cmd = new SqlCommand("SELECT COUNT(*) FROM Images", cn);
        Int32 count = (Int32)cmd.ExecuteScalar();
        if (count == 0)
            RadImageGallery1.Visible = false;

  3. moegal
    moegal avatar
    276 posts
    Member since:
    Jul 2007

    Posted 18 Mar 2014 in reply to Princy Link to this post


    Thanks.  I was hoping I could get the count directly from the control so I did not have to make a second call to the database.

  4. Marin Bratanov
    Marin Bratanov avatar
    4492 posts

    Posted 20 Mar 2014 Link to this post

    Hi Marty,

    You can databind the control manually - perform a Select() to see what the result is: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.select(v=vs.110).aspx. If there is data - set the collection you have to the DataSource property and call the DataBind() method. Otherwise, set the Visible property to false.

    You can also use the Selecting SqlDataSource event to see this information, as discussed in numerous placed in the net, e.g.: http://stackoverflow.com/questions/2231273/how-do-i-check-that-a-sqldatasource-returned-data.


    Marin Bratanov

    Build cross-platform mobile apps using Visual Studio and .NET. Register for the online webinar on 03/27/2014, 11:00AM US ET.. Seats are limited.

Back to Top