using datalist with codebehind

i'm trying to convert my application to use codebehind files instead of just single files. ii need to set the datasource for the list to a method call, the method also needs the customerid variable. Here's the code

<asp:DataList id="MyList" runat="server" RepeatColumns="2" DataSource="<%#customerprofile.GetProfile(customerId) %>">
                                        <ItemTemplate>
                                            <table border="0" width="300">
                                                <tr>
                                                    <td width="25">
                                                        &nbsp;
                                                    </td>
                                                    </a>
                                                    </td>
                                                </tr>
                                                <br />
                                                <br />
                                                <b>Firstname: </b> <%# DataBinder.Eval(Container.DataItem, "firstname") %>
                                                <br />
                                                <b>Email: </b> <%# DataBinder.Eval(Container.DataItem, "emailaddress") %>
                                                <br />
                                                <b>password: </b> <%# DataBinder.Eval(Container.DataItem, "password") %>
                                                <br />
                                                <b>Last Name: </b> <%# DataBinder.Eval(Container.DataItem, "lastname") %>
                                                <br />
                                                <b>Company:</b> <%# DataBinder.Eval(Container.DataItem, "company") %>
                                                <br />
                                                <b>Url: </b> <%# DataBinder.Eval(Container.DataItem, "url") %>
                                                <br />
                                                <b>Phone: </b> <%# DataBinder.Eval(Container.DataItem, "phonenumber") %>
                                                <br />
                                                <b>Address: </b> <%# DataBinder.Eval(Container.DataItem, "address") %>
                                                <br />
                                                <b>State: </b> <%# DataBinder.Eval(Container.DataItem, "state") %> <%# DataBinder.Eval(Container.DataItem, "state") %>
                                                <br />
                                                <b>Zipcode: </b> <%# DataBinder.Eval(Container.DataItem, "zipcode") %>
                                    </table>
                                        </ItemTemplate>
                                    </asp:DataList>
      
CODEBEHIND
...
private void Page_Load(object sender, System.EventArgs e)
            {
                  string customerid;
                  int customerId = Int32.Parse(Request.Params["CustomerID"]);
                  Session["customerid"] = customerId;
                  customerid = Session["customerid"].ToString();

                  // Obtain products and databind to an asp:datalist control
                  IBuySpy.CustomersDB customerprofile = new IBuySpy.CustomersDB();

                  //MyList.DataSource = customerprofile.GetProfile(customerId);
                  //MyList.DataBind();
            }
...

I commented the last two lines, which were how the single file solution worked. Any ideas on how to get this to work? I get this error since obviously it can't see the customerid:

Compiler Error Message: CS0103: The name 'customerId' does not exist in the class or namespace 'ASP.Userprofile_aspx'
Line 13:       <asp:DataList id="MyList" runat="server" RepeatColumns="2" DataSource="<%#customerprofile.GetProfile(customerId) %>">
      
dprasadAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ihenryCommented:
hello dprasad,

the problem because of this line
<asp:DataList id="MyList" runat="server" RepeatColumns="2" DataSource="<%#customerprofile.GetProfile(customerId) %>">

the customerId variable needs to be declared as protected on top of the class (module class scope) in order to be visible within aspx.

public class Userprofile : System.Web.UI.Page
{
     protected int customerId; // place the var here..
     .....
     .....
}
0
ihenryCommented:

Or try another way like so

public class Userprofile : System.Web.UI.Page
{
      private void Page_Load(object sender, System.EventArgs e)
      {
            string customerid;
            int customerId = Int32.Parse(Request.Params["CustomerID"]);
            Session["customerid"] = customerId;
            customerid = Session["customerid"].ToString();

            // Obtain products and databind to an asp:datalist control
            IBuySpy.CustomersDB customerprofile = new IBuySpy.CustomersDB();

            MyList.DataSource = customerprofile.GetProfile(customerId);
            MyList.DataBind();
      }
}

And inside aspx declare the datalist server control tag without datasource attribute
<asp:DataList id="MyList" runat="server" RepeatColumns="2">

That should solve your problem faster.
0
dprasadAuthor Commented:
ok, I tried that but am now getting this error:

Compiler Error Message: CS0246: The type or namespace name 'customerprofile' could not be found (are you missing a using directive or an assembly reference?)

here is what my codebehind is now:

...
public class Userprofile : System.Web.UI.Page
      {
            protected int customerId;

            private void Page_Load(object sender, System.EventArgs e)
            {
                  
                  int customerId = Int32.Parse(Request.Params["CustomerID"]);
                  
                  // Obtain products and databind to an asp:datalist control
                  IBuySpy.CustomersDB customerprofile = new IBuySpy.CustomersDB();

                  //MyList.DataSource = customerprofile.GetProfile(customerId);
                  //MyList.DataBind();
            }

            #region Web Form Designer generated code
            override protected void OnInit(EventArgs e)
            {
                  //
                  // CODEGEN: This call is required by the ASP.NET Web Form Designer.
                  //
                  InitializeComponent();
                  base.OnInit(e);
            }
            
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {    
                  this.Load += new System.EventHandler(this.Page_Load);
            }
            #endregion
      }
...
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

dprasadAuthor Commented:
alright, i tried the second suggestion.. this was my original problem actually, I get this compliation error:

