Solved

Accessing Nested DataList

Posted on 2004-09-16
13
3,173 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
  • 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
 
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now