Click here to Skip to main content
15,890,825 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
web.config

<connectionStrings>
<add name="connect" connectionString="Data Source=TC014PL28\TCDBS;Initial Catalog=Insertdemo;Persist Security Info=True;User ID=sa;Password=satest@123" providerName="System.Data.SqlClient" />
</connectionStrings>

BAL.CS

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace Insertdemo.Models
{
public class Bal
{
public int uid { get; set; }
[Required]
public string uname { get; set; }
[Required]
[StringLength(6)]
public string ugender { get; set; }
[Required]
public string city { get; set; }
[Required]
public string img { get; set; }
//public List<Bal> userlist { get; set; }

}
}

Dal.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Insertdemo.Models;
using System.IO;

namespace Insertdemo.Models
{

public class Dal
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connect"].ConnectionString.ToString());
SqlCommand cmd;
//SqlDataAdapter sda;
//DataSet ds;
public void Insert(Bal ba)
{
con.Open();
cmd = new SqlCommand("insert_data", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@uname", ba.uname);
cmd.Parameters.AddWithValue("@ugender", ba.ugender);
cmd.Parameters.AddWithValue("@city", ba.city);
cmd.Parameters.AddWithValue("@img", ba.img);
cmd.ExecuteNonQuery();
con.Close();
}

public List<Bal> details()
{
List<Bal> li = new List<Bal>();
SqlCommand cmd = new SqlCommand("select * from tbl_user", con);
con.Open();

SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
Bal ba = new Bal();
ba.uid = Convert.ToInt32(dr["uid"]);
ba.uname = dr["uname"].ToString();
ba.ugender = dr["ugender"].ToString();
ba.city = dr["city"].ToString();
ba.img = dr["img"].ToString();
li.Add(ba);
}
con.Close();
return li;
}

public Bal getid(int id)
{
Bal ba = new Bal();
SqlCommand cmd = new SqlCommand("select * from tbl_user where uid='" + id + "'", con);
con.Open();

SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
ba.uid = Convert.ToInt32(dr["uid"]);
ba.uname = dr["uname"].ToString();
ba.ugender = dr["ugender"].ToString();
ba.city = dr["city"].ToString();
ba.img = dr["img"].ToString();
}
con.Close();
return ba;
}

public Bal getdeatails(int id)
{
Bal ba = new Bal();
SqlCommand cmd = new SqlCommand("select * from tbl_user where uid='" + id + "'", con);
con.Open();

SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
ba.uid = Convert.ToInt32(dr["uid"]);
ba.uname = dr["uname"].ToString();
ba.ugender = dr["ugender"].ToString();
ba.city = dr["city"].ToString();
ba.img = dr["img"].ToString();
}
con.Close();
return ba;
}

public void Delete(int id)
{
con.Open();
// Bal ba=new Bal();
cmd = new SqlCommand("delete from tbl_user where uid='" + id + "'", con);
cmd.Parameters.AddWithValue("@uid", id);
cmd.ExecuteNonQuery();
con.Close();
}

public void savechange(Bal ba, int id)
{
con.Open();
// Bal ba = new Bal();
cmd = new SqlCommand("updateuser", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@uname", ba.uname);
cmd.Parameters.AddWithValue("@ugender", ba.ugender);
cmd.Parameters.AddWithValue("@city", ba.city);
cmd.Parameters.AddWithValue("@img", ba.img);
cmd.Parameters.AddWithValue("@uid", id);
cmd.ExecuteNonQuery();
con.Close();
}

public Bal getupdateid(int id)
{
Bal ba = new Bal();
SqlCommand cmd = new SqlCommand("select * from tbl_user where uid='" + id + "'", con);
con.Open();

SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
ba.uid = Convert.ToInt32(dr["uid"]);
ba.uname = dr["uname"].ToString();
ba.ugender = dr["ugender"].ToString();
ba.city = dr["city"].ToString();
ba.img = dr["img"].ToString();
}
con.Close();
return ba;
}

}

}

InsrtController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Insertdemo.Models;
using System.IO;

namespace Insertdemo.Controllers
{
public class InsertController : Controller
{
Bal ba = new Bal();
Dal da = new Dal();

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

[HttpPost]
public ActionResult Insert(Bal ba, HttpPostedFileBase file)
{


if (file != null)
{
file.SaveAs(Server.MapPath("~/Images/" + file.FileName));
ba.img = "~/Images/" + file.FileName;
}

da.Insert(ba);
Response.Write("Successfully insert!!");
return RedirectToAction("Details", "Insert");
}
public ActionResult Details()
{
return View(da.details());
}

public ActionResult Delete(int id)
{
return View(da.getid(id));
}

[HttpPost]
public ActionResult Delete(Bal ba,int id)
{
da.Delete(id);
return RedirectToAction("Details");
}

public ActionResult Getdeatails(int id)
{
return View(da.getdeatails(id));
}

public ActionResult Edit(int id)
{
return View(da.getupdateid(id));
}

[HttpPost]
public ActionResult Edit(Bal ba, int id, HttpPostedFileBase file)
{
if (file != null)
{
file.SaveAs(Server.MapPath("~/Images/" + file.FileName));
ba.img = "~/Images/" + file.FileName;
}
da.savechange(ba,id);
return RedirectToAction("Details");
}

//[HttpPost]
//public ActionResult FileUpload(HttpPostedFileBase uploadFile)
//{
// if (uploadFile.ContentLength > 0)
// {
// string filePath = Path.Combine(HttpContext.Server.MapPath("../Images"),
// Path.GetFileName(uploadFile.FileName));
// uploadFile.SaveAs(filePath);


// }
// return View();
//}
}
}

Insert.chtml

@model Insertdemo.Models.Bal

@{
Layout = null;
}

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Insert</title>
</head>
<body>
<script src="~/Scripts/jquery-1.7.1.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

@using (Html.BeginForm("Insert", "Insert", null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.ValidationSummary(true)

<fieldset>
<legend>Insert User Inforamation</legend>


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

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

<div class="editor-label">
City
</div>
<div class="editor-field">
@Html.EditorFor(model => model.city)
@Html.ValidationMessageFor(model => model.city)
</div>
<div class="editor-label">
User Photo
</div>
<div class="editor-field">
<input type="file" name="file"/>
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}

<div>
@Html.ActionLink("Back to List", "Details")
</div>
</body>
</html>


getdetails.chatml

@model Insertdemo.Models.Bal

@{
Layout = null;
}

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>getdeatails</title>
</head>
<body>
<fieldset>
<legend>User Inforamation</legend>


<div class="display-label">
User Name
</div>
<div class="display-field">
@Html.DisplayFor(model => model.uname)
</div>

<div class="display-label">
Gender
</div>
<div class="display-field">
@Html.DisplayFor(model => model.ugender)
</div>

<div class="display-label">
City
</div>
<div class="display-field">
@Html.DisplayFor(model => model.city)
</div>
</fieldset>
<p>
@Html.ActionLink("Edit", "Edit", new { id= Model.uid}) |
@Html.ActionLink("Back to List", "Details")
</p>
</body>
</html>


Edit.chtml

@model Insertdemo.Models.Bal

@{
Layout = null;
}

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Edit</title>
</head>
<body>
<script src="~/Scripts/jquery-1.7.1.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

@using (Html.BeginForm("Edit", "Insert", null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.ValidationSummary(true)

<fieldset>
<legend>Edit User Name</legend>

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

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

<div class="editor-label">
City
</div>
<div class="editor-field">
@Html.EditorFor(model => model.city)
@Html.ValidationMessageFor(model => model.city)
</div>
<div class="editor-label">
User Photo
</div>
<div class="editor-field">
<input type="file" name="file"/>
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}

<div>
@Html.ActionLink("Back to List", "Details")
</div>
</body>
</html>


Details.chtml

@model IEnumerable<Insertdemo.Models.Bal>

@{
Layout = null;
}

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Details Of User</title>
</head>
<body>
<p>
@Html.ActionLink("Create New", "Insert")
</p>
<table>
<tr>

<th>
User Name
</th>
<th>
Gender
</th>
<th>
City
</th>
<th>
User Photo
</th>
<th>
Operations
</th>
</tr>

@foreach (var item in Model) {
<tr>

<td>
@Html.DisplayFor(modelItem => item.uname)
</td>
<td>
@Html.DisplayFor(modelItem => item.ugender)
</td>
<td>
@Html.DisplayFor(modelItem => item.city)
</td>
<td>
@* @Html.DisplayFor(modelItem => item.Photo)*@
<img src="@Url.Content(item.img)" height="100px" width="100px"/>
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.uid }) |
@Html.ActionLink("Details", "Getdeatails", new { id = item.uid}) |
@Html.ActionLink("Delete", "Delete", new { id = item.uid})
</td>
</tr>
}

</table>
</body>
</html>


Delete.chtml

@model Insertdemo.Models.Bal

@{
Layout = null;
}

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Delete</title>
</head>
<body>
<h3>Are you sure you want to delete this?</h3>
<fieldset>
<legend>Delete User Record</legend>


<div class="display-label">
User Name
</div>
<div class="display-field">
@Html.DisplayFor(model => model.uname)
</div>

<div class="display-label">
Gender
</div>
<div class="display-field">
@Html.DisplayFor(model => model.ugender)
</div>

<div class="display-label">
City
</div>
<div class="display-field">
@Html.DisplayFor(model => model.city)
</div>
</fieldset>
@using (Html.BeginForm()) {
<p>
<input type="submit" value="Delete" /> |
@Html.ActionLink("Back to List", "Details")
</p>
}

</body>
</html>
Posted
Updated 18-Dec-15 18:10pm
v2

1 solution

insted of direct call from controller to db.
you create one "insertmanager" and "insertinterface" with "generic repository".
and call manager from controller.
 
Share this answer
 
Comments
JigxP 19-Dec-15 4:09am    
okk sir i try it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900