Click here to Skip to main content
15,896,453 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
I have two data lists in my page and one of them is nested in the other one.
I would like to set their data sources in run time during page load event, but my inner data list doesn't show the value of the data table and I don't know how to set my inner data lists data source to data table in run time.

I have searched a lot about it but I couldn't find useful information.
Please help me.

In the page I would like to show which user bought which products and show the quantity and price of each product and show some information about paid index, Like index number or index bank name.
The problem is, for one index number I can have several products and I dont know the exact number of products of each index beacause of that I used inner datalist to show products information which are related to the same index and I did not put any control in my innerdatalist because I dont know the exact number of products for each index so I have to set my innerdatalist datasource to datatable at runtime but my inner datalist only show the empty table.
but the (parent)datalist works well

I really need help

This is my code:
ASP.NET
<asp:DataList ID="outerDataLiat" runat="server" RepeatColumns="1" 
                BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" 
                CellPadding="4" CellSpacing="2" ForeColor="Black" GridLines="Both" DataKeyField="idfromIndex" OnItemDataBound="DataList1_ItemDataBound" >
                <FooterStyle BackColor="#CCCCCC"  />
                <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
            <ItemStyle BackColor="White" />
            <ItemTemplate >              
 <fieldset>
    
      <legend>customer information</legend>
      <table style="width: 100%;">
        <tr>
        <td>
          customer code:
        </td>
        <td>
                         <asp:Label ID="Label1" runat="server" Text='<%# Eval("idFromUsers") %>'></asp:Label>
                    </td>
                    
                     <td>
                         telephone:
                    </td>
                     <td>
                         <asp:Label ID="Label2" runat="server" Text='<%# Eval("user_phone") %>'></asp:Label>
                    </td>

                </tr>
                <tr>
                    <td>
                          customer name:
                    </td>
                    <td>
                         <asp:Label ID="Label3" runat="server" Text='<%# Eval("user_name") %>'></asp:Label>
                    </td>
                    <td>
                         customer cellphone number:
                    </td>
                    <td>
                         <asp:Label ID="Label4" runat="server" Text='<%# Eval("user_cellphone") %>'></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td>
                          customer familly:
                    </td>
                    <td>
                         <asp:Label ID="Label5" runat="server" Text='<%# Eval("user_familly") %>'></asp:Label>
                    </td>
                    <td>
                         email :
                    </td>
                    <td>
                        <asp:Label ID="Label6" runat="server" Text='<%# Eval("user_email") %>'></asp:Label>
                    </td>
                </tr>
                
            </table>

        </fieldset>
        <fieldset>
        <legend>index card information</legend>
          <table style="width: 100%;">
              <asp:Label ID="hiddenLable" runat="server" Text='<%# Eval("idfromIndex") %>' Visible="false"></asp:Label>
                <tr>
                    <td>
                         index number :
                    </td>
                    <td>
                         <asp:Label ID="Label7" runat="server" Text='<%# Eval("IndexNO") %>'></asp:Label>
                    </td>
                    <td>
                         index date :
                    </td>
                    <td>
                         <asp:Label ID="Label8" runat="server" ></asp:Label>
                    </td>
                    <td>
                         bank name:
                    </td>
                    <td>
                         <asp:Label ID="Label9" runat="server" Text='<%# Eval("bankName") %>'></asp:Label>
                    </td>
                </tr>
               
            </table>

        </fieldset>
        <fieldset>
        <legend>purchased Products</legend>
        <asp:DataList ID="innerDataLiat" runat="server" RepeatColumns="1" 
                BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" 
                CellPadding="4" CellSpacing="2" ForeColor="Black" GridLines="Both" OnItemDataBound="DataList2_ItemDataBound"  >
                <FooterStyle BackColor="#CCCCCC"  />
                <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
                <ItemStyle BackColor="White" />
            <ItemTemplate >
            <table style="width: 100%;" id="tbl1"></table>
          
                       </ItemTemplate> 

            </asp:DataList>
         
        </fieldset>
        <fieldset><legend>shopping cart information</legend>
            <table style="width: 100%;">
                <tr>
                    <td >
                         Total Quantity of purchased products :
                    </td>
                    <td>
                         <asp:Label ID="Label14" runat="server" Text=""></asp:Label>
                    </td>
                  <td>
                         Total price:
                    </td>
                    <td>
                         <asp:Label ID="Label15" runat="server" Text=""></asp:Label>
                    </td>
                    
                </tr>
                <tr>
                <td>  date of purchase:</td>
                <td dir="rtl"> <asp:Label ID="Label16" runat="server" Text='<%# Eval("orderDate") %>'  /></td>
                </tr>
                
            </table>
            </fieldset>

           
              <asp:LinkButton ID="LinkButton1" runat="server" CommandName="confirm">confirm index</asp:LinkButton>
               
              <asp:LinkButton ID="LinkButton2" runat="server" CommandName="notconfirm">not confirm index</asp:LinkButton>
            </ItemTemplate>

                <SelectedItemStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />

            </asp:DataList>


