Hi
Is there any Generic way that only a sibgle function be created instead of 3 separate methods to be created
public string Add(Location objLoc)
{
try
{
using (SqlConnection con = new SqlConnection(Common.Con))
{
con.Open();
SqlCommand cmd = new SqlCommand("sp_Location", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Id", objLoc.Id);
cmd.Parameters.AddWithValue("@Description", objLoc.Description);
cmd.Parameters.AddWithValue("@Action", "C");
cmd.ExecuteNonQuery();
}
}
catch (Exception ex)
{
ExceptionLogging.SendExcepToDB(ex);
output = ex.Message;
//ViewBag.ErrorMessage = ex.Message.ToString();
//return -1;
}
return output;
}
public string Update(Location objLoc , string hfId)
{
try
{
using (SqlConnection con = new SqlConnection(Common.Con))
{
con.Open();
SqlCommand cmd = new SqlCommand("sp_Location", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Id", objLoc.Id);
cmd.Parameters.AddWithValue("@Description", objLoc.Description);
cmd.Parameters.AddWithValue("@Action", "U");
cmd.ExecuteNonQuery();
}
}
catch (Exception ex)
{
ExceptionLogging.SendExcepToDB(ex);
output = ex.Message;
}
return output;
}
[HttpPost]
public string Delete(string Id)
{
try
{
using (SqlConnection con = new SqlConnection(Common.Con))
{
con.Open();
SqlCommand cmd = new SqlCommand("sp_Location", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Id", Id);
cmd.Parameters.AddWithValue("@Action", "D");
cmd.ExecuteNonQuery();
}
}
catch (Exception ex)
{
ExceptionLogging.SendExcepToDB(ex);
output = ex.Message;
}
return output;
}
Thanks
Ramco RamcoPosted Jul 9, 2021, 3:50 AM
Hi Sachin
How the below code can be done thru
con.Open();
SqlCommand cmd = new SqlCommand("sp_Location", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Id", objLoc.Id);
cmd.Parameters.AddWithValue("@Description", objLoc.Description);
cmd.Parameters.AddWithValue("@Action", "U");
cmd.ExecuteNonQuery();
Something using like below :
public void dml(SqlCommand cmd, SqlConnection con)
{
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
public DataSet Getrecord(SqlCommand cmd)
{
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
// For All DML operations
public void dmlvalue(string proc, dynamic[] col)
{
SqlCommand com = new SqlCommand("Sp_getProcParam", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@Proc_name", proc);
DataSet ds = Getrecord(com);
int i = 0;
SqlCommand cmd1 = new SqlCommand(proc, con);
cmd1.CommandType = CommandType.StoredProcedure;
foreach (DataRow dr in ds.Tables[0].Rows)
{
cmd1.Parameters.AddWithValue(dr[0].ToString(), col[i]);
i++;
}
dml(cmd1, con);
}
Thanks
Sachin SinghPosted Jul 8, 2021, 5:16 PM