Click here to Skip to main content
15,905,028 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello,

I am having a dataset in which am having 2 tables,i want to build relation between them. my 2 tables are as listed below.
SQL
CREATE TABLE SERVICES
(VOY_SERVICES_SERVICE_ID INT PRIMARY KEY IDENTITY(1,1),
VOY_SERVICES_SERVICE_NAME VARCHAR(50))
 
CREATE TABLE SUB_SERVICES
(VOY_SUB_SERVICES_SERVICE_ID INT PRIMARY KEY IDENTITY(1,1),
VOY_SERVICES_SERVICE_ID INT FOREIGN KEY REFERENCES SERVICES (VOY_SERVICES_SERVICE_ID),
VOY_SUB_SERVICES_SERVICE_NAME VARCHAR(50))

i tried this code,
C#
 SqlConnection con = new SqlConnection( "Data Source=USER-PC\\SQLEXPRESS;Initial Catalog=VOY;Integrated Security=True");
        con.Open();
      
        SqlDataAdapter da = new SqlDataAdapter("select  *  from SERVICES;select  *  from SUB_SERVICES as sub,SERVICES as services where sub.VOY_SERVICES_SERVICE_ID = services.VOY_SERVICES_SERVICE_ID", con);

da.TableMappings.Add("Table";, "SERVICES");
       da.TableMappings.Add("Table1";, "SUB_SERVICES");

 DataSet ds2 = new DataSet();
        da.Fill(ds2);
ds2.Relations.Add(ds2.Tables["SERVICES"].Columns["VOY_SERVICES_SERVICE_ID"],
          ds2.Tables["SUB_SERVICES"].Columns["VOY_SERVICES_SERVICE_ID"]);
          ds2.Relations[0].Nested = true;
          ds2.Relations.Add("subservices", ds2.Tables[" SUB_SERVICES"].Columns["VOY_SUB_SERVICES_SERVICE_ID"], ds2.Tables["SUB_SERVICES_CATEGORIES "].Columns["VOY_SUB_SERVICES_SERVICE_ID"]);
          ds2.Relations[1].Nested = true;

C#
services_repeater.DataSource = ds2;
        services_repeater.DataBind();
        con.Close();



am getting the error as "Object reference not set to an instance of an object" at
ds2.Tables["SUB_SERVICES"].Columns["VOY_SERVICES_SERVICE_ID"]) please help me. am using the repeaters so that i can get the chechboxes in nested format, and my .aspx page is as shown,
XML
<asp:Repeater id="services_repeater" runat="server" EnableViewState="false">
    <ItemTemplate>
    Services:<%#DataBinder.Eval(Container.DataItem,"VOY_SERVICES_SERVICE_ID") %> &nbsp;&nbsp;
    <%#DataBinder.Eval(Container.DataItem,"VOY_SERVICES_SERVICE_NAME") %>
    <br />
    <asp:Repeater runat="server" EnableViewState="false" DataSource='<%# GetChildRelation(Container.DataItem,"SERVICES") %>'>
    <ItemTemplate>
     &nbsp;&nbsp; &nbsp;&nbsp;
     SubserviceID:<b><%#DataBinder.Eval(Container.DataItem,"VOY_SUB_SERVICES_SERVICE_ID")%>  </b>
     <br />
     <asp:Repeater runat="server" EnableViewState="false" DataSource='<%#GetChildRelation (Container.DataItem,"SUB_SERVICES")%>'>
     <ItemTemplate>
       &nbsp;&nbsp; &nbsp;&nbsp;
         &nbsp;&nbsp; &nbsp;&nbsp;
         <b><%#DataBinder.Eval(Container.DataItem,"VOY_SUB_SERVICES_CATEGORIES_NAME") %></b>
     </ItemTemplate>
     </asp:Repeater>
    </ItemTemplate>
    </asp:Repeater>
    </ItemTemplate>
    </asp:Repeater>

any solution is welcome.
Posted
Updated 18-Jun-12 3:52am
v2
Comments
Nilesh Patil Kolhapur 18-Jun-12 10:00am    
user stored procedure instead of inline query
Sandeep Mewara 18-Jun-12 11:13am    
Why repost?

1 solution

Hi, I think you r doing a mistake. I think your parent table is services. and your child table is Sub_Services. for making a relation on these two tables, u should use a common column od both tables i.e. VOY_SERVICES_SERVICE_ID.
but here u have used VOY_SUB_SERVICES_SERVICE_ID. so, i think you should add this line.....

ds2.Relations.Add("subServices", ds2.Tables["SERVICES"].Columns["VOY_SERVICES_SERVICE_ID"], ds2.Tables["SUB_SERVICES"].Columns["VOY_SERVICES_SERVICE_ID"]);

use it. I hope your problem will be resolved efficiently.

Thanks
 
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