Empty Repeater with Header and Custom Message when no Data Present in DataSet

In this Blog you will learn the following concepts -
  • How to bind the data to Repeater using stored procedure with parameters.
  • How to get the data by using DeptId.
  • How to bind empty Repeater with header.
  • How to create custom message when no data present in DataSet in Asp.net.
At Database

Create a table like as follows to demonstrate the above concepts.
 


Copy the following script run in your database to create USP_GetEmployeesByDeptId stored procedure.
  1. Create procedure [dbo].[USP_GetEmployeesByDeptId]  
  2. @DeptId int  
  3. AS  
  4. Begin  
  5. Select * from Mas_Employee  
  6. where DeptId = @DeptId  
  7. END  
Steps

Create new ASP.NET Empty Web Application give it to meaning full name.

Add one webfrom to your project.

Web.config

Make the Connection string in web.Config.
  1. <connectionStrings>  
  2.    <add name="conStr" connectionString="Password = 1234; User ID=sa; Database = DB_DEV_JAI; Data Source = ."  
  3. providerName="System.Data.SqlClient" />  
  4. </connectionStrings>  
.aspx:

Copy the following code in your design page. 
  1. <div align="center" style="width:30%;">  
  2.     <asp:TextBox ID="txtDeptID" runat="server" placeholder="Enter DeptId"></asp:TextBox>  
  3.    
  4.     <asp:Button ID="btnGetData" runat="server" Text="GetData" OnClick="btnGetData_Click" />  
  5.     <br />  
  6.     <br />  
  7.     <asp:Repeater ID="rptEmpDetails" runat="server" OnItemDataBound="rptEmpDetails_ItemDataBound">  
  8.         <HeaderTemplate>  
  9.             <table border="1" style="width:100%;">  
  10.                 <tr style="background-color: Blue; color: #FFF;" align="center">  
  11.                     <th>  
  12. Name  
  13. </th>  
  14.                     <th>  
  15. Salary  
  16. </th>  
  17.                     <th>  
  18. DeptId  
  19. </th>  
  20.                 </tr>  
  21.             </HeaderTemplate>  
  22.             <ItemTemplate>  
  23.                 <tr style="background-color: white;" align="center">  
  24.                     <td><%#Eval("Name") %>  
  25.                     </td>  
  26.                     <td><%#Eval("Salary")%>  
  27.                     </td>  
  28.                     <td><%#Eval("DeptId")%>  
  29.                     </td>  
  30.                 </tr>  
  31.             </ItemTemplate>  
  32.             <FooterTemplate>  
  33.             </table>  
  34.             <div id="dvNoRecords" runat="server" visible="false" style="text-align: center; color: Red;">  
  35.                 <font>  
  36.                     <b>  
  37.                         <i>No records to display.</i>  
  38.                     </b>  
  39.                 </font>  
  40.             </div>  
  41.         </FooterTemplate>  
  42.     </asp:Repeater>  
  43. </div>  
CodeBehind

When you click the button by providing DeptId through textbox it binds the data to Repeater with respect to DeptId. If there are no employees with respect to DeptId then it shows empty Repeater with header and custom message.
  1. protected void btnGetData_Click(object sender, EventArgs e)  
  2. {  
  3.    int DeptId = Convert.ToInt32(txtDeptID.Text.Trim());  
  4.    BindRepeater(DeptId);  
  5. }  
BindRepeater
 
BindRepeater method is used to bind the data to Repeater with respect to DeptId. So we have to pass the DeptId to BindRepeater method. In this example we pass DeptId through TextBox.
  1. private void BindRepeater(int DeptID)   
  2. {  
  3.     try   
  4.     {  
  5.         SqlCommand cmd = new SqlCommand("USP_GetEmployeesByDeptId", con);  
  6.         cmd.CommandType = CommandType.StoredProcedure;  
  7.         cmd.Parameters.AddWithValue("@DeptId", DeptID);  
  8.         SqlDataAdapter adp = new SqlDataAdapter(cmd);  
  9.         DataSet ds = new DataSet();  
  10.         adp.Fill(ds);  
  11.         rptEmpDetails.DataSource = ds;  
  12.         rptEmpDetails.DataBind();  
  13.     }   
  14.     catch (Exception ex)   
  15.     {  
  16.         ////Do your exception handling here  
  17.     }  
  18. }  
If there are no records to bind the Reapeter, then the following Reapeter event will be executed.
  1. protected void rptEmpDetails_ItemDataBound(object sender, RepeaterItemEventArgs e) 
  2. {  
  3.     if (rptEmpDetails.Items.Count < 1)   
  4.     {  
  5.         if (e.Item.ItemType == ListItemType.Footer)   
  6.         {  
  7.             HtmlGenericControl dvNoRec = e.Item.FindControl("dvNoRecords"as HtmlGenericControl;  
  8.             if (dvNoRec != null)   
  9.             {  
  10.                 dvNoRec.Visible = true;  
  11.             }  
  12.         }  
  13.     }  
  14. }  
Output :

When no data is found 

Repeater with records 



I hope you enjoyed it. please provide your valuable feedback and suggestions if you found this article is helpful.
Ebook Download
View all
Learn
View all