.Net Better structure : With Product sample - Insert Data from C# (2)
Hi all, this is the 2nd post of this series. In this post I'll try to explain how to insert data to a database while keeping a better structure in your Visual Studio ASP.Net application.
First lets create this (or similar to this) Stored Procedure in your database to add products.
AddProducts Stored Procedure
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE AddProducts
@name varchar(50),
@price decimal(8,2)
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO Products VALUES (@name, @price);
END
GO
Inside the Domain folder of your project, create the following class.
Product.cs
namespace TestWeb.Domains
{
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public double Price { get; set; }
}
}
Under the DomainHelpers folder, inside ProductHelper class add this code.
ProductHelper.cs
public 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;
}
In your Product page add the following to create controllers to add product details.
Products.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Products.aspx.cs" Inherits="TestWeb.Products" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>Products</h2>
<asp:GridView ID="gridProducts" runat="server"></asp:GridView>
<br/>
<table>
<tr>
<td>Name: </td>
<td><asp:TextBox runat="server" ID="txtProductName"></asp:TextBox></td>
</tr>
<tr>
<td>Price: </td>
<td><asp:TextBox runat="server" ID="txtProductPrice"></asp:TextBox></td>
</tr>
<tr>
<td></td>
<td><asp:Button runat="server" ID="btnAddProduct" Text="Add Product" OnClick="btnAddProduct_OnClick"/></td>
</tr>
</table>
<br/>
<asp:Label runat="server" ID="lblMessage" Visible="False" Text="Message"></asp:Label>
</div>
</form>
</body>
</html>
Lets add the product when the AddProduct button clicked.
First lets create this (or similar to this) Stored Procedure in your database to add products.
AddProducts Stored Procedure
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE AddProducts
@name varchar(50),
@price decimal(8,2)
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO Products VALUES (@name, @price);
END
GO
Inside the Domain folder of your project, create the following class.
Product.cs
namespace TestWeb.Domains
{
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public double Price { get; set; }
}
}
Under the DomainHelpers folder, inside ProductHelper class add this code.
ProductHelper.cs
public 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;
}
In your Product page add the following to create controllers to add product details.
Products.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Products.aspx.cs" Inherits="TestWeb.Products" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>Products</h2>
<asp:GridView ID="gridProducts" runat="server"></asp:GridView>
<br/>
<table>
<tr>
<td>Name: </td>
<td><asp:TextBox runat="server" ID="txtProductName"></asp:TextBox></td>
</tr>
<tr>
<td>Price: </td>
<td><asp:TextBox runat="server" ID="txtProductPrice"></asp:TextBox></td>
</tr>
<tr>
<td></td>
<td><asp:Button runat="server" ID="btnAddProduct" Text="Add Product" OnClick="btnAddProduct_OnClick"/></td>
</tr>
</table>
<br/>
<asp:Label runat="server" ID="lblMessage" Visible="False" Text="Message"></asp:Label>
</div>
</form>
</body>
</html>
Lets add the product when the AddProduct button clicked.
Products.aspx.cs
protected void btnAddProduct_OnClick(object sender, EventArgs e)
{
var product = new Product {Name = txtProductName.Text, Price = double.Parse(txtProductPrice.Text)};
lblMessage.Visible = true;
lblMessage.Text = ph.AddProduct(product);
}
Comments
Post a Comment