Solved

Accessing Nested DataList

Posted on 2004-09-16
13
3,193 Views
Last Modified: 2008-01-09
Hi, I have a DataList nested inside another one.  How can I access the DataFieldKey of the childDataList to perform a query in database:

<asp:DataList ID="parentDataList"  runat="server">
   <ItemTemplate>
            <%#DataBinder.Eval(Container.DataItem,"headerName")%>
            
              <asp:DataList ID="childDataList"                   
                     DataSource='<%#((DataRowView)Container.DataItem).Row.GetChildRows("getList")%>'
      OnDeleteCommand="DeleteItem"
      DataFieldKey="itemID"
      runat="server">

                <ItemTemplate>

                   <asp:LinkButton ID="Delete" CommandName="Delete" Text="Delete" runat="server"/>

                        //...etc.


CODE:------------------

void DeleteItem(Object Sender,DataListCommandEventArgs e)
{
   SqlConnection myConn=new SqlConnection();
   myConn.ConnectionString=ConfigurationSettings.AppSettings["DbConn"];
   myConn.Open();
       
       SqlCommand cmdDelete=new SqlCommand("DeleteItem",myConn);
       cmdDelete.CommandType=CommandType.StoredProcedure;
       cmdDelete.Parameters.Add("@itemDelete",SqlDbType.Int);
//Can't get DataKey:
       cmdDelete.Parameters["@itemDelete"].Value=childDataList.DataKeys[e.Item.ItemIndex];
       cmdDelete.ExecuteNonQuery();

myConn.Close();
BindData();
}

I get the error: "The type or namespace name 'childDataList' could not be found (are you missing a using directive or an assembly reference?)"



0
Comment
Question by:champ_010
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
13 Comments
 
LVL 33

Expert Comment

by:raterus
ID: 12078360
Cast the original DataList from sender, like so

DataList dl = (DataList)Sender;
cmdDelete.Parameters["@itemDelete"].Value=dl.DataKeys[e.Item.ItemIndex];

--Michael
0
 
LVL 1

Author Comment

by:champ_010
ID: 12078636
Thanks but I get an error saying:

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

Any ideas??
0
 
LVL 33

Accepted Solution

by:
raterus earned 150 total points
ID: 12078777
That only gets thrown if you didn't set up your datakeyfield properly on your second datalist, it's probably blank.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:champ_010
ID: 12079014

I typed DataFieldKey instead of DataKeyField but now the error says:

System.Web.HttpException: DataBinder.Eval: 'System.Data.DataRow' does not contain a property with the name itemID

...yet I can output the itemID in that DataList if I did :

<%#DataBinder.Eval(Container.DataItem,"itemID")%>

so why does it seem missing?
0
 
LVL 33

Expert Comment

by:raterus
ID: 12079079
The correct property is "DataKeyField"

That's a good question, post entire code of what works/what doesn't in case I see something you're doing silly.
0
 
LVL 1

Author Comment

by:champ_010
ID: 12079258
Thanks here's the code.  Points increased for taking a look at code.

There are a different set of GuideHeader depending on which Category was selected from a DropDownList.

For each type of GuideHeader there are the some Guides.  

I am trying to show the Guides in a nested DataList under each type of GuideHeader (parent DataList) for the chosen Category.  

I would like to click "Delete" next to each Guide in the child DataList to Delete it from the list.

------------

<script runat="server">
int pID;
int SubID;
int CatID;
void Page_Load(object Sender,EventArgs e)
{

if(!IsPostBack){
 SqlConnection myConn=new SqlConnection();
 myConn.ConnectionString=ConfigurationSettings.AppSettings["DBConn"];
 myConn.Open();

 //GET CATEGORIES DROPDOWN
 SqlCommand cmd1=new SqlCommand("GetCategories",myConn);
 cmd1.CommandType=CommandType.StoredProcedure;
 SqlDataReader dr1=cmd1.ExecuteReader();
 SelectCategory.DataSource=dr1;
 SelectCategory.DataBind();
 SelectCategory.Items.Insert(0, "");
 dr1.Close();
 myConn.Close();
 }
}
//For the OnClick of DropDownList
void GetGuides(Object Sender,EventArgs e)
{
 CatID=SelectCategory.SelectedIndex;
 ShowGuides();
}
//BindData
void ShowGuides()
{
 CatID=SelectCategory.SelectedIndex;
 SqlConnection myConn=new SqlConnection();
 myConn.ConnectionString=ConfigurationSettings.AppSettings["DbConn"];
 myConn.Open();

   //MAKE DATASET FOR BOTH TABLES
          DataSet ds=new DataSet();

      //GET GUIDE HEADERS FOR CHOSEN CATEGORY
       SqlCommand cmd2=new SqlCommand("GetAllGuideHeaders",myConn);
      cmd2.CommandType=CommandType.StoredProcedure;
      cmd2.Parameters.Add("@CatID",SqlDbType.Int);
      cmd2.Parameters["@CatID"].Value=CatID;
      SqlDataAdapter da2=new SqlDataAdapter(cmd2);
      da2.Fill(ds,"GuideHeaders");

      //GET GUIDES FOR EACH GUIDEHEADER IN THE CATEGORY
      SqlCommand cmd3=new SqlCommand("GetAllGuidesView",myConn);
      cmd3.CommandType=CommandType.StoredProcedure;
      cmd3.Parameters.Add("@frmCatID",SqlDbType.Int);
      cmd3.Parameters["@frmCatID"].Value=CatID;
      SqlDataAdapter da3=new SqlDataAdapter(cmd3);
      da3.Fill(ds,"Guides");

      //MAKE DATARELATION
      DataRelation dr=ds.Relations.Add(("GetGuides"),
            ds.Tables["GuideHeaders"].Columns["guideHeaderID"],
            ds.Tables["Guides"].Columns["guideHeaderID"]);

      //BIND GUIDE HEADERS
      GuideHeadersList.DataSource=ds.Tables[0].DefaultView;
      GuideHeadersList.DataBind();
 myConn.Close();
}
void DeleteGuide(Object Sender,DataListCommandEventArgs e)
{
      DataList childDL = (DataList)Sender;
      SqlConnection myConn=new SqlConnection();
      myConn.ConnectionString=ConfigurationSettings.AppSettings["DBConn"];
      myConn.Open();
            SqlCommand cmdDelete=new SqlCommand("DeleteGuide",myConn);
            cmdDelete.CommandType=CommandType.StoredProcedure;
            cmdDelete.Parameters.Add("@itemGuideDelete",SqlDbType.Int);
            cmdDelete.Parameters["@itemGuideDelete"].Value=childDL.DataKeys[e.Item.ItemIndex];
            cmdDelete.ExecuteNonQuery();
      myConn.Close();
      ShowGuides();
}
</script>

