Vicomi
- Back to Home »
- .NET MVC 3 Coding
------------------------------------- Connection Class -----------------------------------
class Connection
{
public static string connectionString
{
get { return "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=innosoftPOS;Data Source=Lakshika-PC\\SQLEXPRESS"; }
}
}
================================================================
-------------------------- Data Manager Class ---------------------------------------------------------
public class DataManager
{
public void insertCustomer(Customer customer)
{
string name = customer.name;
string house = customer.house;
string street = customer.street;
string city = customer.city;
string tel1 = customer.tel1;
string tel2 = customer.tel2;
int crediLimit = customer.creditLimit;
int currentCredit = customer.currentCredit;
SqlConnection conn = new SqlConnection(Connection.connectionString);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "insert into Customer values ('"+name+"','"+house+"','"+street+"','"+city+"','"+tel1+"','"+tel2+"',"+crediLimit+","+currentCredit+")";
cmd.ExecuteNonQuery();
conn.Close();
}
public List
{
List
SqlConnection conn = new SqlConnection(Connection.connectionString);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select * from Customer";
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
Customer customer = new Customer();
customer.name = dr.GetString(0);
customer.house = dr.GetString(1);
customer.street = dr.GetString(2);
customer.city = dr.GetString(3);
customer.tel1 = dr.GetString(4);
customer.tel2 = dr.GetString(5);
customer.creditLimit = dr.GetInt32(6);
customer.currentCredit = dr.GetInt32(7);
customers.Add(customer);
}
// conn.Close();
return customers;
}
public Customer getCustomerByName(string name)
{
Customer customer = new Customer();
SqlConnection conn = new SqlConnection(Connection.connectionString);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select * from Customer where name ='"+name+"'";
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
customer.house = dr.GetString(1);
customer.street = dr.GetString(2);
customer.city = dr.GetString(3);
customer.tel1 = dr.GetString(4);
customer.tel2 = dr.GetString(5);
customer.creditLimit = dr.GetInt32(6);
customer.currentCredit = dr.GetInt32(7);
}
conn.Close();
return customer;
}
public void updateCustomer(Customer customer)
{
string name = customer.name;
string house = customer.house;
string street = customer.street;
string city = customer.house;
string tel1 = customer.tel1;
string tel2 = customer.tel2;
int creditLimit = customer.currentCredit;
int currentCredit = customer.currentCredit;
SqlConnection conn = new SqlConnection(Connection.connectionString);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "update Customer set house='" + house + "', street='" + street + "',city='" + city + "',telephone1='" + tel1 + "',telephone2='"+tel2+"',creditLimit='"+creditLimit+"',currentCredit='"+currentCredit+"' where name='"+name+"'";
cmd.ExecuteNonQuery();
conn.Close();
}
public void deleteCustomer(string name)
{
SqlConnection conn = new SqlConnection(Connection.connectionString);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "delete from Customer where name='"+name+"'";
cmd.ExecuteNonQuery();
conn.Close();
}
}
===============================================================
------------------------------------ Customer Controller ----------------------------------
//--------------------------------- Customer Create ---------------------------------
[HttpPost]
public ActionResult Create(Customer customer)
{
DataManager dataManager = new DataManager();
dataManager.insertCustomer(customer);
return View();
}
public ActionResult Create()
{
return View();
}
//-----------------------------------------------------------------------------------
//-------------------------------------- Customer View -----------------------------
public ActionResult View()
{
DataManager dataManager = new DataManager();
List
return View(customer);
}
//-----------------------------------------------------------------------------------
//--------------------------------- Update ----------------------------------------
public ActionResult Update(string name)
{
DataManager dataManager = new DataManager();
Customer customer = dataManager.getCustomerByName(name);
return View(customer);
}
[HttpPost]
public ActionResult Update(Customer customer)
{
DataManager dataManager = new DataManager();
dataManager.updateCustomer(customer);
return RedirectToAction("View");
}
//---------------------------------------------------------------------------------
//------------------------------------ Delete ------------------------------------
public ActionResult Delete()
{
return View();
}
[HttpPost]
public ActionResult Delete(string name)
{
DataManager dataManager = new DataManager();
dataManager.deleteCustomer(name);
return RedirectToAction("View");
}
//----------------------------------------------------------------------------------
==================== Delete View ****---------------------------------------
<% foreach (var item in Model) { %>
<%: Html.ActionLink("Edit", "Update", new { name=item.name }) %> |
<%: Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) %> |
<%: Html.ActionLink("Delete", "Delete", new { name=item.name }) %>
<% using (Html.BeginForm("Delete", "Customer", new { name = item.name })) %>
<% { %>
<%} %>
<%: item.name %>
<%: item.house %>
<%: item.street %>
<%: item.city %>
<%: item.tel1 %>
<%: item.tel2 %>
<%: item.creditLimit %>
<%: item.currentCredit %>
<% } %>
-----------------------==========================================