Solved

Accessing Nested DataList

Posted on 2004-09-16
13
3,178 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

810 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