// Commit changes from the new object form // or edits made to the existing object form. public void Update() { using (var db = new ApirsDatabase()) { try { if (SelectedDrilling.drillIdPk == 0) { try { db.tblDrillings.Add(SelectedDrilling); db.SaveChanges(); TryClose(); } catch { ((ShellViewModel)IoC.Get <IShell>()).ShowInformation("Object can't be added. Please check every field again."); return; } } else { tblDrilling result = db.tblDrillings.Where(drill => drill.drillName == SelectedDrilling.drillName).First(); if (result != null) { db.Entry <tblDrilling>(result).CurrentValues.SetValues(SelectedDrilling); db.SaveChanges(); } } } catch (SqlException ex) { ((ShellViewModel)IoC.Get <IShell>()).ShowInformation("Please provide valid input parameters"); } catch (Exception e) { ((ShellViewModel)IoC.Get <IShell>()).ShowInformation("Something went wrong"); } finally { } } }
/// <summary> /// Saving changes to the data context /// </summary> public void Save() { try { _apirsDatabase.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { foreach (var entry in ex.Entries) { entry.OriginalValues.SetValues(entry.GetDatabaseValues()); } } }
/// <summary> /// Uploading a filestream to the server /// </summary> /// <param name="fileStreamByte"></param> /// <returns></returns> public static Guid UploadFile(string filePath) { if (filePath != "") { //Getting file information FileInfo fi = new FileInfo(filePath); FileStream fs; try { //Implementing a new filestream fs = new FileStream(fi.FullName, FileMode.Open, FileAccess.Read); } catch { MessageBox.ShowError("File is being used by another process."); return(new Guid()); } //Transfering filestream into binary format BinaryReader rdr = new BinaryReader(fs); byte[] fileData = rdr.ReadBytes((int)fs.Length); //Closing filestream rdr.Close(); fs.Close(); try { //Instantiate database using (ApirsDatabase db = new ApirsDatabase()) { //Retrieving file meta data string fileName = fi.Name; string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileName); string extension = fi.Extension; char charac = 'a'; //Changing File name based on the count of occurences while (db.v_FileStore.Where(x => x.name == fileName).Count() > 0) { fileName = fileNameWithoutExtension + charac + extension; charac++; } //Establishing a sql connection using (SqlConnection SqlConn = new SqlConnection(db.Database.Connection.ConnectionString.ToString())) { SqlCommand spAddFile = new SqlCommand("dbo.spAddFile", SqlConn); //Testing if a connection is established //Normally: if (sv.IsNetworkAvailable() && sv.IsServerConnected("130.83.96.87")) if (ServerInteractionHelper.IsNetworkAvailable()) { //Preparing the stored procedure spAddFile.CommandType = CommandType.StoredProcedure; //Adding the parameters spAddFile.Parameters.Add("@pName", SqlDbType.NVarChar, 255).Value = fileName; spAddFile.Parameters.Add("@pFile_Stream", SqlDbType.Image, fileData.Length).Value = fileData; //Opening the connection SqlConn.Open(); Guid result = (Guid)spAddFile.ExecuteScalar(); SqlConn.Close(); db.SaveChanges(); return(result); } } } } catch (Exception e) { MessageBox.ShowError(UserMessageValueConverter.ConvertBack(1)); return(new Guid()); } } return(new Guid()); }