public WorkTicketLog() { initHardCodedVals(); NewOrOld = true; //if true, is new record workTicketLogLinkID = -1; workTicketLogTime = Convert.ToDateTime(System.DateTime.Now.ToString()); workTicketLogCategory = ""; workTicketLogCreator = ""; workTicketLogTitle = ""; workTicketLogDesc = ""; workTicketLogTimeTakenHours = 0; //reads in db connection string try { dbInString = ConfigurationManager.ConnectionStrings["default"].ConnectionString; } catch (Exception e) { throw e; } //this will cause error conditions later wkCategories = new WorkCategories(); }
public int Load(int workTicketID, DateTime logTime) { String queryFull = "SELECT * FROM " + tableName + " ", queryCondition = "WHERE " + attribNames[0] + "=" + workTicketID + " AND " + attribNames[1] + "='" + logTime.ToString("yyyy-MM-dd HH:mm:ss") + "'"; int rCheck = 0; //used for error-checking later String[] outputVars = new String[numAttribs + 1]; //check workTicketID is sensible if (workTicketID < 0) { return -1; } //reinitialise work categories list (just in case) wkCategories = new WorkCategories(); //build query queryFull += queryCondition; NewOrOld = false; //importing existing record //initialise output array for (int i = 0; i < outputVars.Length; i++) { outputVars[i] = ""; } //attempt DB connection try { dbConnection = new SqlConnection(dbInString); dbConnection.Open(); } catch { return -1; } //build query string dbCommand = dbConnection.CreateCommand(); dbCommand.CommandText = queryFull; dbCommand.CommandType = CommandType.Text; dbCommand.CommandTimeout = 15; //Run command, and tidy-up if exceptions occur try { dbReader = dbCommand.ExecuteReader(); } catch { dbReader.Close(); dbConnection.Close(); return -1; } //Import values from query into string array while (dbReader.Read()) { if (rCheck > 0) { return -1; //if more than one result was returned, run hard & run fast! } else { int i = 0; //iterator for output string foreach (String att in attribNames) { try { outputVars[i] = dbReader[att].ToString(); } catch { return -1; } i++; } } rCheck++; //tracks number of results } //Verify that at least one result was returned if (rCheck < 1) { return -1; }; //Import values from Db try { workTicketLogLinkID = Convert.ToInt32(outputVars[0]); } catch { return -1; } try { workTicketLogTime = DateTime.Parse(outputVars[1]); } catch { return -1; } workTicketLogCategory = outputVars[2]; workTicketLogCreator = outputVars[3]; workTicketLogTitle = outputVars[4]; workTicketLogDesc = outputVars[5]; try { workTicketLogTimeTakenHours = Convert.ToDecimal(outputVars[6]); } catch { return -1; } //Clean-up DB connection try { dbReader.Close(); dbConnection.Close(); } catch { return -1; } NewOrOld = false; //fail-safe return 0; }