abidinberkay abidinberkay - 14 days ago 5
ASP.NET (C#) Question

Label text is not updated in first try in asp.net. Why?

On the page, I have only textbox,button,gridview and a label.

I write username to textbox and click button, so it searches in db and fills gridview and update label text as "xx result found" so xx is the count of results found in db.

the sample code is very short and easy:

string userName = TextBox2.Text;
SqlDataSource1.SelectCommand = "Select * From SampleTable Where name = '"+userName+"'";
ResultLable.Text = GridView1.Rows.Count + " Result Found";
GridView1.Visible = true;
ResultLable.Visible = true;


for example; there is a username John in db but Mary does not exist. When I search John, the count is 1 (which is correct), then I search for Mary and click button again still the number is 1(which is incorrect) but when I click button again it becomes 0. Same case valid for reverse scenario as well.

So the problem is click button is not updating the number in first click-just working for 2nd click.

Why does it happen ?

Answer

after set SelectCommand you need to call

SqlDataSource1.DataBind();
GridView1.DataBind();
Comments