Click here to Skip to main content
15,885,782 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a search textbox to search songs and a search button in my form on button click it returns searched results and binds to repeater control.

But the problem is that if first time i search for any song then it is showing and binding to repeater control but when i click on search button when textbox is blank then it binding the last searched result. and Label control showing No result found(i'm using label control to show result count and if repeater1.items.count is 0 then it is showing No result found)
C#
SqlConnection sqlconn = new SqlConnection("server=.\\sqlexpress;database=Musicmania;integrated security=true;");
SqlCommand sqlcomm = new SqlCommand("select  * from uploadmp3 where song_name='" + txtSearch.Text + "'", sqlconn);
        sqlconn.Open();
        SqlDataReader dr = sqlcomm.ExecuteReader();
       
        if (dr.HasRows)
        {
            Repeater2.DataSource = dr;
            Repeater2.DataBind();
            Label1.Text = Repeater2.Items.Count + " Result(s) Found";

           Repeater2.Visible = true;
            
            linkNext.Visible = false;
            linkPrev.Visible = false;
            Label1.Visible = true;
            dr.Close();
            sqlconn.Close();
        }
        else
        {
            linkNext.Visible = false;
            linkPrev.Visible = false;
            Label1.Text = "No Result Found";
            

        }
Posted

You are binding Repeater when data is present. But when data is not present you are not binding Repeater , So what you looking is previous data because your repeater not bind again.
Either you should hide repeater or bind Repeater in both cases.
if (dr.HasRows)
 {
Repeater2.Visible = true;
}
else
{
Repeater2.Visible = false;
}


You should validate your textbox to avoid error.
C#
if(txtSearch.Text.Trim() != "")
{
Label1.Text = "Please insert song name.";
}


Use stored procedure rather than inline query.
 
Share this answer
 
JavaScript
<script type="text/javascript">
function mySubmit()
{
if(document.getElementById('txtSearch').value == '')
{
return false;
}
}</script>


C#
On page Load
{
ImageButton1.Attributes.Add("onclick", "javascript:return mySubmit()");

}
 
Share this answer
 
Comments
n.podbielski 28-Sep-12 5:46am    
Aren't you suppose to bind that data in button click event?
Surendra0x2 28-Sep-12 6:12am    
i want to bind data on button click and its working fine but when it shows searched result and if i make textbox blank and then again click on searchbutton then it is showing last search result.

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