Dhimesh Parmar

Dhimesh Parmar

  • NA
  • 126
  • 14.4k

I need suggestion optimised code for this program?

Feb 27 2018 11:44 PM
this is my code but i need more optimised code for this task
any budy can help for this task. 
 
  1. //This is Dal File
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Data.SqlClient;  
  5. using System.Linq;  
  6. using System.Web;  
  7. using HTML.Common;  
  8. using HTML.BE;  
  9. using System.Data;  
  10.   
  11. namespace HTML.DAL  
  12. {  
  13.     public class clsUserDal:clsUserCommon  
  14.     {  
  15.   
  16.         public int AddUser(clsUserBE objUserBE)  
  17.         {  
  18.             try  
  19.             {  
  20.   
  21.                 SqlCommand cmd = new SqlCommand("userIUD");  
  22.                 cmd.CommandType = CommandType.StoredProcedure;  
  23.   
  24.                 cmd.Parameters.AddWithValue("Action""INSERT");  
  25.                  
  26.                 // cmd.Parameters.AddWithValue("userid", objUserBE.userId);  
  27.                 cmd.Parameters.AddWithValue("name", objUserBE.name);  
  28.                 cmd.Parameters.AddWithValue("emailid", objUserBE.emailid);  
  29.                 cmd.Parameters.AddWithValue("mobilenumber", objUserBE.mobilenumber);  
  30.                 cmd.Parameters.AddWithValue("status", objUserBE.status);  
  31.                 cmd.Parameters.AddWithValue("createOn", objUserBE.createdOn);  
  32.                 cmd.Parameters.AddWithValue("modifyOn", objUserBE.modifiedOn);  
  33.                 return ExecuteNonQueryByCommand(cmd);  
  34.   
  35.             }  
  36.             catch (Exception ex)  
  37.             {  
  38.                 HttpContext.Current.Trace.Warn("Error = " + ex.Message);  
  39.                 HttpContext.Current.Trace.Warn("StackTrace = " + ex.StackTrace);  
  40.             }  
  41.   
  42.             return 0;  
  43.         }  
  44.         public int UpdateUser(clsUserBE objUserBE)  
  45.         {  
  46.             try  
  47.             {  
  48.   
  49.                 SqlCommand cmd = new SqlCommand("userIUD");  
  50.                 cmd.CommandType = CommandType.StoredProcedure;  
  51.                 cmd.Parameters.AddWithValue("Action""UPDATE");  
  52.   
  53.                cmd.Parameters.AddWithValue("userId", objUserBE.userId);  
  54.   
  55.                 cmd.Parameters.AddWithValue("name", objUserBE.name);  
  56.                 cmd.Parameters.AddWithValue("emailid", objUserBE.emailid);  
  57.                 cmd.Parameters.AddWithValue("mobilenumber", objUserBE.mobilenumber);  
  58.                 cmd.Parameters.AddWithValue("modifyOn", objUserBE.modifiedOn);  
  59.                       
  60.                 return ExecuteNonQueryByCommand(cmd);  
  61.   
  62.             }  
  63.             catch (Exception ex)  
  64.             {  
  65.                 HttpContext.Current.Trace.Warn("Error = " + ex.Message);  
  66.                 HttpContext.Current.Trace.Warn("StackTrace = " + ex.StackTrace);  
  67.             }  
  68.   
  69.             return 0;  
  70.         }  
  71.         public int DeleteClient(clsUserBE objUserBE)  
  72.         {  
  73.             try  
  74.             {  
  75.   
  76.                 SqlCommand cmd = new SqlCommand("userIUD");  
  77.                 cmd.CommandType = CommandType.StoredProcedure;  
  78.                 cmd.Parameters.AddWithValue("Action""DELETE");  
  79.                 cmd.Parameters.AddWithValue("userId", objUserBE.userId);  
  80.                 return ExecuteNonQueryByCommand(cmd);  
  81.   
  82.             }  
  83.             catch (Exception ex)  
  84.             {  
  85.                 HttpContext.Current.Trace.Warn("Error = " + ex.Message);  
  86.                 HttpContext.Current.Trace.Warn("StackTrace = " + ex.StackTrace);  
  87.             }  
  88.   
  89.             return 0;  
  90.         }  
  91.         public int selectUser(clsUserBE objUserBE) {  
  92.             try  
  93.             {  
  94.   
  95.                 SqlCommand cmd = new SqlCommand("userIUD");  
  96.                 cmd.CommandType = CommandType.StoredProcedure;  
  97.                 cmd.Parameters.AddWithValue("Action""SELECT");  
  98.                 cmd.Parameters.AddWithValue("userId", objUserBE.userId);  
  99.                 return ExecuteNonQueryByCommand(cmd);  
  100.   
  101.             }  
  102.             catch (Exception ex)  
  103.             {  
  104.                 HttpContext.Current.Trace.Warn("Error = " + ex.Message);  
  105.                 HttpContext.Current.Trace.Warn("StackTrace = " + ex.StackTrace);  
  106.             }  
  107.   
  108.             return 0;  
  109.         }  
  110.         public clsUserBE getUserByid(int id)  
  111.         {  
  112.             clsUserBE c = new clsUserBE();  
  113.             SqlCommand cmd = new SqlCommand();  
  114.             cmd.CommandText = "select * from Users where userId= " + id;  
  115.             DataSet ds = ExecuteSelect(cmd);  
  116.   
  117.   
  118.             if (ds.Tables[0].Rows.Count > 0)  
  119.             {  
  120.                 c.userId = (int)ds.Tables[0].Rows[0]["userId"];  
  121.                 c.name = Convert.ToString(ds.Tables[0].Rows[0]["name"]);  
  122.                 c.emailid = Convert.ToString(ds.Tables[0].Rows[0]["emailid"]);  
  123.                 c.mobilenumber= Convert.ToString(ds.Tables[0].Rows[0]["mobilenumber"]);  
  124.             }  
  125.   
  126.             return c;  
  127.   
  128.         }  
  129.   
  130.           
  131.         public int userStatusA(clsUserBE c)  
  132.         {  
  133.             try  
  134.             {  
  135.   
  136.                 SqlCommand cmd = new SqlCommand("useAID");  
  137.                 cmd.CommandType = CommandType.StoredProcedure;  
  138.                 cmd.Parameters.AddWithValue("Action""ActiveUser");  
  139.   
  140.                 cmd.Parameters.AddWithValue("userId", c.userId);  
  141.                 cmd.Parameters.AddWithValue("status", c.status);  
  142.                 cmd.Parameters.AddWithValue("modifyOn", c.modifiedOn);  
  143.   
  144.                 return ExecuteNonQueryByCommand(cmd);  
  145.   
  146.             }  
  147.             catch (Exception ex)  
  148.             {  
  149.                 HttpContext.Current.Trace.Warn("Error = " + ex.Message);  
  150.                 HttpContext.Current.Trace.Warn("StackTrace = " + ex.StackTrace);  
  151.             }  
  152.   
  153.             return 0;  
  154.                          
  155.         }  
  156.   
  157.   
  158.         public int userStatusI(clsUserBE c)  
  159.         {  
  160.             try  
  161.             {  
  162.   
  163.                 SqlCommand cmd = new SqlCommand("useAID");  
  164.                 cmd.CommandType = CommandType.StoredProcedure;  
  165.                 cmd.Parameters.AddWithValue("Action""DeactiveUser");  
  166.   
  167.                 cmd.Parameters.AddWithValue("userId", c.userId);  
  168.                 cmd.Parameters.AddWithValue("status", c.status);  
  169.                 cmd.Parameters.AddWithValue("modifyOn", c.modifiedOn);  
  170.   
  171.                 return ExecuteNonQueryByCommand(cmd);  
  172.   
  173.             }  
  174.             catch (Exception ex)  
  175.             {  
  176.                 HttpContext.Current.Trace.Warn("Error = " + ex.Message);  
  177.                 HttpContext.Current.Trace.Warn("StackTrace = " + ex.StackTrace);  
  178.             }  
  179.   
  180.             return 0;  
  181.   
  182.         }  
  183.   
  184.         public int userStatusD(clsUserBE c)  
  185.         {  
  186.             try  
  187.             {  
  188.   
  189.                 SqlCommand cmd = new SqlCommand("useAID");  
  190.                 cmd.CommandType = CommandType.StoredProcedure;  
  191.                 cmd.Parameters.AddWithValue("Action""DeleteUser");  
  192.   
  193.                 cmd.Parameters.AddWithValue("userId", c.userId);  
  194.                 cmd.Parameters.AddWithValue("status", c.status);  
  195.                 cmd.Parameters.AddWithValue("modifyOn", c.modifiedOn);  
  196.   
  197.                 return ExecuteNonQueryByCommand(cmd);  
  198.   
  199.                   
  200.   
  201.             }  
  202.             catch (Exception ex)  
  203.             {  
  204.                 HttpContext.Current.Trace.Warn("Error = " + ex.Message);  
  205.                 HttpContext.Current.Trace.Warn("StackTrace = " + ex.StackTrace);  
  206.             }  
  207.   
  208.             return 0;  
  209.   
  210.         }  
  211.   
  212.   
  213.         //public clsUserBE deleteUser(int id)  
  214.         //{  
  215.         //    clsUserBE c = new clsUserBE();  
  216.         //    SqlCommand cmd = new SqlCommand();  
  217.         //    cmd.CommandText = "delete from Users where userId= " + id;  
  218.         //    DataSet ds = ExecuteSelect(cmd);  
  219.         //    return c;  
  220.         //}  
  221.   
  222.   
  223.         public DataSet getdata()  
  224.         {  
  225.             SqlCommand cmd = new SqlCommand();  
  226.             cmd.CommandText = "  select * from Users where status!=2";  
  227.             return ExecuteSelect(cmd);  
  228.         }  
  229.         public clsUserDal()  
  230.         {  
  231.             //  
  232.             // TODO: Add constructor logic here  
  233.             //  
  234.         }  
  235.     }  
  236.   
  237. }  
  1. //this is common file  
  2.   
  3. using System;  
  4. using System.Collections.Generic;  
  5. using System.Configuration;  
  6. using System.Data;  
  7. using System.Data.SqlClient;  
  8. using System.Linq;  
  9. using System.Web;  
  10.   
  11.   
  12. namespace HTML.Common  
  13. {  
  14.     public class clsUserCommon  
  15.     {  
  16.   
  17.         public string ConnectionString = ConfigurationManager.ConnectionStrings["MyDB"].ToString();  
  18.         public int ExecuteNonQueryByCommand(SqlCommand cmd)  
  19.         {  
  20.             SqlConnection con = new SqlConnection(ConnectionString);  
  21.             con.Open();  
  22.             cmd.Connection = con;  
  23.             int intRetval = cmd.ExecuteNonQuery();  
  24.             con.Close();  
  25.             return intRetval;  
  26.         }  
  27.   
  28.         public DataSet ExecuteSelect(SqlCommand cmd)  
  29.         {  
  30.             SqlConnection con = new SqlConnection(ConnectionString);  
  31.             con.Open();  
  32.             cmd.Connection = con;  
  33.             SqlDataAdapter sdp = new SqlDataAdapter(cmd);  
  34.             DataSet dt = new DataSet();  
  35.             sdp.Fill(dt);  
  36.             con.Close();  
  37.             return dt;  
  38.         }  
  39.           
  40.     }  
  41. }  
  1. this is aspx.cs file  
  2.   
  3. using System;  
  4. using System.Collections.Generic;  
  5. using System.Linq;  
  6. using System.Web;  
  7. using System.Web.UI;  
  8. using System.Web.UI.WebControls;  
  9. using HTML.DAL;  
  10. using HTML.BE;  
  11. using System.Data;  
  12. using System.Text;  
  13. using System.Data.SqlClient;  
  14.   
  15. public partial class html : System.Web.UI.Page  
  16. {  
  17.     clsUserBE userBE=new clsUserBE();  
  18.     clsUserDal userDAL=new clsUserDal();  
  19.   
  20.     public DataSet ds = null;  
  21.   
  22.     protected void Page_Load(object sender, EventArgs e)  
  23.     {  
  24.           
  25.   
  26.         ds = userDAL.getdata();  
  27.   
  28.         //if (Request.QueryString["userId"] != "" && Convert.ToInt32(Request.QueryString["userId"]) > 0)  
  29.         //{  
  30.         //    int x = Convert.ToInt32(Request.QueryString["userId"]);  
  31.         //    userBal = userDAL.deleteUser(x);  
  32.   
  33.         //}  
  34.   
  35.         if (Request.QueryString["userId"] != "" && Convert.ToInt32(Request.QueryString["userId"]) > 0 && Request.QueryString["action"] == "ActiveUser")  
  36.         {  
  37.             // int x = Convert.ToInt32(Request.QueryString["userId"]);  
  38.   
  39.             userBE.userId = Convert.ToInt32(Request.QueryString["userId"]);  
  40.             userDAL.UpdateUser(userBE);  
  41.             userBE.status = 1;  
  42.             userBE.modifiedOn = DateTime.Now;  
  43.             userDAL.userStatusA(userBE);  
  44.             Response.Redirect("html.aspx");  
  45.             //userBal = userDAL.userStatusA(x);  
  46.   
  47.         }  
  48.         else if (Request.QueryString["userId"] != "" && Convert.ToInt32(Request.QueryString["userId"]) > 0 && Request.QueryString["action"] == "DeactiveUser")  
  49.         {  
  50.   
  51.             userBE.userId = Convert.ToInt32(Request.QueryString["userId"]);              
  52.             userDAL.UpdateUser(userBE);  
  53.             userBE.status = 0;  
  54.             userBE.modifiedOn = DateTime.Now;  
  55.             userDAL.userStatusI(userBE);  
  56.             Response.Redirect("html.aspx");  
  57.               
  58.         }  
  59.         else if (Request.QueryString["userId"] != "" && Convert.ToInt32(Request.QueryString["userId"]) > 0 && Request.QueryString["action"] == "DeleteUser")  
  60.         {  
  61.             userBE.userId = Convert.ToInt32(Request.QueryString["userId"]);  
  62.             userDAL.UpdateUser(userBE);  
  63.             userBE.status = 2;  
  64.             userBE.modifiedOn = DateTime.Now;  
  65.             userDAL.userStatusD(userBE);  
  66.             Response.Redirect("html.aspx");  
  67.         }  
  68.     }  
  69. }  

Answers (1)