This is a migrated thread and some comments may be shown as answers.

hide RadImageGallery if Datasource is empty

3 Answers 36 Views
ImageGallery
This is a migrated thread and some comments may be shown as answers.
moegal
Top achievements
Rank 1
moegal asked on 17 Mar 2014, 11:43 PM
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.

Marty

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 1
answered on 18 Mar 2014, 05:00 AM
Hi moegal,

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

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

Thanks,
Princy.
0
moegal
Top achievements
Rank 1
answered on 18 Mar 2014, 11:06 AM
Princy,

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.

Marty
0
Marin Bratanov
Telerik team
answered on 20 Mar 2014, 06:27 PM

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.


Regards,

Marin Bratanov
Telerik
 

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.

 
Tags
ImageGallery
Asked by
moegal
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
moegal
Top achievements
Rank 1
Marin Bratanov
Telerik team
Share this question
or