コード例 #1
0
        public ActionResult GetTypeDetails(int ID)
        {
            mTypes Details = new mTypes();

            try
            {
                using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ERPReports"].ConnectionString.ToString()))
                {
                    conn.Open();
                    string getTypeSql = "SELECT * FROM mTypes WHERE IsDeleted = '0' AND ID='" + ID + "'";
                    using (SqlCommand comm = new SqlCommand(getTypeSql, conn))
                    {
                        SqlDataReader reader = comm.ExecuteReader();
                        if (!reader.Read())
                        {
                            throw new InvalidOperationException("No records found.");
                        }

                        Details.ID   = Convert.ToInt32(reader["ID"]);
                        Details.Type = reader["Type"].ToString();
                    }
                    conn.Close();
                }
            }
            catch (Exception err)
            {
                string errmsg;
                if (err.InnerException != null)
                {
                    errmsg = "Error: " + err.InnerException.ToString();
                }
                else
                {
                    errmsg = "Error: " + err.Message.ToString();
                }

                return(Json(new { success = false, errors = errmsg }, JsonRequestBehavior.AllowGet));
            }
            return(Json(new { success = true, data = new { generalData = Details } }, JsonRequestBehavior.AllowGet));
        }
コード例 #2
0
        public ActionResult SaveType(mTypes TypeObj)
        {
            string endMsg = "";

            ModelState.Remove("ID");
            if (ModelState.IsValid)
            {
                try
                {
                    using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ERPReports"].ToString()))
                    {
                        conn.Open();
                        using (SqlCommand cmdSql = conn.CreateCommand())
                        {
                            cmdSql.CommandType = CommandType.StoredProcedure;
                            cmdSql.CommandText = "spType_InsertUpdate";
                            cmdSql.Parameters.Clear();
                            cmdSql.Parameters.AddWithValue("@ID", TypeObj.ID);
                            cmdSql.Parameters.AddWithValue("@Type", TypeObj.Type);
                            cmdSql.Parameters.AddWithValue("@CreateID", Session["UserName"]);
                            SqlParameter EndMsg       = cmdSql.Parameters.Add("@EndMsg", SqlDbType.VarChar, 200);
                            SqlParameter ErrorMessage = cmdSql.Parameters.Add("@ErrorMessage", SqlDbType.VarChar, 200);
                            SqlParameter Error        = cmdSql.Parameters.Add("@Error", SqlDbType.Bit);

                            EndMsg.Direction       = ParameterDirection.Output;
                            Error.Direction        = ParameterDirection.Output;
                            ErrorMessage.Direction = ParameterDirection.Output;

                            cmdSql.ExecuteNonQuery();

                            error = Convert.ToBoolean(Error.Value);
                            if (error)
                            {
                                modelErrors.Add(ErrorMessage.Value.ToString());
                            }

                            endMsg = EndMsg.Value.ToString();
                        }
                        conn.Close();
                    }
                }
                catch (Exception err)
                {
                    string errmsg;
                    if (err.InnerException != null)
                    {
                        errmsg = "Error: " + err.InnerException.ToString();
                    }
                    else
                    {
                        errmsg = "Error: " + err.Message.ToString();
                    }

                    return(Json(new { success = false, errors = errmsg }, JsonRequestBehavior.AllowGet));
                }
            }
            else
            {
                foreach (var modelStateKey in ViewData.ModelState.Keys)
                {
                    var modelStateVal = ViewData.ModelState[modelStateKey];
                    foreach (var error in modelStateVal.Errors)
                    {
                        var key        = modelStateKey;
                        var errMessage = error.ErrorMessage;
                        var exception  = error.Exception;
                        modelErrors.Add(errMessage);
                    }
                }
            }
            if (modelErrors.Count != 0 || error)
            {
                return(Json(new { success = false, errors = modelErrors }));
            }
            else
            {
                return(Json(new { success = true, msg = "Data was successfully " + endMsg }));
            }
        }