public void Load(int ID) { FormDataAdapter adapter = new FormDataAdapter(); try { Form f = adapter.GetForm(ID); this.ID = f.ID; this.CreatedDate = f.CreatedDate; this.CreatedBy = f.CreatedBy; this.Template = f.Template; this.Sections = f.Template.Sections; } catch (Exception ex) { throw new Exception("Error trying to load form in Template.Load(). See inner exception for details.", ex); } }
// POST api/template public object Post(Template template) { if (template != null) { try { template.Save(); return new { result = "success" }; } catch (Exception ex) { return new { result = "Error", error = ex }; } } else { return new { result = "Error", error = "Invalid data returned to server." }; } }
public void New(int TemplateID) { TemplateDataAdapter adapter = new TemplateDataAdapter(); try { this.Template = adapter.GetTemplate(TemplateID); } catch (Exception ex) { throw new Exception("Error trying to load teamplate in Template.New(). See inner exception for details.", ex); } try { this.Sections = Template.Sections; } catch (Exception ex) { throw new Exception("Error trying to load form in Template.New(). See inner exception for details.", ex); } }
/// <summary> /// Adapter function to get data from DB and load it into a From object. /// </summary> /// <param name="ID">ID of the form to get.</param> /// <param name="Recursive">Defaults to true. If set to false, only gets the data for the form and nothing further (IE the sections).</param> public Template GetTemplate(int ID, bool Recursive = true) { ClearData(); DataSet templateDS = new DataSet(); InternalTemplate = new Template(); comm.CommandText = "SELECT * FROM " + Config.TableNames["Templates"] + " WHERE ID=@ID;"; comm.Parameters.AddWithValue("@ID", ID); try { da.Fill(templateDS); var formMeta = templateDS.GetResults(); if (formMeta != null) { InternalTemplate.ID = ID; InternalTemplate.Name = (string)formMeta[0]["Name"]; InternalTemplate.Description = (string)formMeta[0]["Description"]; InternalTemplate.Sections = new List<Section>(); if (Recursive) { GetSections(Recursive); } } else { throw new Exception("No results found"); } } catch (Exception ex) { throw new Exception("Error in getFormStructure(): " + ex.Message); } //Close dat connection. No longer needed, bro! conn.Close(); return InternalTemplate; }
public TemplateCollection GetTemplates(int Number = 10) { DataSet templatesDS = new DataSet(); InternalTemplate = new Template(); TemplateCollection Forms = new TemplateCollection(); comm.CommandText = "SELECT TOP(@num) ID FROM " + Config.TableNames["Templates"]; comm.Parameters.AddWithValue("@num", Number); try { da.Fill(templatesDS); } catch (Exception ex) { throw new Exception("Error trying to open database connection in GetTopForms: " + ex.Message, ex); } var ids = templatesDS.GetResults(); if (ids != null) { foreach (DataRow id in ids) { Template formRef = GetTemplate((int)id["ID"]); Forms.Add(formRef); } } else { //throw new Exception("No results found"); return null; } return Forms; }
/// <summary> /// Save the contents of a form to the database /// </summary> public void SaveTemplate(Template template) { InternalTemplate = template; ClearData(); //Update the form information //conn.BeginTransaction("formTransaction"); //Reusing the parameters from the statement above. //TODO: do a check before updating/inserting? if (InternalTemplate.ID.HasValue) { comm.CommandText = "UPDATE " + Config.TableNames["Templates"] + " SET Name=@Name, Description=@Description WHERE ID=@ID"; } else { comm.CommandText = "INSERT INTO " + Config.TableNames["Templates"] + " (Name, Description) VALUES (@Name,@Description);SELECT SCOPE_IDENTITY();"; } comm.Parameters.AddWithValue("@Name", InternalTemplate.Name); comm.Parameters.AddWithValue("@Description", InternalTemplate.Description); if (!InternalTemplate.ID.HasValue) { int result = Convert.ToInt32(comm.ExecuteScalar()); InternalTemplate.ID = result; } else { comm.Parameters.AddWithValue("@ID", InternalTemplate.ID); comm.ExecuteNonQuery(); } try { SaveSections(); } catch (Exception ex) { throw ex; } }