Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am binding the data to gridview on button click with input,if records exist GV will display data, if there is no records with input data then it will display some msg,
When IF condition is true data loads and diplays when IF condition is not true then msg will display but old GV data will still display on page, How to clear that data ? i want display only msg when IF condition is not true.

C#
protected void btnGV1_Click(object sender, EventArgs e)
{
    string personid = TBpersonid.Text;
    String connstring = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
    SqlConnection con = new SqlConnection(connstring);
    SqlCommand cmd = new SqlCommand("select * from[Transactions_log] where person_id=@personid ", con);
    cmd.Parameters.AddWithValue("@personid",personid);
    con.Open();
    SqlDataReader Er = cmd.ExecuteReader();
    if (Er.HasRows==true)
    {
        GV1.DataSource = Er;
        GV1.DataBind();
    }
    else
    {
        Response.Write("No records found for the user: "+personid+"");
    }
}


What I have tried:

if (!IsPostBack) works ? if yes how to implement it?
Posted
Updated 4-Sep-18 4:00am

1 solution

GridView will only display if there's data associated on it. Have you binded your GridView on Page_Load event? If you did, then make sure that you wrap your code within !IsPosBack block:

C#
protected void Page_Load(object sender, EventArgs e)
{
     if(!IsPostBack){
           //bind your gridview here
     }
}


If you want to force your GridView to clear the data and hide it, then you can set the datasource to nothing like this:

C#
GV1.DataSource = null;
Gv1.DataBind();
 
Share this answer
 
Comments
Virendra S from Bangalore, Karnataka 5-Sep-18 0:38am    
No i have added below code in page load event, issue resolved, it's working now. I am not using (!IsPostBack).

protected void Page_Load(object sender, EventArgs e)
{
GV1.DataSource = null;
GV1.DataBind();
}

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