Saturday, 21 January 2017

How to Create Only One function for all database operations in Asp.net - MVC

Video Tutorial:

Download Full Source Code:

Download here



Introduction:
 Here I will explain how to create only one function for all database operation in Asp.net MVC. It reduce your time to for creating separate function for creating individual function for each operations.
Description:
So now we start.
Step1:  In first step we will create table and stored procedure in database
CREATE TABLE [dbo].[tbl_registration] (
    [Sr_no]    INT            IDENTITY (1, 1) NOT NULL,
    [Email]    NVARCHAR (100) NULL,
    [Password] NVARCHAR (MAX) NULL,
    [Name]     VARCHAR (MAX)  NULL,
    [Address]  NVARCHAR (MAX) NULL,
    [City]     NVARCHAR (MAX) NULL,
    CONSTRAINT [PK_tbl_registration] PRIMARY KEY CLUSTERED ([Sr_no] ASC)
);
Create proc Sp_register
 @Email    NVARCHAR (100) ,
    @Password NVARCHAR (MAX) ,
    @Name     VARCHAR (MAX)  ,
    @Address  NVARCHAR (MAX) ,
    @City     NVARCHAR (MAX)
     as
     begin
     insert into tbl_registration values(
      @Email     ,
    @Password  ,
    @Name      ,
    @Address   ,
    @City     
     )
     end

Step2: In this step we create store procedure for retrieve total parameter from given stored procedure
CREATE proc Sp_getProcParam
@Proc_name nvarchar(50)
as
begin
select Parameter_Name from information_schema.parameters
where specific_name=@Proc_name
end

Step3: Create New folder for Database_Access_Layer and add class operation.cs class and write fallowing code

  public class operation
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);

        public void dml(SqlCommand com,SqlConnection con)
        {
            con.Open();
            com.ExecuteNonQuery();
            con.Close();
        }
        public DataSet Getrecord(SqlCommand com)
        {
            SqlDataAdapter da = new SqlDataAdapter(com);
            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 com1 = new SqlCommand(proc, con);
            com1.CommandType = CommandType.StoredProcedure;
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                com1.Parameters.AddWithValue(dr[0].ToString(), col[i]);
                i++;
            }
            dml(com1, con);
        }
        // For get record from database
        public DataSet Getvalue(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 com1 = new SqlCommand(proc, con);
            com1.CommandType = CommandType.StoredProcedure;
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                com1.Parameters.AddWithValue(dr[0].ToString(), col[i]);
                i++;
            }
            DataSet ds2 = Getrecord(com1);
            return ds2;
        }
    }

Step4: Write down fallowing html under Index.cshtml View

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div>
        <form method="post">
            <table>
                <tr>
                    <td><input placeholder="Email" name="email" /></td>
                </tr>
                <tr>
                    <td><input placeholder="Password" name="Password" /></td>
                </tr>
                <tr>
                    <td><input placeholder="Name" name="Name" /></td>
                </tr>
                <tr>
                    <td><input placeholder="Address" name="Address" /></td>
                </tr>
                <tr>
                    <td><input placeholder="City" name="City" /></td>
                </tr>
                <tr>
                    <td><input type="submit" value="Submit" /></td>
                </tr>
            </table>
        </form>
    </div>
    @{
        if (TempData["msg"] != null)
        {
            <script type="text/javascript">
                alert('@TempData["msg"]');
            </script>
        }
    }
</body>

</html>


Step5: Go to HomeController.cs class and write fallowing code for accessing data and send stored procedure name and parameters
public class HomeController : Controller
    {
        //
        // GET: /Home/
        operation dbop = new operation();
        dynamic[] col;
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Index(FormCollection fc)
        {
            col = new dynamic[] { fc["email"], fc["Password"], fc["Name"], fc["Address"], fc["City"] };
            dbop.dmlvalue("Sp_register", col);
            TempData["msg"] = "Submitted Succssfully";
            return View();
        }

    }

Step5: Now run project and you inserted your data in database

Thanks…

No comments:

Post a Comment

Ads Inside Post