I'm trying to make a few webpages that will allow us in the tech support team at the factory I work at to provide support faster by adding a simple interface to actions we perform everyday directly on the database.
The following is for a simple page where we will enter a list of serial numbers and select from a few choices of actions to perform on them (actions are mostly executing a stored procedure or query in order to change something in the database). It consists of a texbox where we will enter a list of serials, a dropdown list and a checkbox. These basically move the serials to a diferent location and remove its flag as a finished product in the database. It also has two gridviews to show the serial data before and after the change. All this is inside panels to make each visible/invisible easily depending on where we are in the execution. The actuall calls to the database are in a different class which i'm not including here but are very straightforward parametrized queries and stored procedures.
I'm by no means an expert programmer but very interested in moving my career in that direction. I would like to know how a more experienced professional would have done this.
This is the code behind the webpage. Any comments are welcome, thank you very much.
namespace UnpackTool
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
#region ViewStates
private List<string> CRIDS {
get
{
if (ViewState["CRIDS"] != null )
{
return (List<string>)ViewState["CRIDS"];
}
return null;
}
set
{
ViewState["CRIDS"] = value;
}
}
private List<string> SerialNumbers
{
get
{
if (ViewState["SerialNumbers"] != null)
{
return (List<string>)ViewState["SerialNumbers"];
}
return null;
}
set
{
ViewState["SerialNumbers"] = value;
}
}
private DataTable SerialData
{
get
{
if (ViewState["SerialData"] != null)
{
return (DataTable)ViewState["SerialData"];
}
return null;
}
set
{
ViewState["SerialData"] = value;
}
}
#endregion
#region Helpers
//Clear viewstate values and grids to start over
private void ClearSerials()
{
CRIDS = null;
SerialNumbers = null;
SerialData = null;
CRIDS = null;
SearchGrid.DataSource = null;
ResultsGrid.DataSource = null;
SearchGrid.DataBind();
ResultsGrid.DataBind();
}
// Switch view between panels according to current state
public void SwitchView(string sender)
{
if (sender == "Search")
{
Panel1.Visible = false;
Panel2.Visible = true;
Panel3.Visible = false;
SerialBox.Enabled = false;
}
if (sender == "Cancel")
{
Panel1.Visible = true;
Panel2.Visible = false;
Panel3.Visible = false;
SerialBox.Enabled = true;
UnpackCheck.Checked = false;
ProcessList.SelectedValue = "NO CHANGE";
HideErrorMessage();
}
if (sender == "Accept")
{
Panel1.Visible = false;
Panel2.Visible = false;
Panel3.Visible = true;
SerialBox.Enabled = false;
}
if (sender == "Return")
{
Panel1.Visible = true;
Panel2.Visible = false;
Panel3.Visible = false;
SerialBox.Enabled = true;
SerialBox.Text = "";
UnpackCheck.Checked = false;
ProcessList.SelectedValue = "NO CHANGE";
HideErrorMessage();
}
}
private void ShowErrorMessage(string Error)
{
ErrorLabel.Text = Error;
ErrorLabel.ForeColor = System.Drawing.Color.Red;
ErrorLabel.Visible = true;
}
private void HideErrorMessage()
{
ErrorLabel.Text = "";
ErrorLabel.ForeColor = System.Drawing.Color.Black;
ErrorLabel.Visible = false;
}
#endregion
#region Events
//Read from textbbox and get serial info from db
protected void SearchBtn_Click(object sender, EventArgs e)
{
GetSerials();
SwitchView("Search");
//Only get serial info from db if actual data is entered in textbox
if (SerialNumbers.Count > 0)
{
GetSerialData();
GetModelProcess();
}
}
//Check if serials should be moved and/or unpacked, and perform actions
protected void AcceptBtn_Click(object sender, EventArgs e)
{
//only do something if valid data is entered
if (SerialNumbers.Count > 0)
{
try
{
//If Unpack is checked, serials are unpacked
if (UnpackCheck.Checked)
{
UnpackSerials();
}
//When value is NO CHANGE, serials arent moved
if (ProcessList.SelectedValue.ToString() != "NO CHANGE")
{
MoveSerials();
}
//Get serial info again to see results
GetSerialData();
SwitchView("Accept");
HideErrorMessage();
}
catch (Exception ex)
{
ShowErrorMessage(ex.Message);
}
}
}
//Reset view
protected void CancelBtn_Click(object sender, EventArgs e)
{
SwitchView("Cancel");
ClearSerials();
}
//Reset view
protected void ReturnBtn_Click(object sender, EventArgs e)
{
SwitchView("Return");
ClearSerials();
}
#endregion
#region DataAccess
//Get serial info from db
private void GetSerials()
{
//If CrateID is selected search by CRID
if (InputType.SelectedValue.ToString() == "CrateID")
{
//Read from textbox into CRID viewstate list
CRIDS = new List<string>(SerialBox.Text.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries));
//Method received list of CRID and returns list of serials
SerialNumbers = Dal.ExecuteCRIDSerialQuery(CRIDS).AsEnumerable().Select(x => x[0].ToString()).ToList();
}
else
{
//Read from textbox into SerialNumber viewstate list
SerialNumbers = new List<string>(SerialBox.Text.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries));
}
}
private void UnpackSerials()
{
//Unpack method receives list, returns nothing
Dal.ExecuteUnpackQuery(SerialNumbers);
}
private void MoveSerials()
{
//Move serials, executes stored procedure, receives single serialnumber, iterate through list executing for each
if (SerialData.Rows.Count > 0)
{
foreach (DataRow row in SerialData.Rows)
{
Dal.ExecuteMoveQuery(row["TSN"].ToString(), row["Model"].ToString(), ProcessList.SelectedValue.ToString());
}
}
}
private void GetSerialData()
{
DataTable serialdata = new DataTable();
//Get serial data from db, method receives list of serials, returns data table
SerialData = Dal.ExecuteResultsQuery(SerialNumbers);
//Fill datagrids
SearchGrid.DataSource = SerialData;
ResultsGrid.DataSource = SerialData;
SearchGrid.DataBind();
ResultsGrid.DataBind();
}
//Gets processed used by the models listed in the serial data, can receive different models, binds to dropdownlist
private void GetModelProcess()
{
if (SerialNumbers != null)
{
DataTable Processes = Dal.ExecuteProcessByModelQuery(SerialData.AsEnumerable().Select(x => x["Model"].ToString()).ToList());
ProcessList.DataSource = Processes;
ProcessList.DataTextField = "Process_Name";
ProcessList.DataValueField = "Process_Name";
ProcessList.DataBind();
//When value is NO CHANGE, serials arent moved
ProcessList.SelectedValue = "NO CHANGE";
}
}
#endregion
}
}