protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                conOOC.Open();

                SqlDataAdapter adptDDL = new SqlDataAdapter("SELECT ProgrammeCode, StaffID FROM Programme, Staff", conOOC);
                DataTable dtDDL = new DataTable();

                adptDDL.Fill(dtDDL);

                ddlProgramme.DataSource = dtDDL;
                ddlProgramme.DataTextField = "ProgrammeCode";
                ddlProgramme.DataValueField = "ProgrammeCode";
                ddlProgramme.DataBind();

                ddlStaff.DataSource = dtDDL;
                ddlStaff.DataTextField = "StaffID";
                ddlStaff.DataValueField = "StaffID";
                ddlStaff.DataBind();

                conOOC.Close();

                if (ddlProgramme.Items.Count <= 0)
                {
                    Response.Write("<script language=javascript>alert('Please Add Some Programme');window.open('Course Maintenance.aspx','_self');</script>");
                }
                if (ddlStaff.Items.Count <= 0)
                {
                    Response.Write("<script language=javascript>alert('Please Add Some Staff');window.open('Course Maintenance.aspx','_self');</script>");
                }
                if (ddlProgramme.Items.Count <= 0 || ddlStaff.Items.Count <= 0)
                {
                    Response.Write("<script language=javascript>alert('Please Add Some Programme And Staff');window.open('Course Maintenance.aspx','_self');</script>");
                }
            }
        }

it always go to if(ddlProgramme.Items.Count <= 0) condition, but i already added some data into programme table. why? :/

Recommended Answers

All 2 Replies

Did you check if the DataTable has the data?

I think you have to create to datatables, on for each dropdown.

new SqlDataAdapter("SELECT ProgrammeCode FROM Programme")
new SqlDataAdapter("SELECT StaffID FROM Staff")

And I would invert the order of the IF's, if (ddlProgramme.Items.Count <= 0 && ddlStaff.Items.Count <= 0) should be first. It makes more sense. Note that I changed the operator to &&.
And also you should use else if.

This is explanation why you have output like that:
The query which you have written, is actually join, so if you dont have any record in Staff table, the query will return zero rows, and that is why you have always empty result table. So, you need definitely to divide that in two different queries, as AleMonteiro already proposed.

The next thing, in the way which you have written, this line would never execute:

if (ddlProgramme.Items.Count <= 0 || ddlStaff.Items.Count <= 0)

because, you already checked these two values saparately, and if the checking passed (so they are not <=0), this if statement will never be true.

So, the solution would be
1. either to delete the last IF statement, and let it just check one by one value, so it definitely says what needs to be added
OR
2. put ONLY one IF statement with && operator, and you will have one generic output message ('Please Add Some Programme Or Staff'). <--Notice this message. It will be shown either one or both tables are emtpy.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.