Reputation: 55
i am using asp.net C# SQL to create a webpage.I need to list out a courseID to let user choose, but it list out two time same value in dropdownlist S1111 S2222 S3333 S1111 S2222 S3333 ,someone help
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection conn;
SqlDataReader dtr;
SqlCommand cmd;
string Connnection = ConfigurationManager.ConnectionStrings["ELearing"].ConnectionString;
conn = new SqlConnection(Connnection);
if (!Page.IsPostBack)
{
//Get Staff Information
conn.Open();
string cmdString = "SELECT DISTINCT CourseID FROM Schedule WHERE(StaffID = @scheduleStaffID)";
cmd = new SqlCommand(cmdString, conn);
cmd.Parameters.AddWithValue("@scheduleStaffID", Session["UserID"].ToString());
dtr = cmd.ExecuteReader();
while (dtr.Read())
{
ddlCourse.Items.Add(dtr["CourseID"].ToString());
}
dtr.Close();
conn.Close();
}
}
Upvotes: 2
Views: 182
Reputation: 3198
Try these
1.Do you get duplicates when you do externally SELECT DISTINCT CourseID FROM Schedule WHERE StaffId = 1
2.use breakpoints to check additional post backs.
3.Try ddlCourse.Items.Clear just before your while loop.
Upvotes: 1