private void FormSampleParameter_Load(object sender, EventArgs e)
        {
            lblTypeInfo.Text = "";

            SampleParameterName spn = new SampleParameterName();

            spn.Id   = Guid.Empty;
            spn.Name = "";
            spn.Type = "";
            cboxSampleParameterNames.Items.Add(spn);

            SqlConnection conn = null;

            try
            {
                conn = DB.OpenConnection();
                SqlDataReader reader = DB.GetDataReader(conn, null, "select id, name, type from sample_parameter_name order by name", CommandType.Text);
                while (reader.Read())
                {
                    spn      = new SampleParameterName();
                    spn.Id   = reader.GetGuid("id");
                    spn.Name = reader.GetString("name");
                    spn.Type = reader.GetString("type");
                    cboxSampleParameterNames.Items.Add(spn);
                }
            }
            catch (Exception ex)
            {
                Common.Log.Error(ex);
                MessageBox.Show(ex.Message);
                DialogResult = DialogResult.Abort;
                Close();
            }
            finally
            {
                conn?.Close();
            }

            cboxSampleParameterNames.DisplayMember = "Name";
            cboxSampleParameterNames.ValueMember   = "Id";

            if (mSPId != Guid.Empty)
            {
                SampleParameter p = mSample.Parameters.Find(x => x.Id == mSPId);
                cboxSampleParameterNames.Text    = p.Name;
                cboxSampleParameterNames.Enabled = false;
                tbSampleParameterValue.Text      = p.Value;
                lblTypeInfo.Text = "Parameter type: " + p.Type;
            }
        }
        public void StoreToDB(SqlConnection conn, SqlTransaction trans)
        {
            if (Id == Guid.Empty)
            {
                throw new Exception("Error: Can not store a sample parameter with an empty id");
            }

            SqlCommand cmd = new SqlCommand("", conn, trans);

            if (!SampleParameter.IdExists(conn, trans, Id))
            {
                // insert new analysis result
                cmd.CommandText = "csp_insert_sample_parameter";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@id", Id);
                cmd.Parameters.AddWithValue("@sample_id", SampleId, Guid.Empty);
                cmd.Parameters.AddWithValue("@sample_parameter_name_id", SampleParameterNameId, Guid.Empty);
                cmd.Parameters.AddWithValue("@value", Value, String.Empty);
                cmd.Parameters.AddWithValue("@create_date", DateTime.Now);
                cmd.Parameters.AddWithValue("@create_id", Common.UserId, Guid.Empty);
                cmd.Parameters.AddWithValue("@update_date", DateTime.Now);
                cmd.Parameters.AddWithValue("@update_id", Common.UserId, Guid.Empty);

                cmd.ExecuteNonQuery();

                Dirty = false;
            }
            else
            {
                if (Dirty)
                {
                    // update existing analysis result
                    cmd.CommandText = "csp_update_sample_parameter";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Clear();
                    cmd.Parameters.AddWithValue("@id", Id);
                    cmd.Parameters.AddWithValue("@value", Value, String.Empty);
                    cmd.Parameters.AddWithValue("@update_date", DateTime.Now);
                    cmd.Parameters.AddWithValue("@update_id", Common.UserId, Guid.Empty);

                    cmd.ExecuteNonQuery();

                    Dirty = false;
                }
            }
        }
        private void btnOk_Click(object sender, EventArgs e)
        {
            SampleParameterName spn = cboxSampleParameterNames.SelectedItem as SampleParameterName;

            if (spn.Id == Guid.Empty)
            {
                MessageBox.Show("You must select a valid parameter type");
                return;
            }

            if (string.IsNullOrEmpty(tbSampleParameterValue.Text.Trim()))
            {
                MessageBox.Show("You must provide a value for this parameter");
                return;
            }

            if (mSPId == Guid.Empty)
            {
                if (mSample.Parameters.Exists(x => x.SampleId == mSample.Id && x.SampleParameterNameId == spn.Id))
                {
                    MessageBox.Show("This sample already have this parameter");
                    return;
                }

                SampleParameter p = new SampleParameter();
                p.SampleId = mSample.Id;
                p.SampleParameterNameId = spn.Id;
                p.Name  = spn.Name;
                p.Type  = spn.Type;
                p.Value = tbSampleParameterValue.Text.ToString();
                mSample.Parameters.Add(p);
                p.Dirty = true;
            }
            else
            {
                SampleParameter p = mSample.Parameters.Find(x => x.Id == mSPId);
                p.Value = tbSampleParameterValue.Text.ToString();
                p.Dirty = true;
            }

            DialogResult = DialogResult.OK;
            Close();
        }
