Archive for October 2015

Object to Json

 Convert to Json

protected void Button1_Click(object sender, EventArgs e)
        {
            Student s1 = new Student();
            s1.StudentID = TextBox1.Text;
            s1.Fname = TextBox2.Text;
            s1.Lname = TextBox3.Text;

            var json = new JavaScriptSerializer().Serialize(s1);
         
            // Write the string to a file.
            System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\Lakshika\Desktop\dd.txt");
            file.WriteLine(json);

            List student=new List();
            student.Add(s1);

            _studentOp = new StudentOperations();
            _studentOp.SaveStudent(student);

            file.Close();
        }
Sunday, October 18, 2015
Posted by sinhalamp3

MVC 3 Full Teacher

Teacher Controller

public ActionResult Index()
        {
            return View();
        }

        public ActionResult Create()
        {
            DBOperations db = new DBOperations();

            List qualification = db.getAllQualifications();
            ViewBag.QualificationList = new SelectList(qualification, "QualificationID", "QualificationType");
            List school = db.getAllSchools();
            ViewBag.SchoolList = new SelectList(school, "SchoolID", "SchoolName");
            return View();
        }

        [HttpPost]
        public ActionResult Create(Teacher teacher)
        {
            DBOperations db = new DBOperations();

            List qualification = db.getAllQualifications();
            ViewBag.QualificationList = new SelectList(qualification, "QualificationID", "QualificationType");
            List school = db.getAllSchools();
            ViewBag.SchoolList = new SelectList(school, "SchoolID", "SchoolName");
            db.insertTeacher(teacher);
            return View();

        }

        public ActionResult ViewTeacher()
        {
            DBOperations db = new DBOperations();
            List teachers = db.getAllTeachers();
            return View(teachers.AsEnumerable());
        }

        //public JsonResult View(int id)
        //{
        //    DBOperations db = new DBOperations();
        //    return Json(db.getTeacherById(id), JsonRequestBehavior.AllowGet);
        //}

        public ActionResult ViewTeacherById(int id)
        {
            DBOperations db = new DBOperations();
            Teacher teacher = db.getTeacherById(id);
            List school = db.getAllSchools();
            foreach (var items in school)
            {
                if (items.SchoolID == teacher.SchoolID)
                {
                    ViewData["SchoolName"] = items.SchoolName;
                }
            }
            List qualifications = db.getAllQualifications();
            foreach (var items in qualifications)
            {
                if (items.QualificationID == teacher.QualificationID)
                {
                    ViewData["Type"] = items.QualificationType;
                }
            }
            return View(teacher);
        }


        public ActionResult UpdateTeacher(int id)
        {

            DBOperations db = new DBOperations();
            List schools = db.getAllSchools();
            ViewBag.SchoolList = new SelectList(schools, "SchoolID", "SchoolName");
            List qualifications = db.getAllQualifications();
            ViewBag.QualificationList = new SelectList(qualifications, "QualificationID", "QualificationType");
            Teacher model = db.getTeacherById(id);

            return View(model);

        }

        [HttpPost]
        public ActionResult UpdateTeacher(Teacher teacher)
        {
            DBOperations db = new DBOperations();
            List schools = db.getAllSchools();
            ViewBag.SchoolList = new SelectList(schools, "SchoolID", "SchoolName");
            List qualifications = db.getAllQualifications();
            ViewBag.QualificationList = new SelectList(qualifications, "QualificationID", "QualificationType");
           // Teacher model = db.getTeacherById(id);

            db.UpdateTeacher(teacher);

            return View("ViewTeacher");

        }

        [HttpDelete]
        public ActionResult ViewTeacher(int id)
        {
            DBOperations db = new DBOperations();
            db.DeleteTeacher(id);
            return View("ViewTeacher");
        }
-------------------------------------------------------------------------------------

DB Operations Class