This is my C# code:
C#
  protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            getDistinctOrderId();
            doPaging();
        }     
    }
 private void getDistinctOrderId()
    {
        SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["mobile_storeConnectionString"].ConnectionString);
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = myConnection;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "SELECT min(id), [idfromIndex] FROM Orders WHERE [idfromIndex] IN(SELECT DISTINCT [idfromIndex] FROM Orders) GROUP BY [idfromIndex] ";
        DataView MyDv = new DataView();

        try
        {
            myConnection.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            DataTable dt = new DataTable();
            dt.Load(dr);
            MyDv = dt.DefaultView;
            for (int i = 0; i < MyDv.Count; i++)
            {
                if (i == MyDv.Count - 1)
                {
                    distinctList += MyDv[i][0].ToString();
                }
                else
                    distinctList += MyDv[i][0].ToString() + ",";
            }
           
        }
        catch (Exception EXP)
        {
        }
        finally
        { myConnection.Close(); }
    }
    public DataTable getTheData()
    {
       
     DataSet DS = new DataSet();
     SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString1"].ConnectionString);
            SqlDataAdapter objSQLAdapter = new SqlDataAdapter("select idfromIndex,user_name,user_email,indexNO,bankName,Ispay,user_familly,user_cellphone,orderDate,user_phone,idFromUsers,orderId from factors where orderId IN (" + distinctList + ")", myConnection); objSQLAdapter.Fill(DS, "mobile_store");
          
        return DS.Tables[0];
    }
//this method is for paging my outer datalist
 void doPaging()
    {
        //DataTable mydatatable = 
        pagedData.DataSource = getTheData().DefaultView;
        pagedData.AllowPaging = true;
        pagedData.PageSize = 4;

     /* 
     ...
       here I wrote some code to make my outer datalist pagable
     ...
    */
       

        outerDataLiat.DataSource = pagedData;
        outerDataLiat.DataBind();

    }

  protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
        {
            Label lbl = (Label)e.Item.FindControl("hiddenLable");
            string idfromindex = lbl.Text;
            DataList lst2 = (DataList)e.Item.FindControl("innerDataLiat");
            SqlDataSource ds = new SqlDataSource();
            DataTable mydt = new DataTable();
        
            DataTable t = setinnerDataList(idfromindex);
            tb2 = t;
            int sumqty = 0;
            double sumprice = 0;
            for (int i = 0; i < t.Rows.Count; i++)
            {
                sumprice += Convert.ToDouble(t.Rows[i]["p_price"]) * Convert.ToInt32(t.Rows[i]["qty"]);
                 sumqty += Convert.ToInt32(t.Rows[i]["qty"]);
            }
            Label lbl14 = (Label)e.Item.FindControl("Label14");
            lbl14.Text = sumqty.ToString();
            Label lbl15 = (Label)e.Item.FindControl("Label15");
            lbl15.Text = sumprice.ToString();
           
            lst2.DataSource = t;
            lst2.DataBind();
           
        }
    }

private DataTable setinnerDataList(string idfromindex)
    {
        
        SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["mobile_storeConnectionString"].ConnectionString);
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = myConnection;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "SELECT [pname], [p_price], [idfromIndex], [qty] FROM [factors] WHERE ([idfromIndex] = " + idfromindex + ") ";
        DataView MyDv = new DataView();
        DataTable dt = new DataTable();
        try
        {
            myConnection.Open();
            SqlDataReader dr = cmd.ExecuteReader();
           
            dt.Load(dr);
            dr.Close();

        }
        catch (Exception EXP)
        {


        }
        finally
        { myConnection.Close(); }
       
        return dt;
    }


