Asp.Net MVC: With Product example 1
Web.config - Add the connection string
<connectionStrings>
<add name="ProductDBConnectionString" connectionString="Data Source=LK-PRK\SQL2012DEV;Initial Catalog=ProductDB;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
Connections.cs - in Model section
using System.Configuration;
using System.Data.SqlClient;
namespace TestStock.Models
{
public class Connection
{
public static SqlConnection GetConnection()
{
var conn = ConfigurationManager.ConnectionStrings["ProductDBConnectionString"].ConnectionString;
return new SqlConnection(conn);
}
}
}
Products.cs - in Models section
namespace TestStock.Models
{
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public double Price { get; set; }
}
}
ProductHelper.cs - in Model section
<connectionStrings>
<add name="ProductDBConnectionString" connectionString="Data Source=LK-PRK\SQL2012DEV;Initial Catalog=ProductDB;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
Connections.cs - in Model section
using System.Configuration;
using System.Data.SqlClient;
namespace TestStock.Models
{
public class Connection
{
public static SqlConnection GetConnection()
{
var conn = ConfigurationManager.ConnectionStrings["ProductDBConnectionString"].ConnectionString;
return new SqlConnection(conn);
}
}
}
Products.cs - in Models section
namespace TestStock.Models
{
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public double Price { get; set; }
}
}
ProductHelper.cs - in Model section
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
namespace TestStock.Models
{
public class ProductHelper
{
public static List<Product> GetProducts()
{
var conn = Connection.GetConnection();
conn.Open();
var cmd = new SqlCommand("SELECT * FROM Products", conn);
SqlDataReader dr = cmd.ExecuteReader();
var products = new List<Product>();
while (dr.Read())
{
var p = new Product
{
Id = int.Parse(dr.GetValue(0).ToString()),
Name = dr.GetValue(1).ToString(),
Price = double.Parse(dr.GetValue(2).ToString())
};
products.Add(p);
}
return products;
}
public static string AddProduct(Product product)
{
string message;
try
{
var name = new SqlParameter("@name", product.Name);
var price = new SqlParameter("@price", product.Price);
var conn = Connection.GetConnection();
conn.Open();
var cmd = conn.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "AddProducts";
cmd.Parameters.Add(name);
cmd.Parameters.Add(price);
cmd.ExecuteNonQuery();
conn.Close();
message = "Product Added !";
}
catch (Exception ex)
{
message = String.Format("Error: {0}", ex);
}
return message;
}
}
}
ProductsController - in Controller section
using System.Web.Mvc;
using TestStock.Models;
namespace TestStock.Controllers
{
public class ProductsController : Controller
{
//
// GET: /Products/
public ActionResult Index()
{
var products = ProductHelper.GetProducts();
return View(products);
}
}
}
Index - from Products Controller
@using TestStock.Models
@model List<Product>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
<table>
<tr>
<td>Name: </td>
<td><input id="txtProductName"/></td>
</tr>
<tr>
<td>Price: </td>
<td><input id="txtProductPrice" /></td>
</tr>
<tr>
<td></td>
<td><button id="btnAddProduct" onclick="AddProduct()">Add Product</button></td>
</tr>
</table>
<br/>
<table>
<tr>
<td>Id</td>
<td>Name</td>
<td>Price</td>
</tr>
@foreach (Product t in Model)
{
<tr>
<td>@t.Id</td>
<td>@t.Name</td>
<td>@t.Price</td>
</tr>
}
</table>
</div>
</body>
</html>
using System.Web.Mvc;
using TestStock.Models;
namespace TestStock.Controllers
{
public class ProductsController : Controller
{
//
// GET: /Products/
public ActionResult Index()
{
var products = ProductHelper.GetProducts();
return View(products);
}
}
}
Index - from Products Controller
@using TestStock.Models
@model List<Product>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
<table>
<tr>
<td>Name: </td>
<td><input id="txtProductName"/></td>
</tr>
<tr>
<td>Price: </td>
<td><input id="txtProductPrice" /></td>
</tr>
<tr>
<td></td>
<td><button id="btnAddProduct" onclick="AddProduct()">Add Product</button></td>
</tr>
</table>
<br/>
<table>
<tr>
<td>Id</td>
<td>Name</td>
<td>Price</td>
</tr>
@foreach (Product t in Model)
{
<tr>
<td>@t.Id</td>
<td>@t.Name</td>
<td>@t.Price</td>
</tr>
}
</table>
</div>
</body>
</html>
Comments
Post a Comment