Note that there are some explanatory texts on larger screens.

plurals
  1. POhow can I set my datalist's datasource to datatable in run time
    text
    copied!<p>I have two data lists in my page and one of them is nested in the other one. I would like to give 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.</p> <p>I have searched a lot about it but I couldn't find useful information. Please help me.</p> <p>In the page I would like to show which user bought which products, 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. For showing the related products to each index number I used nested data list. In my first data list now my outer (parent) data list can show the information but the inner data list which should show the products information does not show any thing.</p> <p>I really need help</p> <p>This is my code:</p> <pre><code> &lt;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" /&gt; &lt;FooterStyle BackColor="#CCCCCC" /&gt;; &lt;HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" /&gt; &lt;ItemStyle BackColor="White" /&gt; &lt;ItemTemplate /&gt; &lt;fieldset&gt; &lt;legend&amp;gt&gt;customer information&amp;lt&lt;/legend&amp;gt&gt; &lt;table style="width: 100%;"/&gt; &lt;tr&gt; &lt;td&gt; &amp;nbsp;customer code: &lt;/td&gt; &lt;td&gt; &amp;nbsp;&lt;asp:Label ID="Label1" runat="server" Text='&lt;%# Eval("idFromUsers") %&gt;'&lt;/asp:Label&gt; &lt;/td&gt;; &lt;td&gt; &amp;nbsp;telephone: &lt;/td&gt; &lt;td&gt; &amp;nbsp;&lt;asp:Label ID="Label2" runat="server" Text='&lt;%# Eval("user_phone") %&gt;'&gt;&lt;/asp:Label&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; &amp;nbsp; customer name : &lt;/td&gt; &lt;td&gt; &amp;nbsp;&lt;asp:Label ID="Label3" runat="server" Text='&lt;%# Eval("user_name") %&gt;'&gt;&lt;/asp:Label&gt; &lt;/td&gt; &lt;td&gt; &amp;nbsp;cellphone number: &lt;/td&gt; &lt;td&gt; &amp;nbsp;&lt;asp:Label ID="Label4" runat="server" Text='&lt;%# Eval("user_cellphone") %&gt;'&gt;&lt;/asp:Label&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; &amp;nbsp; customer familly name : &lt;/td&gt; &lt;td&gt; &amp;nbsp;&lt;asp:Label ID="Label5" runat="server" Text='&lt;%# Eval("user_familly") %&gt;'&gt;&lt;/asp:Label&gt; &lt;/td&gt; &lt;td&gt; &amp;nbsp;email : &lt;/td&gt; &lt;td&gt; &amp;nbsp;&lt;asp:Label ID="Label6" runat="server" Text='&lt;%# Eval("user_email") %&gt;'&gt;&lt;/asp:Label&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/fieldset&gt; &lt;fieldset&gt; &lt;legend&gt;Index Information&lt;/legend&gt; &lt;table style="width: 100%;"&gt; &lt;asp:Label ID="hiddenLable" runat="server" Text='&lt;%# Eval("idfromIndex") %&gt;' Visible="false"&gt;&lt;/asp:Label&gt; &lt;tr&gt; &lt;td&gt; &amp;nbsp;Index number : &lt;/td&gt; &lt;td&gt; &amp;nbsp;&lt;asp:Label ID="Label7" runat="server" Text='&lt;%# Eval("IndexNO") %&gt;'&gt;&lt;/asp:Label&gt; &lt;/td&gt; &lt;td&gt; &amp;nbsp;date of index : &lt;/td&gt; &lt;td&gt; &amp;nbsp;&lt;asp:Label ID="Label8" runat="server" &gt;&lt;/asp:Label&gt; &lt;/td&gt; &lt;td&gt; &amp;nbsp;bank name: &lt;/td&gt; &lt;td&gt; &amp;nbsp;&lt;asp:Label ID="Label9" runat="server" Text='&lt;%# Eval("bankName") %&gt;'&gt;&lt;/asp:Label&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/fieldset&gt; &lt;fieldset&gt; &lt;legend&gt;purchased products&lt;/legend&gt; &lt;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" &gt; &lt;FooterStyle BackColor="#CCCCCC" /&gt; &lt;HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" /&gt; &lt;ItemStyle BackColor="White" /&gt; &lt;ItemTemplate &gt; &lt;table style="width: 100%;" id="tbl1"&gt;&lt;/table&gt; &lt;table style="width: 100%;"&gt; &lt;tr&gt; &lt;td&gt; &amp;nbsp;product name : &lt;/td&gt; &lt;td&gt; &amp;nbsp;&lt;asp:Label ID="Label10" runat="server" Text=""&gt;&lt;/asp:Label&gt; &lt;/td&gt; &lt;td&gt; &amp;nbsp;quantity: &lt;/td&gt; &lt;td&gt; &amp;nbsp;&lt;asp:Label ID="Label11" runat="server" Text=""&gt;&lt;/asp:Label&gt; &lt;/td&gt; &lt;td&gt; &amp;nbsp;unit price of each product : &lt;/td&gt; &lt;td&gt; &amp;nbsp;&lt;asp:Label ID="Label12" runat="server" Text=""&gt;&lt;/asp:Label&gt; &lt;/td&gt; &lt;td&gt; &amp;nbsp;Total Amount: &lt;/td&gt; &lt;td&gt; &amp;nbsp;&lt;asp:Label ID="Label13" runat="server" Text=""&gt;&lt;/asp:Label&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/ItemTemplate&gt; &lt;/asp:DataList&gt; &lt;/fieldset&gt; &lt;fieldset&gt;&lt;legend&gt;shopping cart Information&lt;/legend&gt; &lt;table style="width: 100%;"&gt; &lt;tr&gt; &lt;td &gt; &amp;nbsp;Total quantity : &lt;/td&gt; &lt;td&gt; &amp;nbsp;&lt;asp:Label ID="Label14" runat="server" Text=""&gt;&lt;/asp:Label&gt; &lt;/td&gt; &lt;td&gt; &amp;nbsp;total amount of shopping cart: &lt;/td&gt; &lt;td&gt; &amp;nbsp;&lt;asp:Label ID="Label15" runat="server" Text=""&gt;&lt;/asp:Label&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; &amp;nbsp;date of shopping :&lt;/td&gt; &lt;td dir="rtl"&gt;&amp;nbsp&lt;asp:Label ID="Label16" runat="server" Text='&lt;%# Eval("orderDate") %&gt;' /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/fieldset&gt; &lt;asp:LinkButton ID="LinkButton1" runat="server" CommandName="confirm"&gt;confirm&lt;/asp:LinkButton&gt; &amp;nbsp; &lt;asp:LinkButton ID="LinkButton2" runat="server" CommandName="notconfirm"&gt;not confirm&lt;/asp:LinkButton&gt; &lt;/ItemTemplate&gt; &lt;SelectedItemStyle BackColor="#000099" Font-Bold="True" ForeColor="White" /&gt; &lt;/asp:DataList&gt; 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 &amp;lt; 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]; } 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(); dataset t = setinnerDataList(idfromindex); tb2 = t; int sumqty = 0; double sumprice = 0; for (int i = 0; i &amp;lt; 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(); Label pname = (Label)lst2.FindControl("pname"); Label qty = (Label)lst2.FindControl("qty"); Label price = (Label)lst2.FindControl("unit_price"); DataRow[] dr = t.Tables[0].Select("idfromIndex = '" + idfromindex + "'");//new DataRow[t.Tables[0].Rows.Count]; //= //This will select all rows where the Name Column has value MAK for (int i = 0; i &lt; t.Tables[0].Rows.Count; i++) { dr[i] = t.Tables[0].Rows[i]; } for (int i = 0; i &lt; dr.Length; i++) { pname.Text = dr[i]["pname"].ToString(); price.Text = dr[i]["p_price"].ToString(); qty.Text = dr[i]["qty"].ToString(); } } } private dataset 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(); dataset dt = new DataTable(); try { myConnection.Open(); SqlDataReader dr = cmd.ExecuteReader(); dt.Load(dr); dr.Close(); } catch (Exception EXP) { } finally { myConnection.Close(); } return dt; }&lt;/pre&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload