0

I'm starting in ASP.NET. I need to write data from two tables that are linked via ID. I would like to write in the cycle. Example: I have a table States and Cities table: 1.United states     a. New York     b. Washington     c. Los Angeles     d. Chicago     e. Houston 2. Russia     a. Moscow     b. St. Petersburg     c. Omsk     d. Kazan 3. France     a. Paris     b. Lyon     c. Marseille

In PHP I solve this problem as follows `

// cycle cities
$sql = mssql_query("SELECT * FROM States");
while($row = mssql_fetch_assoc($sql))
{
   $id_state = row['ID_State'];
   echo ($ row ['Name_State']);
// cycle city with id state
   $sql_city = mssql_query("SELECT * FROM City WHERE ID_State = '. $id_state.'");
   while ($row_city = mssql_fetch_assoc($sql_city))
   {
      echo($row['Name_city']);
   }
}

Alternatively, just advice on how the operation is called, I do not know how to properly ask this in search

I tried Repeater in repeater but i have problem withI tried repeater in repeater, but I have a problem with passing parameters given line Repeater in Repeater

I tried also in DataReader DataReader, but this is an open DataReader reports an error. C# MySQL second DataReader in DataReader while loop

I also tried a treeview, but I am in the city must have a name and the name of the State, the same "name" and "name" which I can not have this. Treeview validation

I now tried good code

select.aspx.cs

SqlDataAdapter cmd1 = new SqlDataAdapter("SELECT * FROM V_Dic", cnn);
     //Create and fill the DataSet.
     DataSet ds = new DataSet();
     cmd1.Fill(ds, "Dic");

    //Create a second DataAdapter for the Titles table.
    SqlDataAdapter cmd2 = new SqlDataAdapter("select * from T_Obdobi", cnn);
    cmd2.Fill(ds, "Obdobi");

    //Create a second DataAdapter for the Titles table.
    SqlDataAdapter cmd3 = new SqlDataAdapter("select  * from V_AktualizaceDic", cnn);
    cmd3.Fill(ds, "OsCislo");

    //Create the relation bewtween the Authors and Titles tables.
    ds.Relations.Add("Obdobi",
    ds.Tables["Dic"].Columns["ID_Dic"],
    ds.Tables["Obdobi"].Columns["ID_Dic"]);

    //ds.Relations.add


    ds.Relations.Add("OsCislo",
    ds.Tables["Dic"].Columns["ID_Dic"],
    ds.Tables["OsCislo"].Columns["ID_Dic"]);

    //Bind the Authors table to the parent Repeater control, and call DataBind.
    parentRepeater.DataSource = ds.Tables["Dic"];
    Page.DataBind();

select.aspx

<asp:Repeater ID="parentRepeater" runat="server">
        <ItemTemplate>
            <div class="accordion">
                <div class="hlavni">
                    <%# DataBinder.Eval(Container.DataItem,"Dic") %>
                </div>
                <div class="rozbalovany">

                    <table>
                        <tr>
                            <td>Osobní číslo: </td>
                            <td><%# DataBinder.Eval(Container.DataItem,"OsCislo") %></td>

                        </tr>
                        <tr>
                            <td>Šetřené zdaňovací období: </td>
                            <td>
                                <div class="box2">
                                    <!-- start child repeater -->
                                    <asp:Repeater ID="childRepeater" DataSource='<%# ((System.Data.DataRowView)Container.DataItem).Row.GetChildRows("Obdobi") %>'
                                        runat="server">

                                        <ItemTemplate>

                                            <%# DataBinder.Eval(Container.DataItem, "[\"Obdobi\"]")%><br />
                                        </ItemTemplate>
                                    </asp:Repeater>
                                </div>
                            </td>
                        </tr>
                    </table>
                    <h3>Aktualizace:</h3>
                    <!-- end child repeater -->
                    <div class="box">
                        <table class="aktualizace" style="border: 1px solid #e9e9e9">
                            <tr>
                                <td><strong>Osobní číslo</strong></td>
                                <td>Jméno a Příjmení</td>
                                <td>Datum Aktualizace</td>
                                <td>Poznámka</td>
                                <td>Šetřené zdaňovací období</td>
                                <td>Změna Řešitele</td>
                                <td>Změna Plné moci</td>
                                <td>Záznam v Insolvenčním řejstříku</td>
                                <td>Významná změna s vazbou na data v OR</td>
                                <td>Jiná významná změna</td>
                            </tr>

                            <asp:Repeater ID="childRepeater2" DataSource='<%# ((System.Data.DataRowView)Container.DataItem).Row.GetChildRows("OsCislo") %>'
                                runat="server">

                                <ItemTemplate>
                                    <tr>
                                        <td><%# DataBinder.Eval(Container.DataItem, "[\"OsCislo\"]")%></td>
                                    </tr>
                                </ItemTemplate>
                            </asp:Repeater>
                        </table>
                    </div>
                </div>
            </div>
        </ItemTemplate>
    </asp:Repeater>

but, I can't add parameter to first select. I tried

cmd1.SelectCommand.Parameters.Add("@EvCislo", SqlDbType.NVarChar, 50, "EvCislo");

but program have error message "this constraint cannot be enabled as not all values have corresponding parent values." this example i see here

Community
  • 1
  • 1
Tom Trnka
  • 56
  • 4

2 Answers2

0

You can use JOINS for fetch data from two tables using ID like

SELECT table1.* FROM table1 LEFT JOIN table2 ON table1.id = table2.id WHERE table1.id = '1';
Sateesh
  • 1,327
  • 9
  • 12
  • Thanks but i think Thank you, but I think your question back to the table **1. row** United states New York **2. row** United states Washington **3. row** United states Los Angeles **4. row** United states Chicago ... **next row** Russia Moscow **next** Russia St. Petersburg but i need `

    United states

    new york

    washington

    Russia

    Moscow

    Omsk

    `
    – Tom Trnka Mar 27 '15 at 11:05
  • can you send me structure of both tables? i am not getting you. – Sateesh Mar 27 '15 at 11:19
  • Thank's for you Answer, but I havent problem witch my database. I need cycle in my asp.net aplication. Now I need add any "where" in my databaze ask. If I add Where to code `SqlDataAdapter cmd1 = new SqlDataAdapter("SELECT * FROM V_Dic", cnn);` to my code, aplication write error message "this constraint cannot be enabled as not all values have corresponding parent values.". Can you help me with this? – Tom Trnka Apr 01 '15 at 06:52
0

I have answer, add "false" behin relations

SqlDataAdapter cmd1 = new SqlDataAdapter("SELECT * FROM V_Dic", cnn);
 //Create and fill the DataSet.
 DataSet ds = new DataSet();
 cmd1.Fill(ds, "Dic");

//Create a second DataAdapter for the Titles table.
SqlDataAdapter cmd2 = new SqlDataAdapter("select * from T_Obdobi", cnn);
cmd2.Fill(ds, "Obdobi");

//Create a second DataAdapter for the Titles table.
SqlDataAdapter cmd3 = new SqlDataAdapter("select  * from V_AktualizaceDic", cnn);
cmd3.Fill(ds, "OsCislo");

//Create the relation bewtween the Authors and Titles tables.
ds.Relations.Add("Obdobi",
ds.Tables["Dic"].Columns["ID_Dic"],
ds.Tables["Obdobi"].Columns["ID_Dic"], false);

//ds.Relations.add


ds.Relations.Add("OsCislo",
ds.Tables["Dic"].Columns["ID_Dic"],
ds.Tables["OsCislo"].Columns["ID_Dic"], false);

//Bind the Authors table to the parent Repeater control, and call DataBind.
parentRepeater.DataSource = ds.Tables["Dic"];
Page.DataBind();
Tom Trnka
  • 56
  • 4