You are on page 1of 4

Class Implementation in ASP.

Net

Product Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public class Product
{
int productid;
string productname, productdescription;
public string dberrorcode="n/a";
SqlConnection con = new SqlConnection();
public int Productid
{
get
{
return productid = GetMaxProductID();
}
}
public string Productname
{
get
{
return productname;
}
set
{
productname = value;
}
}
public string Productdescription
{
get
{
return productdescription;
}
set
{
productdescription = value;
}
}
public int AddProductToDatabase()
{
int i = 0;
productid=GetMaxProductID();
con.ConnectionString = ConfigurationManager.ConnectionStrings["RoleString"].ConnectionString;
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;

SiSTech,Kanpur Page 1
Class Implementation in ASP.Net

cmd.CommandType = CommandType.Text;
cmd.CommandText = "Insert into Product(ProID,ProName,ProDesc) Values(@ProID,@ProName,@ProDesc)";
cmd.Parameters.AddWithValue("@ProID",productid);
cmd.Parameters.AddWithValue("@ProName", productname);
cmd.Parameters.AddWithValue("@ProDesc", productdescription);
try
{
con.Open();
i= cmd.ExecuteNonQuery();
con.Close();
}
catch (SqlException ex)
{
i = -1;
dberrorcode += ex.Message;
}
return i;
}
public int GetMaxProductID()
{
int i = 0;
con.ConnectionString = ConfigurationManager.ConnectionStrings["RoleString"].ConnectionString;
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select @MaxVal=max(ProID) from Product";
SqlParameter p = new SqlParameter("@MaxVal", SqlDbType.Int, 4);
p.Direction = ParameterDirection.Output;
cmd.Parameters.Add(p);
try
{
con.Open();
cmd.ExecuteNonQuery();
con.Close();
if (cmd.Parameters["@MaxVal"].Value == DBNull.Value)
{
i = 1;
}
else
{
i = Convert.ToInt32(cmd.Parameters["@MaxVal"].Value.ToString())+1;
}
}
catch (SqlException ex)
{
dberrorcode = ex.Message;
i = -1;
}
return i;
}
public DataSet GetAllProduct()

SiSTech,Kanpur Page 2
Class Implementation in ASP.Net

con.ConnectionString = ConfigurationManager.ConnectionStrings["RoleString"].ConnectionString;
SqlDataAdapter da = new SqlDataAdapter("select ProID,ProName,ProDesc from Product order by ProID", con);
DataSet ds = new DataSet();
try
{
con.Open();
da.Fill(ds,"Product");
con.Close();
}
catch (SqlException ex)
{
dberrorcode = ex.Message;
}
return ds;
}
}

WebForm C# Code:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Product myPro = new Product();
GridView1.DataSource=myPro.GetAllProduct();
GridView1.DataBind();
Response.Write( myPro.Productid);
}
protected void Button1_Click(object sender, EventArgs e)
{
Product myPro = new Product();
myPro.Productname = TextBox1.Text.Trim();
myPro.Productdescription=TextBox2.Text.Trim();
int i= myPro.AddProductToDatabase();
if (i > 0)
{
Label1.Text = "Record Added";
GridView1.DataSource = myPro.GetAllProduct();
GridView1.DataBind();
}
else if (i == 0)
{
Label1.Text = "Record Adding Failed";
}
else
{
Label1.Text = "Error Occured: "+myPro.dberrorcode;
}
}

SiSTech,Kanpur Page 3
Class Implementation in ASP.Net

Web Form

SiSTech,Kanpur Page 4

You might also like