Click here to Skip to main content
15,881,204 members

How can I set my datalist 's datasource to datatable at runtime

elmirag asked:

Open original thread
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
Tags: ASP.NET

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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