Is there any one to help me I really need help
Posted
Updated 25-Dec-12 23:33pm
v5

I think you are not getting proper datasource for inner datalist. debug it properly.

Check this code:
XML
<asp:DataList runat="server" ID="dl1" OnItemDataBound="dl1_ItemDataBound">
    <ItemTemplate>
        <table width="100%" border="1">
            <tr>
                <td>'<%#Eval("first") %>'</td>
                <td>'<%#Eval("last") %>'</td>
            </tr>
        </table>
        <asp:DataList runat="server" ID="dl2">
            <ItemTemplate>
                <table width="100%" border="1">
                    <tr>
                        <td>'<%#Eval("d1") %>'</td>
                    </tr>
                </table>
            </ItemTemplate>
        </asp:DataList>
    </ItemTemplate>
</asp:DataList>



C#
dl1.DataSource = Getdl1();
        dl1.DataBind();


private DataTable Getdl1()
{
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[] { new DataColumn("first"), new DataColumn("last") });
    dt.Rows.Add("first1", "last1");
    dt.Rows.Add("first2", "last2");
    dt.Rows.Add("first3", "last3");
    dt.Rows.Add("first4", "last4");
    dt.Rows.Add("first5", "last5");
    return dt;
}
private DataTable Getdl2()
{
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[] { new DataColumn("d1") });
    dt.Rows.Add("Inner" + (i++).ToString());
    dt.Rows.Add("Inner" + (i++).ToString());
    dt.Rows.Add("Inner" + (i++).ToString());
    dt.Rows.Add("Inner" + (i++).ToString());
    dt.Rows.Add("Inner" + (i++).ToString());
    return dt;
}
protected void dl1_ItemDataBound(object sender, DataListItemEventArgs e)
{
    DataList dl = (DataList)e.Item.FindControl("dl2");
    dl.DataSource = Getdl2();
    dl.DataBind();
}
 
Share this answer
 
it works well when I change my code,This is edited code:

protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
{
DataList dls1 = (DataList)sender;
Label lbl = (Label)e.Item.FindControl("hiddenLable");
string idfromindex = lbl.Text;
DataList lst2 = (DataList)e.Item.FindControl("innerDataLiat");
Panel pnl = (Panel)e.Item.FindControl("Panel1");
t = setinnerDataList(idfromindex);
int sumqty = 0;
double sumprice = 0;


for (int i = 0; i < t.Tables[0].Rows.Count; i++)
{
sumprice += Convert.ToDouble(t.Tables[0].Rows[i]["p_price"]) * Convert.ToInt32(t.Tables[0].Rows[i]["qty"]);
sumqty += Convert.ToInt32(t.Tables[0].Rows[i]["qty"]);

}
Label lbl14 = (Label)e.Item.FindControl("Label14");
lbl14.Text = sumqty.ToString();
Label lbl15 = (Label)e.Item.FindControl("Label15");
lbl15.Text = sumprice.ToString();
lst2.DataSource = t;
lst2.DataBind();
}
}

ASP.NET
<asp:DataList ID="innerDataLiat" runat="server" RepeatColumns="1" 
            BackColor="#CCCCCC" 
            CellPadding="4" CellSpacing="2" ForeColor="Black" GridLines="Both" OnItemDataBound="DataList2_ItemDataBound"  >
            <FooterStyle BackColor="#CCCCCC"  />
            <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
            <itemstyle backcolor="White" />
        <itemtemplate>
        <table style="width: 100%;"></table>
        <table style="width: 100%;" id="tbl1">
            <tr>
                <td>
                     name :
                </td>
                <td>
                     <asp:label id="pname" runat="server" text="<%# Eval("pname") %>" xmlns:asp="#unknown"></asp:label>
                </td>
                <td>
                     quantity:
                </td>
                <td>
                     <asp:label id="qty" runat="server" text="<%# Eval("qty") %>" xmlns:asp="#unknown"></asp:label>
                </td>
                <td>
                     unit_price:
                </td>
                <td>
                     <asp:label id="unit_price" runat="server" text="<%# Eval("p_price") %>" xmlns:asp="#unknown"></asp:label>
                </td>

            </tr>


        </table>
                   </itemtemplate> 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900