<connectionStrings>
<add name="SqlConnectionString" connectionString="MyConnectionString" providerName="System.Data.SqlClient" />
</connectionStrings>
public static class DBConnection
{
public static string ConnectionString = ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString;
}
public ActionResult Fault()
{
ViewBag.Faults = _oFaultBL.Gets().OrderBy(x => x.Name);
return View(new Fault());
}
[HttpPost]
public JsonResult SaveFault(Fault fault)
{
try
{
fault = _oFaultBL.Save(fault);
}
catch (Exception ex)
{
fault = new Fault() { Message = ex.Message };
}
JavaScriptSerializer serializer = new JavaScriptSerializer();
string json = serializer.Serialize(fault);
return Json(json, JsonRequestBehavior.AllowGet);
}
public class FaultBL
{
Fault _oFault = new Fault();
List<Fault> _oFaults = new List<Fault>();
SqlConnection sqlCon = new SqlConnection(DBConnection.ConnectionString);
SqlCommand sqlCom = null;
public Fault Save(Fault fault)
{
try
{
sqlCon.Open();
sqlCom = new SqlCommand("SP_Fault", sqlCon);
sqlCom.CommandType = CommandType.StoredProcedure;
sqlCom = this.SetParameters(sqlCom, fault, fault.Id > 0 ? EnumOperationTypes.Update : EnumOperationTypes.Save);
sqlCon.Close();
sqlCon.Open();
SqlDataReader reader = sqlCom.ExecuteReader();
_oFault = new Fault();
while (reader.Read()) _oFault = this.Mapping(reader);
}
catch (Exception ex)
{
_oFault = new Fault() { Message = ex.Message.Split('~')[0] };
}
finally
{
sqlCom.Dispose();
sqlCon.Close();
}
return _oFault;
}
public string Delete(Fault fault)
{
string message = "";
try
{
sqlCon.Open();
sqlCom = new SqlCommand("SP_Fault", sqlCon);
sqlCom.CommandType = CommandType.StoredProcedure;
sqlCom = this.SetParameters(sqlCom, fault, EnumOperationTypes.Delete);
sqlCom.ExecuteNonQuery();
sqlCon.Close();
message = "Deleted";
}
catch (Exception ex)
{
message = ex.Message.Split('~')[0];
}
finally
{
sqlCom.Dispose();
}
return message;
}
public Fault Get(long id)
{
return this.ReturnObject("SELECT * FROM View_Fault WHERE Id = " + id);
}
public List<Fault> Gets()
{
return this.ReturnList("SELECT * FROM View_Fault");
}
private Fault ReturnObject(string sSQL)
{
var oFault = new Fault();
try
{
sqlCon.Open();
sqlCom = new SqlCommand(sSQL, sqlCon);
SqlDataReader reader = sqlCom.ExecuteReader();
while (reader.Read()) oFault = this.Mapping(reader);
}
catch (Exception ex)
{
oFault = new Fault() { Message = ex.Message.Split('~')[0] };
}
finally
{
sqlCom.Dispose();
sqlCon.Close();
}
return oFault;
}
private List<Fault> ReturnList(string sSQL)
{
var oFaults = new List<Fault>();
try
{
sqlCon.Open();
sqlCom = new SqlCommand(sSQL, sqlCon);
SqlDataReader reader = sqlCom.ExecuteReader();
while (reader.Read()) oFaults.Add(this.Mapping(reader));
}
catch (Exception ex)
{
oFaults = new List<Fault>();
oFaults.Add(new Fault() { Message = ex.Message.Split('~')[0] });
}
finally
{
sqlCom.Dispose();
sqlCon.Close();
}
return oFaults;
}
public SqlCommand SetParameters(SqlCommand sqlCom, Fault oFault, EnumOperationTypes operationType)
{
sqlCom.Parameters.AddWithValue("@Id", oFault.Id);
sqlCom.Parameters.AddWithValue("@Name", oFault.Name);
sqlCom.Parameters.AddWithValue("@OperationType", operationType);
return sqlCom;
}
public Fault Mapping(SqlDataReader reader)
{
return new Fault()
{
Id = (long)reader["Id"],
Name = reader["Name"].ToString()
};
}
}
public enum EnumOperationTypes
{
Save = 1,
Update = 2,
Delete = 3
}
$(document).ready(function () {
_faults = @Html.Raw(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(ViewBag.Faults));
});
$.ajax({
type: "POST",
dataType: "json",
url: "/Fault/SaveFault",
traditional: true,
data: JSON.stringify(fault),
contentType: "application/json;charset=utf-8",
success: function (data) {
var faultObj = jQuery.parseJSON(data);
if ($.trim(faultObj.Message) != "") {
DisplaySavedMessage(id, "error");
return false;
}\
},
error: function (xhr, status, error) {
alert(error);
}
});
<add name="SqlConnectionString" connectionString="MyConnectionString" providerName="System.Data.SqlClient" />
</connectionStrings>
public static class DBConnection
{
public static string ConnectionString = ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString;
}
public ActionResult Fault()
{
ViewBag.Faults = _oFaultBL.Gets().OrderBy(x => x.Name);
return View(new Fault());
}
[HttpPost]
public JsonResult SaveFault(Fault fault)
{
try
{
fault = _oFaultBL.Save(fault);
}
catch (Exception ex)
{
fault = new Fault() { Message = ex.Message };
}
JavaScriptSerializer serializer = new JavaScriptSerializer();
string json = serializer.Serialize(fault);
return Json(json, JsonRequestBehavior.AllowGet);
}
public class FaultBL
{
Fault _oFault = new Fault();
List<Fault> _oFaults = new List<Fault>();
SqlConnection sqlCon = new SqlConnection(DBConnection.ConnectionString);
SqlCommand sqlCom = null;
public Fault Save(Fault fault)
{
try
{
sqlCon.Open();
sqlCom = new SqlCommand("SP_Fault", sqlCon);
sqlCom.CommandType = CommandType.StoredProcedure;
sqlCom = this.SetParameters(sqlCom, fault, fault.Id > 0 ? EnumOperationTypes.Update : EnumOperationTypes.Save);
sqlCon.Close();
sqlCon.Open();
SqlDataReader reader = sqlCom.ExecuteReader();
_oFault = new Fault();
while (reader.Read()) _oFault = this.Mapping(reader);
}
catch (Exception ex)
{
_oFault = new Fault() { Message = ex.Message.Split('~')[0] };
}
finally
{
sqlCom.Dispose();
sqlCon.Close();
}
return _oFault;
}
public string Delete(Fault fault)
{
string message = "";
try
{
sqlCon.Open();
sqlCom = new SqlCommand("SP_Fault", sqlCon);
sqlCom.CommandType = CommandType.StoredProcedure;
sqlCom = this.SetParameters(sqlCom, fault, EnumOperationTypes.Delete);
sqlCom.ExecuteNonQuery();
sqlCon.Close();
message = "Deleted";
}
catch (Exception ex)
{
message = ex.Message.Split('~')[0];
}
finally
{
sqlCom.Dispose();
}
return message;
}
public Fault Get(long id)
{
return this.ReturnObject("SELECT * FROM View_Fault WHERE Id = " + id);
}
public List<Fault> Gets()
{
return this.ReturnList("SELECT * FROM View_Fault");
}
private Fault ReturnObject(string sSQL)
{
var oFault = new Fault();
try
{
sqlCon.Open();
sqlCom = new SqlCommand(sSQL, sqlCon);
SqlDataReader reader = sqlCom.ExecuteReader();
while (reader.Read()) oFault = this.Mapping(reader);
}
catch (Exception ex)
{
oFault = new Fault() { Message = ex.Message.Split('~')[0] };
}
finally
{
sqlCom.Dispose();
sqlCon.Close();
}
return oFault;
}
private List<Fault> ReturnList(string sSQL)
{
var oFaults = new List<Fault>();
try
{
sqlCon.Open();
sqlCom = new SqlCommand(sSQL, sqlCon);
SqlDataReader reader = sqlCom.ExecuteReader();
while (reader.Read()) oFaults.Add(this.Mapping(reader));
}
catch (Exception ex)
{
oFaults = new List<Fault>();
oFaults.Add(new Fault() { Message = ex.Message.Split('~')[0] });
}
finally
{
sqlCom.Dispose();
sqlCon.Close();
}
return oFaults;
}
public SqlCommand SetParameters(SqlCommand sqlCom, Fault oFault, EnumOperationTypes operationType)
{
sqlCom.Parameters.AddWithValue("@Id", oFault.Id);
sqlCom.Parameters.AddWithValue("@Name", oFault.Name);
sqlCom.Parameters.AddWithValue("@OperationType", operationType);
return sqlCom;
}
public Fault Mapping(SqlDataReader reader)
{
return new Fault()
{
Id = (long)reader["Id"],
Name = reader["Name"].ToString()
};
}
}
public enum EnumOperationTypes
{
Save = 1,
Update = 2,
Delete = 3
}
$(document).ready(function () {
_faults = @Html.Raw(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(ViewBag.Faults));
});
$.ajax({
type: "POST",
dataType: "json",
url: "/Fault/SaveFault",
traditional: true,
data: JSON.stringify(fault),
contentType: "application/json;charset=utf-8",
success: function (data) {
var faultObj = jQuery.parseJSON(data);
if ($.trim(faultObj.Message) != "") {
DisplaySavedMessage(id, "error");
return false;
}\
},
error: function (xhr, status, error) {
alert(error);
}
});
0 Comments