public void insertTeacher(Teacher teacher)
        {
            SqlConnection conn = new SqlConnection(Connection.ConnectionString);
            conn.Open();
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = "insert into Teacher values ('"+teacher.TeacherName+"','"+teacher.TeacherNIC+"','"+teacher.TeacherAddress+"',"+teacher.QualificationID+","+teacher.SchoolID+")";
            cmd.ExecuteNonQuery();
            conn.Close();
        }

        public List getAllQualifications()
        {
            SqlConnection conn = new SqlConnection(Connection.ConnectionString);
            conn.Open();
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = "select * from Qualification";
            SqlDataReader dr = cmd.ExecuteReader();
            List qualifications = new List();
            while (dr.Read())
            {
                Qualification qualification = new Qualification();
                qualification.QualificationID = dr.GetInt32(0);
                qualification.QualificationType = dr.GetString(1);

                qualifications.Add(qualification);
            }

            return qualifications;
        }

        public List getAllSchools()
        {
            SqlConnection conn = new SqlConnection(Connection.ConnectionString);
            conn.Open();
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = "select * from School";
            SqlDataReader dr = cmd.ExecuteReader();
            List schools = new List();
            while (dr.Read())
            {
                School school = new School();
                school.SchoolID = dr.GetInt32(0);
                school.SchoolName = dr.GetString(1);

                schools.Add(school);
            }

            return schools;
        }

        public List getAllTeachers()
        {
            List teachers = new List();
            SqlConnection conn = new SqlConnection(Connection.ConnectionString);
            conn.Open();
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = "SELECT * FROM Teacher";
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                Teacher teacher = new Teacher();
                teacher.TeacherID = dr.GetInt32(0);
                teacher.TeacherName = dr.GetString(1);
                teacher.TeacherNIC = dr.GetString(2);
                teacher.TeacherAddress = dr.GetString(3);
                teacher.QualificationID = dr.GetInt32(4);
                teacher.SchoolID = dr.GetInt32(5);
                
                teachers.Add(teacher);
            }
            return teachers;

        }

        public Teacher getTeacherById(int teacher_id)
        {
            Teacher teacher = new Teacher();
            SqlConnection conn = new SqlConnection(Connection.ConnectionString);
            conn.Open();
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = "select * from Teacher where TeacherID="+teacher_id+"";
            SqlDataReader dr = cmd.ExecuteReader();

            while (dr.Read())
            {
                teacher.TeacherID = dr.GetInt32(0);
                teacher.TeacherName = dr.GetString(1);
                teacher.TeacherNIC = dr.GetString(2);
                teacher.TeacherAddress = dr.GetString(3);
                teacher.QualificationID = dr.GetInt32(4);
                teacher.SchoolID = dr.GetInt32(5);
            }

            return teacher;
        }

        public void UpdateTeacher(Teacher teacher)
        {
            int teacher_id = teacher.TeacherID;
            string teacher_name = teacher.TeacherName;
            string teacher_address = teacher.TeacherAddress;
            string teacher_nic = teacher.TeacherNIC;
            int quali_id = teacher.QualificationID;
            int school_id = teacher.SchoolID;

            SqlConnection conn = new SqlConnection(Connection.ConnectionString);
            conn.Open();
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = "Update Teacher set TeacherName='" + teacher_name + "',TeacherNIC='" + teacher_nic + "',TeacherAddress='" + teacher_address + "',QualificationID=" + quali_id + ",SchoolID=" + school_id + " where TeacherID=" + teacher_id + "";
            cmd.ExecuteNonQuery();
            conn.Close();
        }

        public void DeleteTeacher(int id)
        {
            SqlConnection conn = new SqlConnection(Connection.ConnectionString);
            conn.Open();
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = "delete from Teacher where TeacherID="+id+"";
            cmd.ExecuteNonQuery();
            conn.Close();
        }

---------------------------------------------------------------------------------------------

DBConnection Class

public class Connection
    {
        public static string ConnectionString
        {
            get { return "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=SINGADB;Data Source=Lakshika-PC\\SQLEXPRESS"; }
        }
     
 
    }
Saturday, October 17, 2015
Posted by sinhalamp3

MVC 2 Create, Search

Create Student

Controller

DekmaEntities _dekmaEntities;

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(Student student)
        {
            using (_dekmaEntities = new DekmaEntities())
            {
                _dekmaEntities.Students.AddObject(student);
                _dekmaEntities.SaveChanges();

                return View("Index");
            }

        }

public ActionResult ViewAll()
        {
            using (_dekmaEntities = new DekmaEntities())
            {
                var stu = _dekmaEntities.Students.Take(10).ToArray();
                List studentList = new List();

                for (int i = 0; i < stu.Length; i++)
                {
                    Student student = new Student();
                    student.Student_ID = stu[i].Student_ID;
                    student.Name = stu[i].Name;

                    studentList.Add(student);
                }

                return View(studentList);
            }
        }
---------------------------------------------------------------------------------------------

Views

---
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>


Create




   

