public wsSQLResult DeleteUser(string UserID) { wsSQLResult result = new wsSQLResult(); try { HomeNetworkDataContext dc = new HomeNetworkDataContext(); Users_HNU user = dc.Users_HNUs.Where(u => u.UserID == UserID).FirstOrDefault(); if (user == null) { result.WasSuccessful = -3; result.Exception = "Could not find a [Users_HNU] record with ID: " + UserID.ToString(); return(result); } dc.Users_HNUs.DeleteOnSubmit(user); dc.SubmitChanges(); result.WasSuccessful = 1; result.Exception = ""; return(result); //Success } catch (Exception ex) { result.WasSuccessful = -1; result.Exception = "An exception occurred: " + ex.Message; return(result); //Failed } }
public wsSQLResult InsertNewUser(Stream JSONdataStream) { wsSQLResult result = new wsSQLResult(); try { //Read JSON Stream into a String.. StreamReader reader = new StreamReader(JSONdataStream); string JSONdata = reader.ReadToEnd(); // Convert to New User record.. JavaScriptSerializer jss = new JavaScriptSerializer(); Users user = jss.Deserialize <Users>(JSONdata); if (user == null) { // Error: Couldn't deserialize JSON String result.WasSuccessful = 0; result.Exception = "Unable to deserialize the JSON data."; return(result); } HomeNetworkDataContext dc = new HomeNetworkDataContext(); //if (user.UserID == Convert.ToString(dc.Users_HNUs.Where(i => i.UserID == user.UserID).FirstOrDefault())) //{ // // User Already Exists // return -3; //} // Insert Record to SQL Server Table Users_HNU newUser = new Users_HNU() { UserID = user.UserID, FirstName = user.FirstName, LastName = user.LastName, Password = user.Password, MachineID = user.MachineID }; dc.Users_HNUs.InsertOnSubmit(newUser); dc.SubmitChanges(); result.WasSuccessful = 1; result.Exception = ""; return(result); } catch (Exception ex) { result.WasSuccessful = 0; result.Exception = ex.Message; return(result); } }
public wsSQLResult UpdateUser(Stream JSONdataStream) { wsSQLResult result = new wsSQLResult(); try { //Read JSON Stream into a String.. StreamReader reader = new StreamReader(JSONdataStream); string JSONdata = reader.ReadToEnd(); // Convert to New User record.. JavaScriptSerializer jss = new JavaScriptSerializer(); Users user = jss.Deserialize <Users>(JSONdata); if (user == null) { // Error: Couldn't deserialize JSON String result.WasSuccessful = 0; result.Exception = "Unable to deserialize the JSON data."; return(result); } HomeNetworkDataContext dc = new HomeNetworkDataContext(); Users_HNU currentUser = dc.Users_HNUs.Where(u => u.UserID == user.UserID).FirstOrDefault(); if (currentUser == null) { // Couldnt Find User to Update result.WasSuccessful = -3; result.Exception = "Could not find a [Users_HNU] record with ID: " + user.UserID.ToString(); return(result); } // Update Record to SQL Server Table currentUser.UserID = user.UserID; currentUser.FirstName = user.FirstName; currentUser.LastName = user.LastName; currentUser.Password = user.Password; currentUser.MachineID = user.MachineID; dc.SubmitChanges(); result.WasSuccessful = 1; result.Exception = ""; return(result); //Success } catch (Exception ex) { result.WasSuccessful = -1; result.Exception = "An exception occurred: " + ex.Message; return(result); //Failed } }