/// <summary> /// This button event reads all the available text in textboxes and assign them to the object properties. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void DataSubmitBtn_Click(object sender, EventArgs e) { if (ValidateInputData()) { SpecimenModel model = new SpecimenModel( userID.Text, userName.Text, gbType.Text, gbEnergy.Text, solidificationFactor.Text, thermalGradient.Text); //foreach(IDataConnection db in GlobalConfig.ConnectionChoice) //{ // db.CreateSampleSubmission(model); //} GlobalConfig.ConnectionChoice.CreateSampleSubmission(model); userID.Text = ""; userName.Text = ""; gbType.Text = "000"; gbEnergy.Text = "0"; solidificationFactor.Text = "0"; thermalGradient.Text = "0"; } else { MessageBox.Show("This form has invalid data. Please follow the error labels"); } }
{ /// <summary> /// Using Dapper, Data is stored into database /// </summary> /// <param name="model"></param> /// <returns></returns> public SpecimenModel CreateSampleSubmission(SpecimenModel model) { try { //download sqlcleint //getting database name using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(GlobalConfig.GetConnectionString("GBAnalysisDB"))) { var val = new DynamicParameters(); val.Add("@UserId", model.UserId); val.Add("@UserName", model.UserName); val.Add("@GBLabel", model.GBLabel); val.Add("@GBEnergy", model.GBEnergy); val.Add("@SolidificationFactor", model.SolidificationFactor); val.Add("@ThermalFactor", model.ThermalFactor); val.Add("@id", 0, dbType: DbType.Int32, ParameterDirection.Output); connection.Execute("dbo.spSpecimens_Insert", val, commandType: CommandType.StoredProcedure);//store procedure=dbo.spSpecimens_Insert model.Id = val.Get <int>("@id"); return(model); } } catch (Exception) { throw; } }
/// <summary> /// This static metho gets the lines of whole document of text file data or single record. It return the object models initialized according to saved data. /// </summary> /// <param name="lines"></param> /// <returns></returns> public static List <SpecimenModel> TextRecordToObjectProperties(this List <string> lines) { List <SpecimenModel> output = new List <SpecimenModel>(); foreach (string index in lines) { string[] cols = index.Split(','); SpecimenModel p = new SpecimenModel(); p.Id = int.Parse(cols[0]); p.UserId = int.Parse(cols[1]); p.UserName = cols[2]; p.GBLabel = cols[3]; p.GBEnergy = double.Parse(cols[4]); p.SolidificationFactor = double.Parse(cols[5]); p.ThermalFactor = double.Parse(cols[6]); output.Add(p); } return(output); }
/// <summary> /// This class creates or modifies the text database file for new entries. /// </summary> /// <param name="model"></param> /// <returns></returns> public SpecimenModel CreateSampleSubmission(SpecimenModel model) { //*loading the data from existing file IN TEXTCONNECTIONPROCESSOR //*Converting to appropriate dataTypes //List<SpecimenModel> samples = SampleFileName.FullFilePathFromConfigFile().Loadfile().TextRecordToObjectProperties(); List <SpecimenModel> samples = SampleFileName.PortableFilePathByRuntimeDirectory().Loadfile().TextRecordToObjectProperties(); // Finding maximum data length and Last stored ID int currentId = 1; if (samples.Count > 0) { currentId = samples.OrderByDescending(x => x.Id).First().Id + 1; } // Adding new record at new id after last max id (newID= maxID+1) model.Id = currentId; samples.Add(model); // convert model to List<string> // Save file list to output text file samples.SaveToSampleFile(SampleFileName); return(model); }