public void TestUpdateById() { SqlDataAccessBasic <int, Test> dataAccess = new SqlDataAccessBasic <int, Test>(cConnectionString); Test updatetData = new Test { TestDate = System.DateTime.Now, TestInt = 1, TestString = "String" }; dataAccess.Update(1, updatetData, "Test.UpdateByIdTest"); }
public void ProcessRequest(HttpContext context) { object returnObject = null; ReturnData returnData = new ReturnData(); try { if (UpdateStoredProcedureName == string.Empty) { throw new ArgumentException("Property UpdateStoredProcedureName must be set in handler"); } if (GetByIdStoredProcedureName == string.Empty) { throw new ArgumentException("Property GetByIdStoredProcedureName must be set in handler"); } if (PreExecution != null) { PreExecutionResult result = PreExecution(context); if (result != PreExecutionResult.NoErrors) { throw new PreExecutionFailException(result, "PreExecution Failed"); } } // Get the JSON that was posted string json; using (Stream st = context.Request.InputStream) { byte[] buf = new byte[context.Request.InputStream.Length]; int iRead = st.Read(buf, 0, buf.Length); json = Encoding.UTF8.GetString(buf); } //Deserialize the Javascript JavaScriptSerializer ser = new JavaScriptSerializer(); Dictionary <string, object> ajaxData = (Dictionary <string, object>)ser.Deserialize <Dictionary <string, object> >(json); T obj = (T)Activator.CreateInstance(typeof(T), new object[] { }); foreach (var prop in typeof(T).GetProperties()) { obj.GetType().GetProperty(prop.Name).SetValue(obj, Convert.ChangeType(ajaxData[prop.Name], prop.PropertyType)); } TID id = (TID)Convert.ChangeType(ajaxData["Id"], typeof(TID)); SqlDataAccessBasic <TID, T> dataAccess = new SqlDataAccessBasic <TID, T>(ConnectionString); SqlDataAccessBasic <object, B> dataAccess2 = new SqlDataAccessBasic <object, B>(ConnectionString); dataAccess.Update(id, obj, UpdateStoredProcedureName); returnData.Value = dataAccess2.GetById(id, GetByIdStoredProcedureName); returnData.IsSuccessful = true; if (BusinessLogic != null) { returnObject = BusinessLogic(returnData); } else { returnObject = returnData; } } catch (Exception ex) { //The child class of this class should implement an ErrorHandling funciton. //Error message should be logged and a more user friendly error message should be sent in the string ErrorMessage //CallingMethod should be logged and cleared before being serialized into JSON returnData = new ReturnData { ErrorMessage = ex.ToString(), CallingMethod = this.GetType().Name, IsSuccessful = false }; if (ErrorHandling != null) { returnObject = ErrorHandling(returnData, ex); } else { returnObject = returnData; } } //Convert the results to JSON JavaScriptSerializer jss = new JavaScriptSerializer(); string JSON = jss.Serialize(returnObject); //Send JSON string back to browser context.Response.ContentType = "application/json"; context.Response.Write(JSON); }