Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Use of Repository in MVC

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 348
    Comment on it

    Repository in MVC project is used to store and perform the crud database operations.

     

    It is folder in which there is class file that contains definitions of functions performing the operations

     

    So we are explaining the function insert details which is used to insert user details into the database using the repository class.

     

     /*  To insert User Details into the Database */

     

    public Details InsertDetails(Details dobj)
            { 
                try
                {
       /*  To initialize the connection class*/ 
    
                    using (SqlConnection mycon = new SqlConnection("Data Source=mydemoserver;Initial Catalog=TestDb;Persist Security Info=True;User ID=sa;Password=evonadmin@123"))
                    {
    
      /*  To initialize the SqlCommand class */ 
    
                        SqlCommand cmd = new SqlCommand("procassignment", mycon);
    
                        cmd.CommandType = CommandType.StoredProcedure;
    
                        cmd.Parameters.AddWithValue("@name", dobj.Name);
                        cmd.Parameters.AddWithValue("@dob", dobj.dob);
                        cmd.Parameters.AddWithValue("@category", dobj.getcategory);
                        cmd.Parameters.AddWithValue("@hobbies", dobj.gethobbies);
    
                        cmd.Parameters.AddWithValue("@country", dobj.getcountry);
    
                        mycon.Open();
    
                        cmd.ExecuteNonQuery();
    
    
                        SqlDataAdapter adapgetdata = new SqlDataAdapter("select * from asssignment where id=(select max(id) from dbo.asssignment)", mycon);
    
    
                        DataTable dtdata = new DataTable();
    
    
                        adapgetdata.Fill(dtdata);
    
    
                        dobj.Name = dtdata.Rows[0]["name"].ToString();
    
                        dobj.dob = dtdata.Rows[0]["dob"].ToString();
    
                        dobj.getcategory = dtdata.Rows[0]["category"].ToString();
    
                        dobj.gethobbies = dtdata.Rows[0]["hobbies"].ToString();
    
                        dobj.getcountry = dtdata.Rows[0]["country"].ToString();
                    }
    
                }
    
                catch (Exception ex)
                {
    
                    throw ex;
    
                }    
    
                return dobj;
            }
    

     

     

    So in this operation the details of the user are being taken into the command parameters and the command parameter is added into the SqlCommand class from which it is passed into the stored procedure named procassignment .

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: