.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Visual Studio >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Edit the data in the table

Posted By:Daniel Bhagathsingh       Posted Date: December 18, 2013    Points: 40    Category: Visual Studio    URL: http://www.dotnetspark.com  

you edit any data in the table you can use this code
 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BusinessLayer;

namespace MVCDemo14.Controllers
{
    public class StudentController : Controller
    {
        public ActionResult Index()
        {
            StudentBusinessLayer studentBusinessLayer = new StudentBusinessLayer();
            List<Student> students = studentBusinessLayer.Students.ToList();
            return View(students);
        }
        [HttpGet]
        public ActionResult Create()
        {
            return View();

        }
        [HttpGet]
        public ActionResult Edit(int id)
        {
            StudentBusinessLayer studentBusinessLayer = new StudentBusinessLayer();
           Student student = studentBusinessLayer.Students.Single(stu => stu.SId == id);
           return View(student);

        }
        [HttpPost]
        public ActionResult Create(FormCollection formCollection)
        {
            Student student = new Student();
            student.SId = Convert.ToInt32(formCollection["SId"]);
            student.Name = formCollection["Name"];
            student.Age = Convert.ToInt32(formCollection["Age"]);
            student.Gender = formCollection["Gender"];
            student.Course = formCollection["Course"];
            student.Departement = formCollection["Departement"];
            student.Place = formCollection["Place"];

            StudentBusinessLayer studentBusinessLayer = new StudentBusinessLayer();
            studentBusinessLayer.AddEmployee(student);

            return RedirectToAction("Index");
         
        }

     
    }
}
*************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;


namespace BusinessLayer
{
    public class StudentBusinessLayer
    {
        public IEnumerable<Student> Students
        {
            get {
                string connectionString =
                    ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

                List<Student> students = new List<Student>();

                using (SqlConnection con = new SqlConnection(connectionString))
                {
                    SqlCommand cmd = new SqlCommand("spGetAllStudents", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    con.Open();
                    SqlDataReader rdr = cmd.ExecuteReader();
                    while (rdr.Read())
                    {
                        Student student = new Student();
                        student.SId = Convert.ToInt32(rdr["SId"]);
                        student.Name = rdr["Name"].ToString();
                        student.Age = Convert.ToInt32(rdr["Age"]);
                        student.Gender = rdr["Gender"].ToString();
                        student.Course = rdr["Course"].ToString();
                        student.Departement = rdr["Departement"].ToString();
                        student.Place = rdr["Place"].ToString();
                       

                        students.Add(student);
                    }
                }

                return students;
            }
           
        }
         
                  public void AddEmployee(Student student)

            {
                string connectionString = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
                using (SqlConnection con = new SqlConnection(connectionString))
                {
                    SqlCommand cmd = new SqlCommand("spAddStudent", con);
                    cmd.CommandType = CommandType.StoredProcedure;
SqlParameter paramSId = new SqlParameter();
paramSId.ParameterName="@SId";
paramSId.Value=student.SId;
cmd.Parameters.Add(paramSId);
SqlParameter paramName = new SqlParameter();
paramName.ParameterName="@Name";
paramName.Value=student.Name;
                    cmd.Parameters.Add(paramName);
SqlParameter paramAge = new SqlParameter();
paramAge.ParameterName="@Age";
paramAge.Value=student.Age;
cmd.Parameters.Add(paramAge);
SqlParameter paramGender = new SqlParameter();
paramGender.ParameterName="@Gender";
paramGender.Value=student.Gender;
cmd.Parameters.Add(paramGender);
SqlParameter paramCourse = new SqlParameter();
paramCourse.ParameterName="@Course";
paramCourse.Value=student.Course;
cmd.Parameters.Add(paramCourse);
SqlParameter paramDepartement = new SqlParameter();
paramDepartement.ParameterName="@Departement";
paramDepartement.Value=student.Departement;
cmd.Parameters.Add(paramDepartement);
SqlParameter paramPlace = new SqlParameter();
paramPlace.ParameterName="@Place";
paramPlace.Value=student.Place;
cmd.Parameters.Add(paramPlace);
                    
con.Open();
                    cmd.ExecuteNonQuery();
                  }

                  }
public  SqlParameter paramname { get; set; }}
}
********************************************************

@model BusinessLayer.Student

@{
    ViewBag.Title = "Create";
}
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />

<div style="font-family: Arial">

    <h2>Create</h2>

    @using (Html.BeginForm()) {
        @Html.ValidationSummary(true)

        <fieldset>
            <legend>Student</legend>

            <div class="editor-label">
                @Html.LabelFor(model => model.SId)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.SId)
                @Html.ValidationMessageFor(model => model.SId)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.Name)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Name)
                @Html.ValidationMessageFor(model => model.Name)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.Age)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Age)
                @Html.ValidationMessageFor(model => model.Age)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.Gender)
            </div>
            <div class="editor-field">
                @Html.DropDownList("Gender",new List<SelectListItem>{
            new SelectListItem{Text="Male",Value="Male"},
            new SelectListItem{Text="Femaale",Value="Female"}
       
       },"Select Gender")
                @Html.ValidationMessageFor(model => model.Gender)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.Course)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Course)
                @Html.ValidationMessageFor(model => model.Course)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.Departement)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Departement)
                @Html.ValidationMessageFor(model => model.Departement)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.Place)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Place)
                @Html.ValidationMessageFor(model => model.Place)
            </div>

            <p>
                <input type="submit" value="Create" />
            </p>
        </fieldset>

}
    <div>
        @Html.ActionLink("Back to List", "Index")
    </div>

</div>
 



     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend