1 Replies - 1274 Views - Last Post: 08 September 2009 - 07:20 AM Rate Topic: -----

#1 husna_  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 07-September 09

Drop down list

Post icon  Posted 07 September 2009 - 08:34 PM

I am trying to show a view display of a teacher's schedule.
But, I am unable to show a new row of data which i had just added in the database.
In this case, the new row is "Role"

The first set of code shows the filter that user can search from.

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class TAView : System.Web.UI.Page
{
	protected void Page_Load(object sender, EventArgs e)
	{
		if (Page.IsPostBack == false)
		{
			sessionValidate();

			binddropdownlists(); 
			}
		}
	}
	public void sessionValidate()
	{
		if (Session["UserID"] == null)
		{
			Session.Add("LoggedOut", "Session Expired. Please re-login.");
			Response.Redirect("Login.aspx");
		}
		else
		{
			if (Convert.ToInt32(Session["Role1"].ToString()) == 0)
			{
				Response.Redirect("Welcome1.aspx");
			}
			else
			{
			}
		}
	}
	protected void btnSubmit_Click(object sender, EventArgs e)
	{
		string year = ddlYear.SelectedItem.Value;
		string semester = ddlSem.SelectedItem.Value;
		string course = ddlCourse.SelectedItem.Value;
		string stage = ddlStage.SelectedItem.Value;
		string specialisation = ddlSpecialisation.SelectedItem.Value;
		string staff = ddlStaff.SelectedItem.Value;
		string role = ddlRole.SelectedItem.Value;
		bool mlonly = cbxMLOnly.Checked;
		string module = "";
		if (ddlModule.SelectedItem.Text == "All")
		{
			module = null;
		}
		else
		{
			module = ddlModule.SelectedItem.Value;
		}
		Server.Transfer("TAViewDisplay.aspx?callfrom=TAView.aspx&year=" + year + "&semester=" + semester + "&course=" + course + "&stage=" + stage + "&specialisation=" + specialisation + "&module=" + module + "&mlonly=" + mlonly + "&staff=" + staff + "&role=" + role + "&callback=TAView.aspx");
	}
	public void binddropdownlists()
	{
		string strCon = Database.GetConStr();
		SqlConnection sqlCon = new SqlConnection(strCon);
		try
		{ //bind the Staff dropdownlist
			string strSql = "SELECT Name, StaffID FROM StaffAccount WHERE Status = 'A' ORDER BY Name";
			SqlCommand sqlCmd = new SqlCommand(strSql, sqlCon);
			sqlCon.Open();
			SqlDataReader reader = sqlCmd.ExecuteReader();
			while (reader.Read())
			{
				ddlStaff.Items.Add(new ListItem(reader["Name"].ToString(), reader["StaffID"].ToString()));
			}
			reader.Close();
		}
		catch (Exception ex)
		{
			lblError.Text = ex.Message;
		}
		finally
		{
			sqlCon.Close();
		}

		//TRYINGGGG
		string strCon7 = Database.GetConStr();
		SqlConnection sqlCon7 = new SqlConnection(strCon7);
		try
		{ //bind the Role dropdownlist
			string strSql7 = "SELECT Role, ModuleLeader FROM ModuleStr ORDER BY Role";
			SqlCommand sqlCmd7 = new SqlCommand(strSql7, sqlCon7);
			sqlCon7.Open();
			SqlDataReader reader7 = sqlCmd7.ExecuteReader();
			while (reader7.Read())
			{
				ddlRole.Items.Add(new ListItem(reader7["Role"].ToString(), reader7["ModuleLeader"].ToString()));
			}
			reader7.Close();
		}
		catch (Exception ex)
		{
			lblError.Text = ex.Message;
		}
		finally
		{
			sqlCon7.Close();
		}
	   

		string strCon2 = Database.GetConStr();
		SqlConnection sqlCon2 = new SqlConnection(strCon2);
		try
		{ //bind the Module Type dropdownlist
			string strSql2 = "SELECT Desc1, Paracode2 FROM Parameter WHERE Paracode1 = 'MODULETYPE' AND Status = 'A' ORDER BY Desc1";
			SqlCommand sqlCmd2 = new SqlCommand(strSql2, sqlCon2);
			sqlCon2.Open();
			SqlDataReader reader2 = sqlCmd2.ExecuteReader();
			while (reader2.Read())
			{
				ddlModule.Items.Add(new ListItem(reader2["Desc1"].ToString(), reader2["Paracode2"].ToString()));
			}
			reader2.Close();
		}
		catch (Exception ex)
		{
			lblError.Text = ex.Message;
		}
		finally
		{
			sqlCon2.Close();
		}
		string strCon3 = Database.GetConStr();
		SqlConnection sqlCon3 = new SqlConnection(strCon3);
		try
		{ //bind the Specialisation Group dropdownlist
			string strSql3 = "SELECT Desc1, Paracode2 FROM Parameter WHERE Paracode1 = 'SPECGRP' AND Paracode3 = @course OR Paracode3 = 'NULL' AND Status = 'A' ORDER BY Desc1";
			SqlCommand sqlCmd3 = new SqlCommand(strSql3, sqlCon3);
			sqlCmd3.Parameters.AddWithValue("@course", ddlCourse.SelectedValue);
			sqlCon3.Open();
			SqlDataReader reader3 = sqlCmd3.ExecuteReader();
			while (reader3.Read())
			{
				ddlSpecialisation.Items.Add(new ListItem(reader3["Desc1"].ToString(), reader3["Paracode2"].ToString()));
			}
			reader3.Close();
		}
		catch (Exception ex)
		{
			lblError.Text = ex.Message;
		}
		finally
		{
			sqlCon3.Close();
		}
		string strCon4 = Database.GetConStr();
		SqlConnection sqlCon4 = new SqlConnection(strCon4);
		try
		{ //bind the Academic Year dropdownlist
			string strSql4 = "SELECT Desc1 FROM Parameter WHERE Paracode1 = 'ACADYEAR' AND Paracode2 = 'RANGE' AND Status = 'A'";
			SqlCommand sqlCmd4 = new SqlCommand(strSql4, sqlCon4);
			sqlCon4.Open();
			SqlDataReader reader4 = sqlCmd4.ExecuteReader();
			while (reader4.Read())
			{
				ddlYear.Items.Add(new ListItem(reader4["Desc1"].ToString(), reader4["Desc1"].ToString()));
			}
			reader4.Close();
		}
		catch (Exception ex)
		{
			lblError.Text = ex.Message;
		}
		finally
		{
			sqlCon4.Close();
		}
		string strCon5 = Database.GetConStr();
		SqlConnection sqlCon5 = new SqlConnection(strCon5);
		try
		{ //bind the Course dropdownlist
			string strSql5 = "SELECT Desc1, Paracode2 FROM Parameter WHERE Paracode1 = 'COURSE' AND Status = 'A' ORDER BY Desc1";
			SqlCommand sqlCmd5 = new SqlCommand(strSql5, sqlCon5);
			sqlCon5.Open();
			SqlDataReader reader5 = sqlCmd5.ExecuteReader();
			while (reader5.Read())
			{
				ddlCourse.Items.Add(new ListItem(reader5["Desc1"].ToString(), reader5["Paracode2"].ToString()));
			}
			reader5.Close();
		}
		catch (Exception ex)
		{
			lblError.Text = ex.Message;
		}
		finally
		{
			sqlCon5.Close();
		}
		string strCon6 = Database.GetConStr();
		SqlConnection sqlCon6 = new SqlConnection(strCon6);
		try
		{ //bind the Stage dropdownlist
			string strSql6 = "SELECT Desc1 FROM Parameter WHERE Paracode1 = 'ACAD' AND Paracode2 = 'STAGE' AND Status = 'A'";
			SqlCommand sqlCmd6 = new SqlCommand(strSql6, sqlCon6);
			sqlCon6.Open();
			SqlDataReader reader6 = sqlCmd6.ExecuteReader();
			while (reader6.Read())
			{
				ddlStage.Items.Add(new ListItem(reader6["Desc1"].ToString(), reader6["Desc1"].ToString()));
			}
			reader6.Close();
		}
		catch (Exception ex)
		{
			lblError.Text = ex.Message;
		}
		finally
		{
			sqlCon6.Close();
		}
	}
	protected void ddlCourse_SelectedIndexChanged(object sender, EventArgs e)
	{
		ddlSpecialisation.Items.Clear(); //upon selected course, refresh specialisation
		ddlSpecialisation.Items.Add(new ListItem("All", "%"));
		string strCon3 = Database.GetConStr();
		SqlConnection sqlCon3 = new SqlConnection(strCon3);
		try
		{ //bind the Specialisation Group dropdownlist
			string strSql3 = "SELECT Desc1, Paracode2 FROM Parameter WHERE Paracode1 = 'SPECGRP' AND Paracode3 = @course OR Paracode3 = 'NULL' AND Status = 'A' ORDER BY Desc1";
			SqlCommand sqlCmd3 = new SqlCommand(strSql3, sqlCon3);
			sqlCmd3.Parameters.AddWithValue("@course", ddlCourse.SelectedValue);
			sqlCon3.Open();
			SqlDataReader reader3 = sqlCmd3.ExecuteReader();
			while (reader3.Read())
			{
				ddlSpecialisation.Items.Add(new ListItem(reader3["Desc1"].ToString(), reader3["Paracode2"].ToString()));
			}
			reader3.Close();
		}
		catch (Exception ex)
		{
			lblError.Text = ex.Message;
		}
		finally
		{
			sqlCon3.Close();
		}
	}
	//protected void gridAMDisplay_RowDataBound(object sender, GridViewRowEventArgs e)
	//{
	//System.Data.DataRowView drv;
	//drv = (System.Data.DataRowView)e.Row.DataItem;
	//if (e.Row.RowType == DataControlRowType.DataRow)
	//{
	//	if (drv != null)
	//	{
	//		gridAMDisplay.Columns[8].ItemStyle.Width = 10;
	//		gridAMDisplay.Columns[8].ItemStyle.Wrap = false;
	//	}
	//}
	//}
}


