vyclarks
vyclarks

Reputation: 878

Error with Dropdownlist Control

In shownotice.aspx page has a Repeater control to show the notice loaded from Database (sql server 2008) and a Dropdownlist control for filtering: all notices(default value = 0)/lecturer's notices(value=1)/ student notices (value=2).

<asp:DropDownList ID="DropDownList" runat="server" OnSelectedIndexChanged="DropDownlist_Changed"
            AutoPostBack="true" >
                <asp:ListItem Selected="True" Value="0"> All notice </asp:ListItem>
                <asp:ListItem Value="1"> Lecturer </asp:ListItem>
                  <asp:ListItem Value="2"> Student </asp:ListItem>
            </asp:DropDownList>

NOTICE table in database

 NOTICE(id, title, content,object)

object = 1 --> notice for lecturer

object= 2--> notice for student.

Please take a look at my code below:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        bool check = BindRepeater(madoituong, maloai);
        if (check == false)
            LabelError.Text = "There is no notice.";
    }
}

private bool BindRepeater()
{
    string sql = "";
    string key = DropDownList.SelectedValue;
    if (key == "0")
        sql = "select * from NOTICE";
    else
        sql = "select * from NOTICE where object=" + key;

    DataTable tb = EXECUTEQUERYSQL(sql);
    if (tb.Rows.Count > 0)
       {

        Repeater1.DataSource = tb;
        Repeater1.DataBind();
        return true;

         }
    else
        return false;
}

public void DropDownlist_Changed(Object sender, EventArgs e)
{
    bool check = BindRepeater();
    if (check == false)
        LabelError.Text = "There is no notice.";
}

All I want is:

When I built this page, the program worked fine if it has some notices for lecturer (object=1) and some notices for student (object=2) in Database.

But when it only has notices for lecturer or only notices for student--> program worked wrong.

For example:

There is only one record in NOTICE table:

id= 1, title= aaa, content= holiday, object=1

When I choose Lecturer or Student --> it always show notice aaa , it should show messsage "There is no notice" when I choose Student option.

Try to debug:

When I choose Student option: Datatable tb = null and return false, the program jump into if statement and hit line: LabelError = "There is no notice."; But after finish debugging, the Repeater still shows notice aaa instead of message.

Help!!! I really dont know why when I debug, the program hit the line LabelError = "There is no notice."; but then, there is no LabelError Message appear. Help!!!

Upvotes: 1

Views: 94

Answers (1)

Suraj Singh
Suraj Singh

Reputation: 4059

  DataTable tb = EXECUTEQUERYSQL(sql);
        Repeater1.DataSource = tb;
        Repeater1.DataBind();
        return true;

    if (tb.Rows.Count > 0)
       {
           return true;       
       }
    else
        return false;

See here you are binding your Repeater only if when RowCount >0 , So when your row count is 0 then your repeater is not getting updated and it's displaying previous values , You have to bind your Repeater again to update it's value but when there are no values in table and your RowCount is zero your if condition do not let Repeater to bind again.

Upvotes: 1

Related Questions