Scripter25
asked on
Pulling Data from DB with Class
Treat me as a complete newb on this one
I am trying to get a row of values from the database so that I can I can display on my page and then use those same variables to insert into a temp table so far this is what I have even though I understand why it is not working I have no idea how to make it work.
here is my codebehind for the page that I am working with
ODSClass gatci = new ODSClass();
gatci.category_ID= Convert.ToInt32(Request.Qu eryString[ "categoryI D"]);
gatci.product_ID= Convert.ToInt32(Request.Qu eryString[ "productID "]);
lbl_name.Text = gatci.orderItem_name;
in a class file I have this
public class ODSClass
{
public ODSClass()
{
//
// TODO: Add constructor logic here
//
}
private string _SessionKey;
private decimal _LineTotal;
private int _category_ID;
private int _product_ID;
private string _orderItem_name;
private decimal _orderItem_price;
private int _orderItem_qty;
private string _orderItem_o1;
private string _orderItem_o2;
private string _orderItem_o3;
private string _orderItem_ac;
private string _orderItem_ph;
private string _orderItem_photo;
public string SessionKey
{
get { return _SessionKey; }
set { _SessionKey = value; }
}
public decimal LineTotal
{
get { return _LineTotal; }
set { _LineTotal = value; }
}
public int category_ID
{
get { return _category_ID; }
set { _category_ID = value; }
}
public int product_ID
{
get { return _product_ID; }
set { _product_ID = value; }
}
public string orderItem_name
{
get { return _orderItem_name; }
set { _orderItem_name = value; }
}
public decimal orderItem_price
{
get { return _orderItem_price; }
set { _orderItem_price = value; }
}
public int orderItem_qty
{
get { return _orderItem_qty; }
set { _orderItem_qty = value; }
}
public string orderItem_o1
{
get { return _orderItem_o1; }
set { _orderItem_o1 = value; }
}
public string orderItem_o2
{
get { return _orderItem_o2; }
set { _orderItem_o2 = value; }
}
public string orderItem_o3
{
get { return _orderItem_o3; }
set { _orderItem_o3 = value; }
}
public string orderItem_ac
{
get { return _orderItem_ac; }
set { _orderItem_ac = value; }
}
public string orderItem_ph
{
get { return _orderItem_ph; }
set { _orderItem_ph = value; }
}
public string orderItem_photo
{
get { return _orderItem_photo; }
set { _orderItem_photo = value; }
}
public void GetAddToCartInfo(int category_ID, int product_ID)
{
SqlConnection sqlConnection1 = new SqlConnection("Data Source=.\\SQLEXPRESS;Attac hDbFilenam e=|DataDir ectory|\\A SPNETDB.md f;Integrat ed Security=True;User Instance=True");
SqlCommand cmd = new SqlCommand();
//category_ID = Convert.ToInt32(Request.Qu eryString[ "categoryI D"]);
//product_ID = Convert.ToInt32(Request.Qu eryString[ "productID "]);
cmd.CommandText = "productAddToCartInfo";
cmd.CommandType = CommandType.StoredProcedur e;
cmd.Connection = sqlConnection1;
cmd.Parameters.AddWithValu e("@catego ry_ID", category_ID);
cmd.Parameters.AddWithValu e("@produc t_ID", product_ID);
sqlConnection1.Open();
SqlDataReader DReader = cmd.ExecuteReader();
while (DReader.Read())
{
if (DReader.HasRows)
{
SessionKey = Convert.ToString(DReader.G etValue(0) );
LineTotal = Convert.ToDecimal(DReader. GetValue(1 ));
product_ID = Convert.ToInt32(DReader.Ge tValue(2)) ;
orderItem_name = Convert.ToString(DReader.G etValue(3) );
orderItem_price = Convert.ToDecimal(DReader. GetValue(4 ));
orderItem_qty = Convert.ToInt32(DReader.Ge tValue(5)) ;
orderItem_o1 = Convert.ToString(DReader.G etValue(6) );
orderItem_o2 = Convert.ToString(DReader.G etValue(7) );
orderItem_o3 = Convert.ToString(DReader.G etValue(8) );
orderItem_ac = Convert.ToString(DReader.G etValue(9) );
orderItem_ph = Convert.ToString(DReader.G etValue(10 ));
orderItem_photo = Convert.ToString(DReader.G etValue(11 ));
}
else
{
// lbl_name.Text = "no rows";
}
}
}
I am trying to get a row of values from the database so that I can I can display on my page and then use those same variables to insert into a temp table so far this is what I have even though I understand why it is not working I have no idea how to make it work.
here is my codebehind for the page that I am working with
ODSClass gatci = new ODSClass();
gatci.category_ID= Convert.ToInt32(Request.Qu
gatci.product_ID= Convert.ToInt32(Request.Qu
lbl_name.Text = gatci.orderItem_name;
in a class file I have this
public class ODSClass
{
public ODSClass()
{
//
// TODO: Add constructor logic here
//
}
private string _SessionKey;
private decimal _LineTotal;
private int _category_ID;
private int _product_ID;
private string _orderItem_name;
private decimal _orderItem_price;
private int _orderItem_qty;
private string _orderItem_o1;
private string _orderItem_o2;
private string _orderItem_o3;
private string _orderItem_ac;
private string _orderItem_ph;
private string _orderItem_photo;
public string SessionKey
{
get { return _SessionKey; }
set { _SessionKey = value; }
}
public decimal LineTotal
{
get { return _LineTotal; }
set { _LineTotal = value; }
}
public int category_ID
{
get { return _category_ID; }
set { _category_ID = value; }
}
public int product_ID
{
get { return _product_ID; }
set { _product_ID = value; }
}
public string orderItem_name
{
get { return _orderItem_name; }
set { _orderItem_name = value; }
}
public decimal orderItem_price
{
get { return _orderItem_price; }
set { _orderItem_price = value; }
}
public int orderItem_qty
{
get { return _orderItem_qty; }
set { _orderItem_qty = value; }
}
public string orderItem_o1
{
get { return _orderItem_o1; }
set { _orderItem_o1 = value; }
}
public string orderItem_o2
{
get { return _orderItem_o2; }
set { _orderItem_o2 = value; }
}
public string orderItem_o3
{
get { return _orderItem_o3; }
set { _orderItem_o3 = value; }
}
public string orderItem_ac
{
get { return _orderItem_ac; }
set { _orderItem_ac = value; }
}
public string orderItem_ph
{
get { return _orderItem_ph; }
set { _orderItem_ph = value; }
}
public string orderItem_photo
{
get { return _orderItem_photo; }
set { _orderItem_photo = value; }
}
public void GetAddToCartInfo(int category_ID, int product_ID)
{
SqlConnection sqlConnection1 = new SqlConnection("Data Source=.\\SQLEXPRESS;Attac
SqlCommand cmd = new SqlCommand();
//category_ID = Convert.ToInt32(Request.Qu
//product_ID = Convert.ToInt32(Request.Qu
cmd.CommandText = "productAddToCartInfo";
cmd.CommandType = CommandType.StoredProcedur
cmd.Connection = sqlConnection1;
cmd.Parameters.AddWithValu
cmd.Parameters.AddWithValu
sqlConnection1.Open();
SqlDataReader DReader = cmd.ExecuteReader();
while (DReader.Read())
{
if (DReader.HasRows)
{
SessionKey = Convert.ToString(DReader.G
LineTotal = Convert.ToDecimal(DReader.
product_ID = Convert.ToInt32(DReader.Ge
orderItem_name = Convert.ToString(DReader.G
orderItem_price = Convert.ToDecimal(DReader.
orderItem_qty = Convert.ToInt32(DReader.Ge
orderItem_o1 = Convert.ToString(DReader.G
orderItem_o2 = Convert.ToString(DReader.G
orderItem_o3 = Convert.ToString(DReader.G
orderItem_ac = Convert.ToString(DReader.G
orderItem_ph = Convert.ToString(DReader.G
orderItem_photo = Convert.ToString(DReader.G
}
else
{
// lbl_name.Text = "no rows";
}
}
}
There are alternatives, but given your current setup, this will work for you.
ASKER
I get the following error
No parameterless constructor defined for this object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.MissingMethodExcept ion: No parameterless constructor defined for this object.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[MissingMethodException: No parameterless constructor defined for this object.]
System.RuntimeTypeHandle.C reateInsta nce(Runtim eType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandle& ctor, Boolean& bNeedSecurityCheck) +0
System.RuntimeType.CreateI nstanceSlo w(Boolean publicOnly, Boolean fillCache) +103
System.RuntimeType.CreateI nstanceImp l(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean fillCache) +261
System.Activator.CreateIns tance(Type type, Boolean nonPublic) +66
System.Web.UI.WebControls. ObjectData SourceView .InvokeMet hod(Object DataSource Method method, Boolean disposeInstance, Object& instance) +127
System.Web.UI.WebControls. ObjectData SourceView .ExecuteSe lect(DataS ourceSelec tArguments arguments) +2040
System.Web.UI.WebControls. BaseDataLi st.GetData () +53
System.Web.UI.WebControls. DataList.C reateContr olHierarch y(Boolean useDataSource) +284
System.Web.UI.WebControls. BaseDataLi st.OnDataB inding(Eve ntArgs e) +56
System.Web.UI.WebControls. BaseDataLi st.DataBin d() +72
System.Web.UI.WebControls. BaseDataLi st.EnsureD ataBound() +55
System.Web.UI.WebControls. BaseDataLi st.CreateC hildContro ls() +63
System.Web.UI.Control.Ensu reChildCon trols() +87
System.Web.UI.Control.PreR enderRecur siveIntern al() +41
System.Web.UI.Control.PreR enderRecur siveIntern al() +161
System.Web.UI.Control.PreR enderRecur siveIntern al() +161
System.Web.UI.Control.PreR enderRecur siveIntern al() +161
System.Web.UI.Control.PreR enderRecur siveIntern al() +161
System.Web.UI.Page.Process RequestMai n(Boolean includeStagesBeforeAsyncPo int, Boolean includeStagesAfterAsyncPoi nt) +1360
Version Information: Microsoft .NET Framework Version:2.0.50727.832; ASP.NET Version:2.0.50727.832
No parameterless constructor defined for this object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.MissingMethodExcept
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[MissingMethodException: No parameterless constructor defined for this object.]
System.RuntimeTypeHandle.C
System.RuntimeType.CreateI
System.RuntimeType.CreateI
System.Activator.CreateIns
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.Control.Ensu
System.Web.UI.Control.PreR
System.Web.UI.Control.PreR
System.Web.UI.Control.PreR
System.Web.UI.Control.PreR
System.Web.UI.Control.PreR
System.Web.UI.Page.Process
Version Information: Microsoft .NET Framework Version:2.0.50727.832; ASP.NET Version:2.0.50727.832
are you doing this:
ODSClass gatci = new ODSClass(Convert.ToInt32(R equest.Que ryString[" categoryID "]), Convert.ToInt32(Request.Qu eryString[ "productID "]));
instead of this:
ODSClass gatci = new ODSClass();
ODSClass gatci = new ODSClass(Convert.ToInt32(R
instead of this:
ODSClass gatci = new ODSClass();
ASKER
yes
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Looks like that did it
private ODSClass()
{
//
// TODO: Add constructor logic here
//
}
public ODSClass(int category_ID, int product_ID)
{
GetAddToCartInfo();
}
private void GetAddToCartInfo()
{
SqlConnection sqlConnection1 = new SqlConnection("Data Source=.\\SQLEXPRESS;Attac
SqlCommand cmd = new SqlCommand();
//category_ID = Convert.ToInt32(Request.Qu
//product_ID = Convert.ToInt32(Request.Qu
cmd.CommandText = "productAddToCartInfo";
cmd.CommandType = CommandType.StoredProcedur
cmd.Connection = sqlConnection1;
cmd.Parameters.AddWithValu
cmd.Parameters.AddWithValu
ODSClass gatci = new ODSClass(Convert.ToInt32(R