This set of code is the displaying page of the filtering.
[code]using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class TAViewDisplay : System.Web.UI.Page
{
public string callfrom;
public string module;
public string specialisation;
public string year;
public string semester;
public string course;
public string stage;
public string mlonly;
public string staff;
public string role;
protected void Page_Load(object sender, EventArgs e)
{
callfrom = Request.QueryString["callfrom"];
module = Request.QueryString["module"];
specialisation = Request.QueryString["specialisation"];
year = Request.QueryString["year"];
semester = Request.QueryString["semester"];
course = Request.QueryString["course"];
stage = Request.QueryString["stage"];
mlonly = Request.QueryString["mlonly"];
staff = Request.QueryString["staff"];
role = Request.QueryString["role"];
if (!Page.IsPostBack)
{
sessionValidate();
bindgridDisplay();
if (gridCEDisplay.Rows.Count == 0)
{
lblCE.Visible = false;
}
if (gridPEDisplay.Rows.Count == 0)
{
lblPE.Visible = false;
}
if ((gridPEDisplay.Rows.Count == 0) && (gridAMDisplay.Rows.Count == 0) && (gridCEDisplay.Rows.Count == 0))
{
lblError.Text = "No results found.";
}
}
}
public void sessionValidate()
{
if (Session["UserID"] == null)
{
Session.Add("LoggedOut", "Session Expired. Please re-login.");
Response.Redirect("Login.aspx");
}
else
{
if (Convert.ToInt32(Session["Role1"].ToString()) == 0)
{
Response.Redirect("Welcome1.aspx");
}
else
{

}
}
}
public void bindgridDisplay() //bind gridview with ModuleType = Academic Modules
{
lblCE.Visible = true;
lblPE.Visible = true;
string moduletype = "";
for (int t = 0; t < 3; t++)
{
if ((t == 0) && ((module == "AM") || (module == "")))
{
moduletype = "AM";
}
else if ((t == 1) && ((module == "PE") || (module == "")))
{
moduletype = "PE";
}
else if ((t == 2) && ((module == "CE") || (module == "")))
{
moduletype = "CE";
}
string strCon = Database.GetConStr();
SqlConnection sqlCon = new SqlConnection(strCon);
DataSet dsML = new DataSet(); //create a dataset to store the ML details
DataSet dsMLBackup = new DataSet(); //create a backup dataset to store the ML details for referencing to default data
try
{ //retrieve first row of data to display(the module leader)
string strSql = "SELECT ModuleStrGrp.ModuleCode, Module.Mod_Abbreviation, Module.NameOfModule, ModuleStr.NofGrps, ModuleStr.TotalHour, ModuleStr.WeeklyLectHr, ModuleStr.WeeklyPractHr, ModuleStr.WeeklyTutHr, ModuleStr.ModuleLeader, ModuleStr.Role, ModuleStr.AcadSemester, ModuleStr.AcadYear, ModuleStr.Course, ModuleStr.Stage, Parameter.Desc3 As Grps " +
"FROM ModuleStrGrp " +
"INNER JOIN Module ON ModuleStrGrp.ModuleCode = Module.MCode " +
"INNER JOIN ModuleStr ON ModuleStrGrp.ModuleCode = ModuleStr.ModuleCode " +
"INNER JOIN Parameter ON ModuleStr.SpecGrp = Parameter.Paracode2 " +
"WHERE ModuleStr.AcadYear LIKE @Year AND ModuleStr.AcadSemester LIKE @Semester AND ModuleStr.Course LIKE @Course AND ModuleStr.Stage LIKE @Stage AND ModuleStr.ModuleLeader LIKE @Staff AND ModuleStr.SpecGrp LIKE @Specialisation AND Module.ModuleType = @ModuleType AND ModuleStr.Role = @Role" +
"GROUP BY ModuleStrGrp.ModuleCode, Module.Mod_Abbreviation, Module.NameOfModule, ModuleStr.NofGrps, ModuleStr.TotalHour, ModuleStr.WeeklyLectHr, ModuleStr.WeeklyPractHr, ModuleStr.WeeklyTutHr, ModuleStr.ModuleLeader, ModuleStr.Role, Parameter.SortSeq, ModuleStr.AcadSemester, ModuleStr.AcadYear, ModuleStr.Course, ModuleStr.Stage, Parameter.Desc3 " +
"ORDER BY ModuleStrGrp.ModuleCode, Parameter.SortSeq";
SqlDataAdapter da = new SqlDataAdapter(strSql, sqlCon);
da.SelectCommand.Parameters.AddWithValue("@Year", year);
da.SelectCommand.Parameters.AddWithValue("@Semester", semester);
da.SelectCommand.Parameters.AddWithValue("@Course", course);
da.SelectCommand.Parameters.AddWithValue("@Stage", stage);
da.SelectCommand.Parameters.AddWithValue("@Staff", staff);
da.SelectCommand.Parameters.AddWithValue("@Specialisation", specialisation);
da.SelectCommand.Parameters.AddWithValue("@ModuleType", moduletype);
da.SelectCommand.Parameters.AddWithValue("@Role", role);
da.Fill(dsML, "strSql"); //fill the dataset with the sql query
da.Fill(dsMLBackup, "strSql"); //backup the sql query with another dataset
//
string strCon1 = Database.GetConStr();
SqlConnection sqlCon1 = new SqlConnection(strCon1);
DataSet dsModuleStrGrp = new DataSet();
//retrieve data from ModuleStrGrp to display the number of groups each staff is taking
string strSql4 = "SELECT ModuleStrGrp.StaffID, ModuleStrGrp.ModuleCode, ModuleGrpFrom, ModuleGrpTo, StaffAccount.Name, ModuleStrGrp.AcadSemester, ModuleStrGrp.AcadYear, ModuleStrGrp.Course, ModuleStrGrp.Stage " +
"FROM ModuleStrGrp " +
"INNER JOIN Module ON ModuleStrGrp.ModuleCode = Module.MCode " +
"INNER JOIN ModuleStr ON ModuleStrGrp.ModuleCode = ModuleStr.ModuleCode " +
"INNER JOIN Parameter ON ModuleStr.SpecGrp = Parameter.Paracode2 " +
"INNER JOIN StaffAccount ON ModuleStrGrp.StaffID = StaffAccount.StaffID " +
"WHERE ModuleStrGrp.AcadYear LIKE @Year AND ModuleStrGrp.AcadSemester LIKE @Semester AND ModuleStrGrp.Course LIKE @Course AND ModuleStrGrp.Stage LIKE @Stage AND ModuleStrGrp.StaffID LIKE @Staff AND ModuleStr.SpecGrp LIKE @Specialisation AND Module.ModuleType = @ModuleType AND ModuleStrGrp.GrpType = 'P' OR ModuleStrGrp.GrpType = 'T' " +
"GROUP BY ModuleStrGrp.StaffID, ModuleStrGrp.ModuleCode, ModuleGrpFrom, ModuleGrpTo, StaffAccount.Name, Parameter.SortSeq, ModuleStrGrp.AcadSemester, ModuleStrGrp.AcadYear, ModuleStrGrp.Course, ModuleStrGrp.Stage " +
"ORDER BY ModuleStrGrp.ModuleCode, Parameter.SortSeq";
SqlDataAdapter da1 = new SqlDataAdapter(strSql4, sqlCon1);
da1.SelectCommand.Parameters.AddWithValue("@Year", year);
da1.SelectCommand.Parameters.AddWithValue("@Semester", semester);
da1.SelectCommand.Parameters.AddWithValue("@Course", course);
da1.SelectCommand.Parameters.AddWithValue("@Stage", stage);
da1.SelectCommand.Parameters.AddWithValue("@Staff", staff);
da1.SelectCommand.Parameters.AddWithValue("@Specialisation", specialisation);
da1.SelectCommand.Parameters.AddWithValue("@ModuleType", moduletype);
da1.SelectCommand.Parameters.AddWithValue("@Role", role);
da1.Fill(dsModuleStrGrp, "strSql4"); //fill the dataset with the sql query
int result, total = 0, MLtotal = 0; //for calculating of number of groups
for (int i = 0; i < dsML.Tables[0].Rows.Count; i++) //loop every record of the ModuleLeader details dataset
{
for (int r = 0; r < dsModuleStrGrp.Tables[0].Rows.Count; r++) //loop every record of the ModuleStructureGroup dataset
{
if ((dsML.Tables[0].Rows[i]["AcadYear"].ToString() == dsModuleStrGrp.Tables[0].Rows[r]["AcadYear"].ToString()) && (dsML.Tables[0].Rows[i]["AcadSemester"].ToString() == dsModuleStrGrp.Tables[0].Rows[r]["AcadSemester"].ToString()))
{
if ((dsML.Tables[0].Rows[i]["Course"].ToString() == dsModuleStrGrp.Tables[0].Rows[r]["Course"].ToString()) && (dsML.Tables[0].Rows[i]["Stage"].ToString() == dsModuleStrGrp.Tables[0].Rows[r]["Stage"].ToString()))
{
if (dsML.Tables[0].Rows[i]["ModuleCode"].ToString() == dsModuleStrGrp.Tables[0].Rows[r]["ModuleCode"].ToString()) //when the ModuleCode of both dataset matches
{
for (int c = 0; c < dsModuleStrGrp.Tables[0].Rows.Count; c++) //loop every record of the StaffAccount dataset
{
if (dsML.Tables[0].Rows[i]["ModuleLeader"].ToString() == dsModuleStrGrp.Tables[0].Rows[c]["StaffID"].ToString()) //when the StaffID matches
{
dsML.Tables[0].Rows[i]["ModuleLeader"] = dsModuleStrGrp.Tables[0].Rows[c]["Name"].ToString(); //rename ModuleLeader into a Name instead of ID
break; //end loop when renamed
}
}
if (dsMLBackup.Tables[0].Rows[i]["ModuleLeader"].ToString() == dsModuleStrGrp.Tables[0].Rows[r]["StaffID"].ToString()) //when ModuleLeader is in teaching list
{
result = Convert.ToInt32(dsModuleStrGrp.Tables[0].Rows[r]["ModuleGrpTo"]) - Convert.ToInt32(dsModuleStrGrp.Tables[0].Rows[r]["ModuleGrpFrom"]) + 1; //calculate the number of groups per record
MLtotal += result; //add up (the number of times this record is looped X number of groups calculated)
}
}
}
}
}
dsML.Tables[0].Rows[i]["Grps"] = MLtotal.ToString(); //replace the uncalculated group data with the calculated one
MLtotal = 0; //reset the number back to 0 for the next Module Leader
if (mlonly == "False") //when display ML only is unchecked
{
string checkname = "0"; //a variable to check whether the Staff is already added to the gridview to prevent duplicate
for (int r = 0; r < dsModuleStrGrp.Tables[0].Rows.Count; r++) //loop every record of the ModuleStructureGroup dataset
{
if ((dsML.Tables[0].Rows[i]["AcadYear"].ToString() == dsModuleStrGrp.Tables[0].Rows[r]["AcadYear"].ToString()) && (dsML.Tables[0].Rows[i]["AcadSemester"].ToString() == dsModuleStrGrp.Tables[0].Rows[r]["AcadSemester"].ToString()))
{
if ((dsML.Tables[0].Rows[i]["Course"].ToString() == dsModuleStrGrp.Tables[0].Rows[r]["Course"].ToString()) && (dsML.Tables[0].Rows[i]["Stage"].ToString() == dsModuleStrGrp.Tables[0].Rows[r]["Stage"].ToString()))
{
if (dsML.Tables[0].Rows[i]["ModuleCode"].ToString() == dsModuleStrGrp.Tables[0].Rows[r]["ModuleCode"].ToString()) //when the ModuleCode of both dataset matches
{
if (dsMLBackup.Tables[0].Rows[i]["ModuleLeader"].ToString() != dsModuleStrGrp.Tables[0].Rows[r]["StaffID"].ToString()) //when Staff is not a Module Leader
{
if (dsModuleStrGrp.Tables[0].Rows[r]["StaffID"].ToString() != checkname) //when staffID is not existing
{
if (checkname != "0") //check whether this is the first time
{
dsML.Tables[0].Rows[i]["Grps"] += "<br />" + total.ToString(); //if not, then add the calculated data into gridview
total = 0; //reset the number back to 0 for the next staff
}
for (int c = 0; c < dsModuleStrGrp.Tables[0].Rows.Count; c++) //loop every record of the StaffAccount dataset
{
if (dsModuleStrGrp.Tables[0].Rows[r]["StaffID"].ToString() == dsModuleStrGrp.Tables[0].Rows[c]["StaffID"].ToString()) //when the StaffID matches
{
checkname = dsModuleStrGrp.Tables[0].Rows[r]["StaffID"].ToString(); //set variable checkname to this staffID to check existing for above
dsML.Tables[0].Rows[i]["ModuleLeader"] += "<br />" + dsModuleStrGrp.Tables[0].Rows[c]["Name"].ToString(); //add the Name of the staff into the gridview
break; //end loop when added
}
}
}
result = Convert.ToInt32(dsModuleStrGrp.Tables[0].Rows[r]["ModuleGrpTo"]) - Convert.ToInt32(dsModuleStrGrp.Tables[0].Rows[r]["ModuleGrpFrom"]) + 1; //calculate the number of groups per record
total += result; //add up (the number of times this record is looped X number of groups calculated)
}
}
}
}
}
if (checkname != "0") //check whether this is the first time
{
dsML.Tables[0].Rows[i]["Grps"] += "<br />" + total.ToString(); //if not, then add the calculated data into gridview
total = 0; //reset the number back to 0 for the next staff
}

Is This A Good Question/Topic? 0
  • +

Replies To: Drop down list

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Drop down list

Posted 08 September 2009 - 07:20 AM

After you have added the row to your database, then rebind the drop down list to get the current data.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1