Beispiel #4
0
        public void LoadFromDB(SqlConnection conn, SqlTransaction trans, Guid sampleId)
        {
            using (SqlDataReader reader = DB.GetDataReader(conn, trans, "csp_select_sample", CommandType.StoredProcedure,
                                                           new SqlParameter("@id", sampleId)))
            {
                if (!reader.HasRows)
                {
                    throw new Exception("Error: Sample with id " + sampleId.ToString() + " was not found");
                }

                reader.Read();

                Id                  = reader.GetGuid("id");
                Number              = reader.GetInt32("number");
                LaboratoryId        = reader.GetGuid("laboratory_id");
                SampleTypeId        = reader.GetGuid("sample_type_id");
                SampleStorageId     = reader.GetGuid("sample_storage_id");
                SampleComponentId   = reader.GetGuid("sample_component_id");
                ProjectSubId        = reader.GetGuid("project_sub_id");
                StationId           = reader.GetGuid("station_id");
                SamplerId           = reader.GetGuid("sampler_id");
                SamplingMethodId    = reader.GetGuid("sampling_method_id");
                TransformFromId     = reader.GetGuid("transform_from_id");
                TransformToId       = reader.GetGuid("transform_to_id");
                ImportedFrom        = reader.GetString("imported_from");
                ImportedFromId      = reader.GetString("imported_from_id");
                MunicipalityId      = reader.GetGuid("municipality_id");
                LocationType        = reader.GetString("location_type");
                Location            = reader.GetString("location");
                Latitude            = reader.GetDoubleNullable("latitude");
                Longitude           = reader.GetDoubleNullable("longitude");
                Altitude            = reader.GetDoubleNullable("altitude");
                SamplingDateFrom    = reader.GetDateTimeNullable("sampling_date_from");
                SamplingDateTo      = reader.GetDateTimeNullable("sampling_date_to");
                ReferenceDate       = reader.GetDateTimeNullable("reference_date");
                ExternalId          = reader.GetString("external_id");
                WetWeight_g         = reader.GetDoubleNullable("wet_weight_g");
                DryWeight_g         = reader.GetDoubleNullable("dry_weight_g");
                Volume_l            = reader.GetDoubleNullable("volume_l");
                LodWeightStart      = reader.GetDoubleNullable("lod_weight_start");
                LodWeightEnd        = reader.GetDoubleNullable("lod_weight_end");
                LodTemperature      = reader.GetDoubleNullable("lod_temperature");
                LodWaterPercent     = reader.GetDoubleNullable("lod_water_percent");
                LodFactor           = reader.GetDoubleNullable("lod_factor");
                LodWeightStartAsh   = reader.GetDoubleNullable("lod_weight_ash");
                LodWeightEndAsh     = reader.GetDoubleNullable("lod_weight_end_ash");
                LodTemperatureAsh   = reader.GetDoubleNullable("lod_temperature_ash");
                LodWaterPercentAsh  = reader.GetDoubleNullable("lod_water_percent_ash");
                LodFactorAsh        = reader.GetDoubleNullable("lod_factor_ash");
                LodWeightStartAsh2  = reader.GetDoubleNullable("lod_weight_ash2");
                LodWeightEndAsh2    = reader.GetDoubleNullable("lod_weight_end_ash2");
                LodTemperatureAsh2  = reader.GetDoubleNullable("lod_temperature_ash2");
                LodWaterPercentAsh2 = reader.GetDoubleNullable("lod_water_percent_ash2");
                LodFactorAsh2       = reader.GetDoubleNullable("lod_factor_ash2");
                Confidential        = reader.GetBoolean("confidential");
                InstanceStatusId    = reader.GetInt32("instance_status_id");
                LockedId            = reader.GetGuid("locked_id");
                Comment             = reader.GetString("comment");
                CreateDate          = reader.GetDateTime("create_date");
                CreateId            = reader.GetGuid("create_id");
                UpdateDate          = reader.GetDateTime("update_date");
                UpdateId            = reader.GetGuid("update_id");
            }

            // Load parameters
            Parameters.Clear();

            List <Guid> sampParamIds = new List <Guid>();

            using (SqlDataReader reader = DB.GetDataReader(conn, trans, "select id from sample_parameter where sample_id = @id", CommandType.Text,
                                                           new SqlParameter("@id", sampleId)))
            {
                while (reader.Read())
                {
                    sampParamIds.Add(reader.GetGuid("id"));
                }
            }

            foreach (Guid sampParamId in sampParamIds)
            {
                SampleParameter p = new SampleParameter();
                p.LoadFromDB(conn, trans, sampParamId);
                Parameters.Add(p);
            }

            // Load preparations
            Preparations.Clear();

            List <Guid> preparationIds = new List <Guid>();

            using (SqlDataReader reader = DB.GetDataReader(conn, trans, "select id from preparation where sample_id = @id", CommandType.Text,
                                                           new SqlParameter("@id", Id)))
            {
                while (reader.Read())
                {
                    preparationIds.Add(reader.GetGuid("id"));
                }
            }

            foreach (Guid pId in preparationIds)
            {
                Preparation p = new Preparation();
                p.LoadFromDB(conn, trans, pId);
                Preparations.Add(p);
            }

            Dirty = false;
        }