Here we will discuss the following concepts :
  • How to bind the data to GridView using stored procedure with parameters or how to bind the data to GridView with respect to DeptId.
  • How to bind empty GridView with header and custom message when no data is present in DataSet in ASP.NET.
At Database:

Create the following table to demonstrate the above concepts.


Copy the following script and 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
Implementation:

Create new ASP.NET Empty Web Application and give it a meaningfull name. Add one webform to your project.

In 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>
Design Your.aspx:

Copy the following code in your design page.
  1. <div>
  2. <asp:TextBox ID="txtDeptID" runat="server" placeholder="Enter DeptId"></asp:TextBox>
  3. <asp:Button ID="btnGetData" runat="server" Text="GetData" OnClick="btnGetData_Click" />
  4. <br />
  5. <br />
  6. <asp:GridView ID="gvEmptyDta" runat="server" AutoGenerateColumns="False">
  7. <Columns>
  8. <asp:BoundField DataField="Name" HeaderText="Emp_Name" />
  9. <asp:BoundField DataField="Salary" HeaderText="Emp_Salary" />
  10. <asp:BoundField DataField="DeptId" HeaderText="Emp_DeptId" />
  11. </Columns>
  12. </asp:GridView>
  13. </div>
CodeBehind

When you click the button by providing DeptId through textbox, it binds the data to GridView with respect to DeptId. If there are no employees with respect to DeptId, then it shows empty Gridview with header and custom message.
BindGrid(int DeptID): BindGrid method is used to bind the data to GridView with respect to DeptId. So we have to pass the DeptId to BindGrid method. In this example we will pass DeptId through TextBox.
Copy the following code in your .cs or codebehind file:
  1. protected void btnGetData_Click(object sender, EventArgs e)
  2. {
  3. int DeptId = Convert.ToInt32(txtDeptID.Text.Trim());
  4. BindGrid(DeptId);
  5. }
  6. private void BindGrid(int DeptID)
  7. {
  8. SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString);
  9. SqlCommand cmd = new SqlCommand("USP_GetEmployeesByDeptId", con);
  10. cmd.CommandType = CommandType.StoredProcedure;
  11. cmd.Parameters.AddWithValue("@DeptId", DeptID);
  12. SqlDataAdapter adp = new SqlDataAdapter(cmd);
  13. DataSet ds = new DataSet();
  14. adp.Fill(ds);
  15. if (ds.Tables[0].Rows.Count > 0)
  16. {
  17. gvEmptyDta.DataSource = ds;
  18. gvEmptyDta.DataBind();
  19. }
  20. else
  21. {
  22. BingEmpyGridViewWithHeader(gvEmptyDta, ds, "No Data Found");
  23. }
  24. }
  25. //If there are no records to bind the gridview then the following method will be executed.
  26. protected void BingEmpyGridViewWithHeader(GridView grd, DataSet ds, String msg)
  27. {
  28. try
  29. {
  30. if (ds.Tables[0].Rows.Count == 0)
  31. {
  32. //Add a blank row to the dataset
  33. ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
  34. //Bind the DataSet to the GridView
  35. grd.DataSource = ds;
  36. grd.DataBind();
  37. //Get the number of columns to know what the Column Span should be
  38. int columnCount = grd.Rows[0].Cells.Count;
  39. //Call the clear method to clear out any controls that you use in the columns. E.g If you are using dropdown list etc. in any of the column then it is necessary.
  40. grd.Rows[0].Cells.Clear();
  41. grd.Rows[0].Cells.Add(new TableCell());
  42. grd.Rows[0].Cells[0].ColumnSpan = columnCount;
  43. grd.Rows[0].Cells[0].Text = "<font color=Red><b><i><center>" + msg + "</center></i></b></font>";
  44. }
  45. }
  46. catch (Exception ex)
  47. {
  48. //Do your exception handling here
  49. }
  50. }
Output

When no data is found:


When data is found:


I hope you enjoyed it. Please provide your valuable suggestions and feedback if you found this article helpful.