Link to home
Start Free TrialLog in
Avatar of Scripter25
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.QueryString["categoryID"]);
        gatci.product_ID= Convert.ToInt32(Request.QueryString["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;AttachDbFilename=|DataDirectory|\\ASPNETDB.mdf;Integrated Security=True;User Instance=True");
        SqlCommand cmd = new SqlCommand();

        //category_ID = Convert.ToInt32(Request.QueryString["categoryID"]);
        //product_ID = Convert.ToInt32(Request.QueryString["productID"]);
       
        cmd.CommandText = "productAddToCartInfo";
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Connection = sqlConnection1;
        cmd.Parameters.AddWithValue("@category_ID", category_ID);
        cmd.Parameters.AddWithValue("@product_ID", product_ID);


        sqlConnection1.Open();

        SqlDataReader DReader = cmd.ExecuteReader();
        while (DReader.Read())
        {
            if (DReader.HasRows)
            {
                SessionKey = Convert.ToString(DReader.GetValue(0));
                LineTotal = Convert.ToDecimal(DReader.GetValue(1));
                product_ID = Convert.ToInt32(DReader.GetValue(2));
                orderItem_name = Convert.ToString(DReader.GetValue(3));
                orderItem_price = Convert.ToDecimal(DReader.GetValue(4));
                orderItem_qty = Convert.ToInt32(DReader.GetValue(5));
                orderItem_o1 = Convert.ToString(DReader.GetValue(6));
                orderItem_o2 = Convert.ToString(DReader.GetValue(7));
                orderItem_o3 = Convert.ToString(DReader.GetValue(8));
                orderItem_ac = Convert.ToString(DReader.GetValue(9));
                orderItem_ph = Convert.ToString(DReader.GetValue(10));
                orderItem_photo = Convert.ToString(DReader.GetValue(11));

            }
            else
            {
               // lbl_name.Text = "no rows";
            }
        }

    }
Avatar of strickdd
strickdd
Flag of United States of America image

What you need to do is this:


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;AttachDbFilename=|DataDirectory|\\ASPNETDB.mdf;Integrated Security=True;User Instance=True");
        SqlCommand cmd = new SqlCommand();

        //category_ID = Convert.ToInt32(Request.QueryString["categoryID"]);
        //product_ID = Convert.ToInt32(Request.QueryString["productID"]);
       
        cmd.CommandText = "productAddToCartInfo";
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Connection = sqlConnection1;
        cmd.Parameters.AddWithValue("@category_ID", category_ID);
        cmd.Parameters.AddWithValue("@product_ID", product_ID);


ODSClass gatci = new ODSClass(Convert.ToInt32(Request.QueryString["categoryID"]), Convert.ToInt32(Request.QueryString["productID"]));
There are alternatives, but given your current setup, this will work for you.
Avatar of Scripter25
Scripter25

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.MissingMethodException: 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.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandle& ctor, Boolean& bNeedSecurityCheck) +0
   System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean fillCache) +103
   System.RuntimeType.CreateInstanceImpl(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean fillCache) +261
   System.Activator.CreateInstance(Type type, Boolean nonPublic) +66
   System.Web.UI.WebControls.ObjectDataSourceView.InvokeMethod(ObjectDataSourceMethod method, Boolean disposeInstance, Object& instance) +127
   System.Web.UI.WebControls.ObjectDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments) +2040
   System.Web.UI.WebControls.BaseDataList.GetData() +53
   System.Web.UI.WebControls.DataList.CreateControlHierarchy(Boolean useDataSource) +284
   System.Web.UI.WebControls.BaseDataList.OnDataBinding(EventArgs e) +56
   System.Web.UI.WebControls.BaseDataList.DataBind() +72
   System.Web.UI.WebControls.BaseDataList.EnsureDataBound() +55
   System.Web.UI.WebControls.BaseDataList.CreateChildControls() +63
   System.Web.UI.Control.EnsureChildControls() +87
   System.Web.UI.Control.PreRenderRecursiveInternal() +41
   System.Web.UI.Control.PreRenderRecursiveInternal() +161
   System.Web.UI.Control.PreRenderRecursiveInternal() +161
   System.Web.UI.Control.PreRenderRecursiveInternal() +161
   System.Web.UI.Control.PreRenderRecursiveInternal() +161
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1360


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(Request.QueryString["categoryID"]), Convert.ToInt32(Request.QueryString["productID"]));

instead of this:

ODSClass gatci = new ODSClass();
yes
ASKER CERTIFIED SOLUTION
Avatar of strickdd
strickdd
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Looks like that did it