<form runat="server">

<asp:DropDownList ID="SelectCategory" DataValueField="categoryID" DataTextField="categoryName" class="textfield_grey" AutoPostBack="true" OnSelectedIndexChanged="GetGuides" runat="server"/>

<asp:DataList ID="GuideHeadersList" HorizontalAlign="Center" Width="750" CellPadding="0" CellSpacing="1" runat="server">
       <ItemTemplate>
      <%#DataBinder.Eval(Container.DataItem,"guideHeaderName")%>
       </ItemTemplate>
  </asp:DataList>

         <asp:DataList ID="Guides"
      DataSource='<%#((DataRowView)Container.DataItem).Row.GetChildRows("GetGuides")%>'
      OnDeleteCommand="DeleteGuide"
      DataKeyField="guideID"
      runat="server">
                  
            <ItemTemplate>
                <%# DataBinder.Eval(Container.DataItem,"[\"guideID\"]")%>
                <%# DataBinder.Eval(Container.DataItem,"[\"guideTitle\"]")%>
                <asp:LinkButton ID="Delete" CommandName="Delete" CssClass="button_admin" Text="Delete" runat="server"/>
            </ItemTemplate>

</asp:DataList>
0
 
LVL 33

Expert Comment

by:raterus
ID: 12079345
I don't program in C#, but I know enough to say that you shouldn't be doing this..

<%# DataBinder.Eval(Container.DataItem,"[\"guideID\"]")%>
<%# DataBinder.Eval(Container.DataItem,"[\"guideTitle\"]")%>

it will physically be looking for a column named "guideID" <-- quotes include, take that out!

<%# DataBinder.Eval(Container.DataItem,"[guideID]")%>
<%# DataBinder.Eval(Container.DataItem,"[guideTitle]")%>

The brackets will always be enough to qualify it as a fieldname.

Ok that probably didn't help you at all, what is still broke?
--Michael

0
 
LVL 1

Author Comment

by:champ_010
ID: 12079648
I took out what you said but I get an error before whereas I didn't before.  I found that syntax somewhere because I couldn't get it to output the normal way.  Anyways the <%# DataBinder.Eval(Container.DataItem,"[\"guideID\"]")%> isn't what's causing my problem.

THe error message I get is:
DataBinder.Eval: 'System.Data.DataRow' does not contain a property with the name guideID.

It refers to this line of code:
GuideHeadersList.DataBind();

I think it is saying that "guideID" which I want for my DataKeyField can't be found in the DataTables of my DataSet--yet I did retrieve in my stored procedure....
0
 
LVL 1

Author Comment

by:champ_010
ID: 12079884
However, if I took the nested DataList out of the parent DataList and simply pasted underneath, there are no errors, the DataKeyField is Valid and I can delete my row.  So there is some problem identifying I think the DataKeyField when it is nested.
0
 
LVL 33

Expert Comment

by:raterus
ID: 12083803
As I said before, I don't think you are filling the DataKeyField correctly for the nested datalist.  If you were using Visual Studio you could at least Debug easily enough to verify this, I'm not sure how to best access these objects from inline script to see.  Sorry I can't be more of a help..

--Michael
0
 
LVL 1

Author Comment

by:champ_010
ID: 12087857
raterus,

I'm going to try to build the page another way.  I've encountered the DataKeyField with DataRelation and nested DataList before.  Will have to look into it more if I want to use this kind of thing often.

I'm going to ask Community Support to roll the points back to the original 150 accept your posting of  09/16/2004 01:16PM PDT for answering my original error problem. I had added points later for my second error.

Let me know if this is o.k. with you.

Thanks
0
 
LVL 33

Expert Comment

by:raterus
ID: 12087884
That's fine with me
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question