ProductsDb.cs
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace prjstart {
public class
ProductDetails {
public String ModelNumber;
public String ModelName;
public String ProductImage; // Get the Product details .Defined a class for
personalization purposes
public decimal
UnitCost;
public String Description;
}
public class
ProductsDB {
public SqlDataReader GetProductCategories()
{
// This function gets the product categories
and executes the productcategorylist stored procedure.
// this selects the category id nad category name from product categories
// Create Instance of Connection and Command
Object
SqlConnection myConnection = new
SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
SqlCommand myCommand = new
SqlCommand("ProductCategoryList", myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
// Execute the command
myConnection.Open();
SqlDataReader result =
myCommand.ExecuteReader(CommandBehavior.CloseConnection);
// Return the datareader result
return result;
}
public ProductDetails GetProductDetails(int productID) {
// Gets the product details through the query.
DataTable da = new DataTable();
SqlConnection
myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
SqlConnection
objcon = new
SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
string sql ="Select productid,modelname from
products where productid = '" + productID + "'";
SqlDataAdapter
objda = new SqlDataAdapter(sql,objcon);
objda.Fill(da);
// Create an instance of the class Product
details class defined above and return the details to the main page.
// Here i am accessing the model name in the main page.
ProductDetails myProductDetails = new
ProductDetails();
foreach(DataRow dr in
da.Rows)
{
myProductDetails.ModelName
= (string)dr["ModelName"];
}
return myProductDetails;
}
}
}