private void DrawFieldsAndSetQueries()
{
switch (Request.QueryString[0])
{
case "E"://"Company":
{
sqlDataSource.SelectCommand = "SELECT * FROM Company";
sqlDataSource.UpdateCommand = "updateCompany";
sqlDataSource.DeleteCommand = "deleteCompany";
sqlDataSource.SelectCommandType = SqlDataSourceCommandType.Text;
sqlDataSource.UpdateCommandType = SqlDataSourceCommandType.StoredProcedure;
sqlDataSource.DeleteCommandType = SqlDataSourceCommandType.StoredProcedure;
TemplateField tf1 = new TemplateField();
tf1.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, "Email", "Email", new Label());
tf1.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "Email", "Email", new Label());
//tf1.EditItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "Email", "Email", new TextBox()); //PRIMARY KEY
TemplateField tf2 = new TemplateField();
tf2.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, "Name", "CompanyName", new Label());
tf2.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "Name", "CompanyName", new Label());
tf2.EditItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "Name", "CompanyName", new TextBox());
TemplateField tf3 = new TemplateField();
tf3.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, "Telephone", "Telephone", new Label());
tf3.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "Telephone", "Telephone", new Label());
tf3.EditItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "Telephone", "Telephone", new TextBox());
TemplateField tf4 = new TemplateField();
tf4.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, "Fax", "Fax", new Label());
tf4.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "Fax", "Fax", new Label());
tf4.EditItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "Fax", "Fax", new TextBox());
gridData.Columns.Add(tf1);
gridData.Columns.Add(tf2);
gridData.Columns.Add(tf3);
gridData.Columns.Add(tf4);
break;
}
case "F"://"SystemUser":
{
sqlDataSource.SelectCommand = "SELECT DISTINCT SystemUser.Email, SystemUser.UserName, SystemUser.Password, SystemUser.Enabled, SystemUser.EmailOnProblems, SystemUser.Permission, Company.CompanyName, Department.DepartmentName FROM SystemUser INNER JOIN Company ON SystemUser.CompanyEmail = Company.Email INNER JOIN Department ON SystemUser.DepartmentID = Department.DepartmentID";
sqlDataSource.UpdateCommand = "updateSystemUser";
sqlDataSource.DeleteCommand = "deleteSystemUser";
sqlDataSource.SelectCommandType = SqlDataSourceCommandType.Text;
sqlDataSource.UpdateCommandType = SqlDataSourceCommandType.StoredProcedure;
sqlDataSource.DeleteCommandType = SqlDataSourceCommandType.StoredProcedure;
TemplateField tf1 = new TemplateField();
tf1.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, "Email", "Email", new Label());
tf1.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "Email", "Email", new Label());
//tf1.EditItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "Email", "Email", new TextBox()); //PRIMARY KEY
TemplateField tf2 = new TemplateField();
tf2.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, "Name", "UserName", new Label());
tf2.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "Name", "UserName", new Label());
tf2.EditItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "Name", "UserName", new TextBox());
TemplateField tf3 = new TemplateField();
tf3.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, "Enabled", "Enabled", new Label());
tf3.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "Enabled", "Enabled", new Label());
tf3.EditItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "Enabled", "Enabled", new CheckBox());
TemplateField tf4 = new TemplateField();
tf4.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, "Emailed", "EmailOnProblems", new Label());
tf4.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "Emailed", "EmailOnProblems", new Label());
tf4.EditItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "Emailed", "EmailOnProblems", new CheckBox());
TemplateField tf5 = new TemplateField();
tf5.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, "Administrator", "Permission", new Label());
tf5.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "Administrator", "Permission", new Label());
tf5.EditItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "Administrator", "Permission", new CheckBox());
TemplateField tf6 = new TemplateField();
tf6.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, "Company", "CompanyName", new Label());
tf6.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "Company", "CompanyName", new Label());
DropDownList ddlCompany = new DropDownList();
SqlCommand sqlCom = new SqlCommand("SELECT DISTINCT CompanyName FROM Company", sqlCon);
sqlCon.Open();
SqlDataReader sqlRd = sqlCom.ExecuteReader();
while (sqlRd.Read())
{
ddlCompany.Items.Add(sqlRd[0].ToString());
}
sqlRd.Close();
sqlCon.Close();
tf6.EditItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "Company", "CompanyName", ddlCompany);
TemplateField tf7 = new TemplateField();
tf7.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, "Department", "DepartmentName", new Label());
tf7.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "Department", "DepartmentName", new Label());
DropDownList ddlDepartment = new DropDownList();
ddlCompany.Items.Add("");
SqlCommand sqlComD = new SqlCommand("SELECT DISTINCT DepartmentName FROM Department", sqlCon);
sqlCon.Open();
SqlDataReader sqlRdD = sqlComD.ExecuteReader();
while (sqlRdD.Read())
{
ddlDepartment.Items.Add(sqlRdD[0].ToString());
}
sqlRdD.Close();
sqlCon.Close();
tf7.EditItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "Department", "DepartmentName", ddlDepartment);
gridData.Columns.Add(tf1);
gridData.Columns.Add(tf2);
gridData.Columns.Add(tf3);
gridData.Columns.Add(tf4);
gridData.Columns.Add(tf5);
gridData.Columns.Add(tf6);
gridData.Columns.Add(tf7);
break;
}
case "G"://"Customer":
{
sqlDataSource.SelectCommand = "SELECT DISTINCT Customer.Email, Customer.CustomerName, Customer.Telephone, Customer.Fax, Company.CompanyName FROM Customer LEFT OUTER JOIN Company ON Customer.CompanyEmail = Company.Email";
sqlDataSource.UpdateCommand = "updateCustomer";
sqlDataSource.DeleteCommand = "deleteCustomer";
sqlDataSource.SelectCommandType = SqlDataSourceCommandType.Text;
sqlDataSource.UpdateCommandType = SqlDataSourceCommandType.StoredProcedure;
sqlDataSource.DeleteCommandType = SqlDataSourceCommandType.StoredProcedure;
TemplateField tf1 = new TemplateField();
tf1.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, "Email", "Email", new Label());
tf1.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "Email", "Email", new Label());
//tf1.EditItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "Email", "Email", new TextBox()); //PRIMARY KEY
TemplateField tf2 = new TemplateField();
tf2.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, "Name", "CustomerName", new Label());
tf2.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "Name", "CustomerName", new Label());
tf2.EditItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "Name", "CustomerName", new TextBox());
TemplateField tf3 = new TemplateField();
tf3.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, "Telephone", "Telephone", new Label());
tf3.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "Telephone", "Telephone", new Label());
tf3.EditItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "Telephone", "Telephone", new TextBox());
TemplateField tf4 = new TemplateField();
tf4.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, "Fax", "Fax", new Label());
tf4.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "Fax", "Fax", new Label());
tf4.EditItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "Fax", "Fax", new TextBox());
TemplateField tf5 = new TemplateField();
tf5.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, "Company", "CompanyName", new Label());
tf5.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "Company", "CompanyName", new Label());
DropDownList ddl = new DropDownList();
SqlCommand sqlCom = new SqlCommand("SELECT DISTINCT CompanyName FROM Company", sqlCon);
sqlCon.Open();
SqlDataReader sqlRd = sqlCom.ExecuteReader();
while (sqlRd.Read())
{
ddl.Items.Add(sqlRd[0].ToString());
}
sqlRd.Close();
sqlCon.Close();
tf5.EditItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "Company", "CompanyName", ddl);
gridData.Columns.Add(tf1);
gridData.Columns.Add(tf2);
gridData.Columns.Add(tf3);
gridData.Columns.Add(tf4);
gridData.Columns.Add(tf5);
break;
}
case "H"://"ProblemCategory":
{
break;
}
case "I"://"Problem":
{
break;
}
case "J"://"Priority":
{
break;
}
case "K"://"Department":
{
break;
}
case "L"://"Costing":
{
break;
}
}
gridData.DataBind();
}
Or if I click edit on the last row: then the above rows display, except the Email label:
I thought at first that there may be a problem with my labels but i parsed control type TextBox instead and that then displayed the last row as textboxes instead of labels but the other rows are still hidden (not null i don't think, just missing.)
Am I missing a if(!Page.IsPostBack) or something stupid. This is driving me nuts, please help! Any ideas would be appreciated.