Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Display record not found, if records not exists in gridview, when item selected from dropdownlist
==================================================================================================

Hi frnds,

am working on asp.net c#, sqlserver2005,

on my webpage i have a dropdownlist and a gridview. and 4 textboxes to read the data from database am displaying data in gridview & textboxes according to dropdownlist selected item,

according to dropdownlist if records not exists in gridview. Display a message records not found.

This is my code.

C#
protected void DDLStudents_SelectedIndexChanged(object sender, EventArgs e)
    {

        SqlConnection conDisp = new SqlConnection(_connString);
        SqlCommand cmdDisp = new SqlCommand();
        cmdDisp.Connection = conDisp;
      
        cmdDisp.CommandText = "Select * from Student_Records where Student_code ='" + DDLStudents.SelectedValue +"'";
        using (conDisp)
        {
            conDisp.Open();
            SqlDataReader readerDisp = cmdDisp.ExecuteReader();
            if (readerDisp.Read())
            {
                TxtPrincipal.Text = readerDisp["Principal"].ToString();
                TxtTeacher.Text = readerDisp["Teacher"].ToString();
                TxtEmail.Text = readerDisp["Email"].ToString();
                TxtTelephone.Text = readerDisp["TelephoneNo"].ToString();

                GridBind();


            }
            

            else
            {


                TxtPrincipal.Text = "";
                TxtTeacher.Text = "";
                TxtEmail.Text = "";
                TxtTelephone.Text = "";
            }
        }
       
        
        }
  
  
        public void GridBind()
    {
        
        SqlConnection con = new SqlConnection(_connString);
        SqlCommand cmd = new SqlCommand("select StudentName,RollNo, FathersName, Standard from Student_Records where Student_code = '" + DDLStudents.SelectedValue + "'", con);
        SqlDataAdapter Adpt = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        Adpt.Fill(dt);
        lblCount.Text = dt.Rows.Count.ToString();
        lblCount.Visible = true;
        GVView.DataSource = dt;
        GVView.DataBind();

    }



display a message when records not found in gridview according to dropdownlist.

Note: Gridview will not be displayed when Pageloads. Based on dropdownlist selected item gridview will be displayed.

Please help thanks in advance.
Posted
Updated 19-May-13 13:46pm
v2
Comments
Member 9581488 19-May-13 15:54pm    
On what line you are getting error?
[no name] 19-May-13 16:04pm    
So check that your reader has rows and if it does not then show your error message.
Ubaid ur Rahman IT 19-May-13 16:04pm    
No Error, But am unable to display a message, if record not exists in gridview while item selected from dropdown.

I need a message if records not found when item selected selected from dropdownlist.

Please help, thanks.
Member 9581488 19-May-13 19:11pm    
Adpt.Fill(dt); after this line
dt.rows.count == 0
{
// your message here!
}

1 solution

Try this,

use EmptyDataText property of GridView.

XML
<asp:GridView EmptyDataText="No Record Found." ID="grddata" runat="server">
    </asp:GridView>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900