Create



    <% using (Html.BeginForm()) {%>
        <%: Html.ValidationSummary(true) %>

       

            Fields
           
           

                <%: Html.LabelFor(model => model.Student_ID) %>
           
           

                <%: Html.TextBoxFor(model => model.Student_ID) %>
                <%: Html.ValidationMessageFor(model => model.Student_ID) %>
           
           
           

                <%: Html.LabelFor(model => model.Name) %>
           
           

                <%: Html.TextBoxFor(model => model.Name) %>
                <%: Html.ValidationMessageFor(model => model.Name) %>
           
           
           

                <%: Html.LabelFor(model => model.House) %>
           
           

                <%: Html.TextBoxFor(model => model.House) %>
                <%: Html.ValidationMessageFor(model => model.House) %>
           
           
           

                <%: Html.LabelFor(model => model.street) %>
           
           

                <%: Html.TextBoxFor(model => model.street) %>
                <%: Html.ValidationMessageFor(model => model.street) %>
           
           
           

                <%: Html.LabelFor(model => model.City) %>
           
           

                <%: Html.TextBoxFor(model => model.City) %>
                <%: Html.ValidationMessageFor(model => model.City) %>
           
           
           

                <%: Html.LabelFor(model => model.Gender) %>
           
           

                <%: Html.TextBoxFor(model => model.Gender) %>
                <%: Html.ValidationMessageFor(model => model.Gender) %>
           
           
           

                <%: Html.LabelFor(model => model.Birth_day) %>
           
           

                <%: Html.TextBoxFor(model => model.Birth_day) %>
                <%: Html.ValidationMessageFor(model => model.Birth_day) %>
           
           
           

                <%: Html.LabelFor(model => model.NIC) %>
           
           

                <%: Html.TextBoxFor(model => model.NIC) %>
                <%: Html.ValidationMessageFor(model => model.NIC) %>
           
           
           

                <%: Html.LabelFor(model => model.Tel_land) %>
           
           

                <%: Html.TextBoxFor(model => model.Tel_land) %>
                <%: Html.ValidationMessageFor(model => model.Tel_land) %>
           
           
           

                <%: Html.LabelFor(model => model.Tel_mobile) %>
           
           

                <%: Html.TextBoxFor(model => model.Tel_mobile) %>
                <%: Html.ValidationMessageFor(model => model.Tel_mobile) %>
           
           
           

                <%: Html.LabelFor(model => model.Stream) %>
           
           

                <%: Html.TextBoxFor(model => model.Stream) %>
                <%: Html.ValidationMessageFor(model => model.Stream) %>
           
           
           

                <%: Html.LabelFor(model => model.Ex_Center) %>
           
           

                <%: Html.TextBoxFor(model => model.Ex_Center) %>
                <%: Html.ValidationMessageFor(model => model.Ex_Center) %>
           
           
           

                <%: Html.LabelFor(model => model.T_R) %>
           
           

                <%: Html.TextBoxFor(model => model.T_R) %>
                <%: Html.ValidationMessageFor(model => model.T_R) %>
           
           
           

                <%: Html.LabelFor(model => model.AL_Year) %>
           
           

                <%: Html.TextBoxFor(model => model.AL_Year) %>
                <%: Html.ValidationMessageFor(model => model.AL_Year) %>
           
           
           

                <%: Html.LabelFor(model => model.School_Name) %>
           
           

                <%: Html.TextBoxFor(model => model.School_Name) %>
                <%: Html.ValidationMessageFor(model => model.School_Name) %>
           
           
           

                <%: Html.LabelFor(model => model.Guardian_Name) %>
           
           

                <%: Html.TextBoxFor(model => model.Guardian_Name) %>
                <%: Html.ValidationMessageFor(model => model.Guardian_Name) %>
           
           
           

                <%: Html.LabelFor(model => model.Guardian_TP) %>
           
           

                <%: Html.TextBoxFor(model => model.Guardian_TP) %>
                <%: Html.ValidationMessageFor(model => model.Guardian_TP) %>
           
           
           

                <%: Html.LabelFor(model => model.Guardian_Occupation) %>
           
           

                <%: Html.TextBoxFor(model => model.Guardian_Occupation) %>
                <%: Html.ValidationMessageFor(model => model.Guardian_Occupation) %>
           
           
           
               
           
       

    <% } %>

   

        <%: Html.ActionLink("Back to List", "Index") %>
   



Sunday, October 11, 2015
Posted by sinhalamp3

Popular Post

Blogger templates

lakshika345@gmail.com. Powered by Blogger.

- Copyright © mp3s for you -Metrominimalist- Powered by Blogger - Designed by Johanes Djogan -