E:\Inetpub\wwwroot\StoreCSVS\Userprofile.aspx.cs(32): The type or namespace name 'MyList' could not be found (are you missing a using directive or an assembly reference?)


0
ihenryCommented:
The first problem, import full qualified customerprofile class in the Userprofile.aspx.cs. Something similar like the following
     
     using IBuySpy;

And also in the Userprofile.aspx

<%@ Import Namespace="IBuySpy" %>

The "IBuySpy" is the namespace name where the CustomersDB resides.
0
ihenryCommented:

The second problem, In VS.NET IDE, close the two file Userprofile.aspx and Userprofile.aspx.cs. And then recompile, always recompile after you make any change  in your code.
0
dprasadAuthor Commented:
hmm, I tried the first suggesion and I still get the The type or namespace name 'customerprofile' could not be found runtime error.

I did try the second suggestion and close the two userprofile files and rebuild. got the same error. Here's my .cs file

using IBuySpy;
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace IBuySpy
{
      /// <summary>
      /// Summary description for Userprofile.
      /// </summary>
      public class Userprofile : System.Web.UI.Page
      {
            protected System.Web.UI.WebControls.DataList MyList;
            protected int customerId;

            private void Page_Load(object sender, System.EventArgs e)
            {
                  
                  
                  customerId = Int32.Parse(Request.Params["CustomerID"]);
                  Session["customerid"] = customerId;
                  //customerid = Session["customerid"].ToString();

                  // Obtain products and databind to an asp:datalist control
                  IBuySpy.CustomersDB customerprofile = new IBuySpy.CustomersDB();

                  //MyList.DataSource = customerprofile.GetProfile(customerId);
                  //MyList.DataBind();
            }

            #region Web Form Designer generated code
            override protected void OnInit(EventArgs e)
            {
                  //
                  // CODEGEN: This call is required by the ASP.NET Web Form Designer.
                  //
                  InitializeComponent();
                  base.OnInit(e);
            }
            
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {    
                  this.Load += new System.EventHandler(this.Page_Load);

            }
            #endregion
      }
}

ASP

<asp:DataList id="MyList" runat="server" RepeatColumns="2" DataSource="<%#customerprofile.GetProfile(customerId)%>">
                  <ItemTemplate>
                        <table border="0" width="300">
                              <tr>
                                    <td width="25">
                                          &nbsp;
                                    </td>
                                    </a> </td>
                              </tr>
                              <br />
                              <br />
                              <b>Firstname: </b>
                              <%# DataBinder.Eval(Container.DataItem, "firstname") %>
                              <br />
                              <b>Email: </b>
                              <%# DataBinder.Eval(Container.DataItem, "emailaddress") %>
                              <br />
                              <b>password: </b>
                              <%# DataBinder.Eval(Container.DataItem, "password") %>
                              <br />
                              <b>Last Name: </b>
                              <%# DataBinder.Eval(Container.DataItem, "lastname") %>
                              <br />
                              <b>Company:</b>
                              <%# DataBinder.Eval(Container.DataItem, "company") %>
                              <br />
                              <b>Url: </b>
                              <%# DataBinder.Eval(Container.DataItem, "url") %>
                              <br />
                              <b>Phone: </b>
                              <%# DataBinder.Eval(Container.DataItem, "phonenumber") %>
                              <br />
                              <b>Address: </b>
                              <%# DataBinder.Eval(Container.DataItem, "address") %>
                              <br />
                              <b>State: </b>
                              <%# DataBinder.Eval(Container.DataItem, "state") %>
                              <%# DataBinder.Eval(Container.DataItem, "state") %>
                              <br />
                              <b>Zipcode: </b>
                              <%# DataBinder.Eval(Container.DataItem, "zipcode") %>
                        </table>
                  </ItemTemplate>
            </asp:DataList>
...

0
ihenryCommented:
Assuming the customerprofile class looks like this

namespace theNamespace
{
      public class customerprofile
      {
           ....
           ....
      }
      ....
      ....
}

in the Userprofile.aspx.cs import the namespace, like so

<%@ Import Namespace="theNamespace" %>
<HTML>
  <HEAD>
        <link rel="stylesheet" type="text/css" href="IBuySpy.css">
  </HEAD>
    <body background="images/sitebkgrdnogray.gif" leftmargin="0" topmargin="0" rightmargin="0" bottommargin="0" marginheight="0" marginwidth="0">


using theNamespace;   <<== need this

namespace IBuySpy
{
     /// <summary>
     /// Summary description for Userprofile.
     /// </summary>
     public class Userprofile : System.Web.UI.Page
     {
     }
}

And in the Userprofile.aspx

<%@ Import Namespace="theNamespace" %> <<== need this
<HTML>
  <HEAD>
        <link rel="stylesheet" type="text/css" href="IBuySpy.css">
  </HEAD>
    <body background="images/sitebkgrdnogray.gif" leftmargin="0" topmargin="0" rightmargin="0" bottommargin="0" marginheight="0" marginwidth="0">

Pay attention to letter case-sensivity.
0
dprasadAuthor Commented:
hmmm.. yeah that's what i've done.. I will keep fiddling with it. thanks!

Dinesh
0
ihenryCommented:
So, do you get it works?
If you have the customerprofile class is in another assembly then add a reference to the assembly in your project.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dprasadAuthor Commented:
ahh.. got it now. think i misplaced some code between the solution. Thanks!

Dinesh
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.