ASP.net_Editable_Demo.FakeDatabase.update C# (CSharp) Method

update() public static method

public static update ( string>.Dictionary data ) : void
data string>.Dictionary
return void
        public static void update(Dictionary<string, string> data)
        {
            int id = int.Parse(data["EmpId"]);
            string fname = data["updatedFieldName"].Replace(" ", "");
            string fval = data["updatedFieldValue"];

            Employee e = db[id];

            //find and set the location property in loc name.
            if(fname == "DOB")
                e.GetType().GetProperty(fname).SetValue(e, Convert.ToDateTime(fval), null);
            else
                e.GetType().GetProperty(fname).SetValue(e, fval, null);

            db[id] = e;
        }

Usage Example

        public void ProcessRequest(HttpContext context)
        {
            var contextData = context.Request;
            var sr          = new StreamReader(contextData.InputStream);
            var stream      = sr.ReadToEnd();

            var javaScriptSerializer = new JavaScriptSerializer();
            var data = javaScriptSerializer.Deserialize <Dictionary <string, string> >(stream);

            string command = data["command"];
            string table   = data["table"];

            string response     = "";
            string message      = "";
            object responseData = null;

            try
            {
                //employee table operations
                if (table == "Employees")
                {
                    if (command == "Add")
                    {
                        response     = "Update";
                        responseData = "{\"EmpId\":" + FakeDatabase.add(data) + "}";
                    }

                    if (command == "Delete")
                    {
                        FakeDatabase.delete(data);
                    }
                    if (command == "Update")
                    {
                        FakeDatabase.update(data);
                    }

                    if (command == "Load")
                    {
                        response     = "Load";
                        responseData = FakeDatabase.select(data);
                    }
                }

                response = response == "" ? "Success" : response;
                message  = "Hell Yeah!";
            }
            catch (Exception e)
            {
                response = "Error";
                message  = e.Message;
            }

            context.Response.ContentType = "application/json; charset=utf-8";

            Dictionary <string, object> responseItems = new Dictionary <string, object>();

            if (response != null)
            {
                responseItems.Add("response", response);
            }
            if (message != null)
            {
                responseItems.Add("message", message);
            }
            if (responseData != null)
            {
                responseItems.Add("responseData", responseData);
            }
            //string test = javaScriptSerializer.Serialize(responseItems);
            context.Response.Write(javaScriptSerializer.Serialize(responseItems));
        }