CodeSmith Community
Your Code. Your Way. Faster!

beginners blues - can't find a clean answer for adding a datasource to a datalist amongst other things!

Latest post 08-17-2007 12:07 PM by evolved. 7 replies.
  • 08-15-2007 8:53 AM

    • azyure
    • Not Ranked
    • Joined on 08-14-2007
    • Posts 4
    • Points 140

    beginners blues - can't find a clean answer for adding a datasource to a datalist amongst other things!

     Hi there,

    I'd like to apologise in advance if I am repeating a question someone else has asked or if it's a really stupid question! - but I have been searching the forums for ages and can't find a clear answer!

    I have installed netTiers on by asp.net/c# website and been using the basics without too many problems - but I have a simple request - I have a page where I would like to list my company profile using a DataList - now I can easily do this with using sql - but that's not really the point is it!  Can someone tell me - please - where I am going wrong trying to bind a some data to the datasource!

    Here is my code:

    (page)

    <asp:DataList ID="dlProfile" runat="server" Visible="true" RepeatColumns="2">
        <HeaderTemplate>Your Profile</HeaderTemplate>
        <ItemTemplate>
            Company Name: <%# Eval("companyName")%><br />
            Address 1:  <%# Eval("address1")%><br />
            Address 2:  <%# Eval("address2")%><br />
            Address 3:  <%# Eval("address3")%><br />
            Postcode:   <%# Eval("postcode")%><br />
            Phone:  <%# Eval("phone")%><br />
            Fax:    <%# Eval("fax")%><br />
            Mobile: <%# Eval("mobile")%><br />
            Email:  <%# Eval("companyEmail")%><br />
            Logo:   <%# Eval("companyLogo")%><br />
            Promo Email:    <%# Eval("promoEmail")%><br />   
        </ItemTemplate>
        </asp:DataList><br />


    Now I have tried to bind the data a few ways (with no success!)

     
    By adding a Datasource: 

         top Line changed:
        <asp:DataList ID="dlProfile" runat="server" Visible="true" RepeatColumns="2" DataSourceID="companyDataSourceStuff" DataKeyField="PkCompanyId" >


        <data:CompanyDataSource ID="companyDataSourceStuff" SelectMethod="GetByPkCompanyId" runat="server">
        </data:CompanyDataSource>
     

    Code Behind:

            companyDataSourceStuff.Parameters.Add("PkCompanyId",(string)Session["CompanyId"]);

     Which dosn't error - but also dosn't bring up the data!

     

    2nd way: (code behind)

            Company CurrentCompany = new Company();
            CompanyService CurrentCompanyService = new CompanyService();
            CurrentCompany = CurrentCompanyService.GetByPkCompanyId(CurrentCompanyID);
                   
            dlProfile.DataSource = CurrentCompany;
            dlProfile.DataBind();
     

    Which dosn't work as it can't loop through the data!

     

    3rd way: (code behind)

            TList<Company> companyCollection = DataRepository.CompanyProvider.GetByPkCompanyId(CurrentCompanyID);
            DataRepository.CompanyProvider.DeepLoad(companyCollection);
            dlProfile.DataSource = companyCollection;
            dlProfile.DataBind();

     Which was a total stab in the dark

    Any help would be appreciated!

    :o)
    Cassandra
     

    • Post Points: 65
  • 08-15-2007 9:41 AM In reply to

    • Aggnaught
    • Top 500 Contributor
    • Joined on 01-19-2007
    • Portland, OR
    • Posts 15
    • Points 315

    Re: beginners blues - can't find a clean answer for adding a datasource to a datalist amongst other things!

    Hi Cassandra,

    What does GetByPkCompanyId do, exactly? Does it return a single instance of a Company object based on primary key, or does it return a TList of objects? Per it's naming convention, it appears to be returning a single object, though in your 3rd implementation you have it returning a TList of Companies.

    If it does return a single Company, I'd use someting else besides a DataList.

    Jim

    • Post Points: 35
  • 08-15-2007 10:24 AM In reply to

    • azyure
    • Not Ranked
    • Joined on 08-14-2007
    • Posts 4
    • Points 140

    Re: beginners blues - can't find a clean answer for adding a datasource to a datalist amongst other things!

    Hi Jim,

    It returns a row of data (single instance) of all the company information (from the Company table).

    What would your suggestion be instead of a DataList? 

    Thanks for replying!
    Cassandra

     

    • Post Points: 35
  • 08-15-2007 11:42 AM In reply to

    • Aggnaught
    • Top 500 Contributor
    • Joined on 01-19-2007
    • Portland, OR
    • Posts 15
    • Points 315

    Re: beginners blues - can't find a clean answer for adding a datasource to a datalist amongst other things!

    Howdy -

    I'm not 100% certain of your requirements of course, but looking at the HTML contained in your DataList, a series of asp:Literal controls would do the trick just as well. You would just need to add one per Company property, and bind them in your code behind.

    Or if a DataList is really the way you want to go, add your single Company object to any collection, and then bind that collection, like this:

    Company currentCompany = CurrentCompanyService.GetByPkCompanyId(CurrentCompanyID);
    Company[] companyArray = new Company[1] {currentCompany};

    And then bind:

    dlProfile.DataSource = companyArray;
    dlProfile.DataBind();

    Howaboutthat?
    Jim

    • Post Points: 5
  • 08-15-2007 2:32 PM In reply to

    • stu28bu
    • Top 500 Contributor
    • Joined on 07-11-2007
    • Posts 10
    • Points 330

    Re: beginners blues - can't find a clean answer for adding a datasource to a datalist amongst other things!

    First question: are you SURE that the Session variable is getting set?  You might enable tracing on the aspx page and make sure that the value in the session field is what you're expecting.

    Second, you can use this method to avoid any code behind:

    <data:CompanyDataSource ID="companyDataSourceStuff" SelectMethod="GetByPkCompanyId" runat="server">
         <Parameters>
              <asp:SessionParameter Name="PkCompanyId" SessionField="CompanyID" />
         </Parameters>
    </data:CompanyDataSource>

    Also make sure that the SessionField above is using the correct SessionField name.

    • Post Points: 35
  • 08-16-2007 3:41 AM In reply to

    • azyure
    • Not Ranked
    • Joined on 08-14-2007
    • Posts 4
    • Points 140

    Re: beginners blues - can't find a clean answer for adding a datasource to a datalist amongst other things!

    Hi guys - thanks heaps for replying!

    Jim - your solution worked great thankyou!

    stu28bu - I also tried what you suggested (and checked that the correct variable is being passed  through) - but while it didn't error - I also couldn't get it to fill the DataList with information - thought purhaps I was setting the SessionField incorrectly?  I tried setting it in the code behind as well but with no results :(

    If you guys have time - the next step on from this that I need to do - is actually bind multiple sets of companies information to a repeater.

    So instead of returning one row of data - it will be returning 3-4 rows of companies information (I believe in a TList<>)?

    CompanyService companyListService = new CompanyService();
    TList<Company> companyTList = new TList<Company>();
    companyTList = companyListService.GetByFkMemberId(intMemberID);

    and then bind it to a DataSet or DataView?

     companyTList[0];

    ?

    Am I on the right track?

    Thanks again!
    :o)
    Cassandra
     

     

    • Post Points: 5
  • 08-16-2007 10:34 AM In reply to

    • azyure
    • Not Ranked
    • Joined on 08-14-2007
    • Posts 4
    • Points 140

    Re: beginners blues - can't find a clean answer for adding a datasource to a datalist amongst other things!

    Well I seem to have gotten it working - just for the record this is what I did!

    CodeBehind:

    CompanyService companyListService = new CompanyService();
    TList<Company> companyTList = new TList<Company>();
    companyTList = companyListService.GetByFkMemberId(intMemberID);

    DataSet ds = companyTList.ToDataSet(true);

    DataView dv = new DataView(ds.Tables[0]);

    (I needed it in a dv to do paging and stuff)

    So thanks for all your help!
    :o)

    Cassandra

    • Post Points: 35
  • 08-17-2007 12:07 PM In reply to

    • evolved
    • Top 50 Contributor
    • Joined on 12-27-2004
    • South River, NJ
    • Posts 96
    • Points 1,840

    Re: beginners blues - can't find a clean answer for adding a datasource to a datalist amongst other things!

    FWIW - you dont have to instantiate the tlist variable, the returned tlist is an instance you're just overwriting immediately. 

    http://www.jheidt.com
    ------------------------------
    Member of the .NetTiers team
    http://www.nettiers.com
    ------------------------------

    • Post Points: 5
Page 1 of 1 (8 items) | RSS
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems