Click here to Skip to main content
15,914,419 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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(strconn);
        con.Open();
        SqlDataAdapter da1 = new SqlDataAdapter("select  *  from SERVICES", con);
        SqlDataAdapter da2 = new SqlDataAdapter("select  *  from SUB_SERVICES as sub,SERVICES as services where sub.VOY_SERVICES_SERVICE_ID = services.VOY_SERVICES_SERVICE_ID", con);
   DataSet ds2 = new DataSet();
        da1.Fill(ds2, "SERVICES");

        da2.Fill(ds2, " SUB_SERVICES");

ds.Relations.Add("servicessel", ds.Tables["SERVICES"].Columns["VOY_SERVICES_SERVICE_ID"], ds.Tables["SUB_SERVICES"].Columns["VOY_SERVICES_SERVICE_ID"]);
      ds.Relations[0].Nested = true;
        ds.Relations.Add(subservices, ds.Tables["SUB_SERVICES"].Columns["VOY_SUB_SERVICES_SERVICE_ID"], ds.Tables["SUB_SERVICES_CATEGORIES"].Columns["VOY_SUB_SERVICES_SERVICE_ID"]);
        ds.Relations[1].Nested = true;
 services_repeater.DataSource = ds2;
        services_repeater.DataBind();
con.Close();

in
C#
ds.Tables["SUB_SERVICES"].Columns["VOY_SERVICES_SERVICE_ID"]

its showing an error "Object reference not set to an instance of an object", please help me to over come this problem asap.
Posted
Updated 18-Jun-12 0:10am
v2
Comments
shabadiveda 18-Jun-12 7:46am    
thanx for the reply..,i changed ds to ds2, but still am getting the same error.

Object reference not set to an instance of an object

This error happens when you try to use a property or call a method of an object that is null. More details: here[^]

A simple use of Visual studio DEBUGGER can tell you the object because of which it is happening. Just look at the stack trace and put a debugger on that line. Check the objects of that line and see if any one is null and you are trying to use that objects property. Handle the same.

Based on what you say:
C#
ds.Tables["SUB_SERVICES"].Columns["VOY_SERVICES_SERVICE_ID"]

This means either ds is null or ds.Tables["SUB_SERVICES"] is null or ds.Tables["SUB_SERVICES"].Columns["VOY_SERVICES_SERVICE_ID"] is null. If you have data, check for table name typo or column name typo.
 
Share this answer
 
Comments
shabadiveda 19-Jun-12 0:22am    
thanx for the reply...i wil c to it.
Manas Bhardwaj 19-Jun-12 6:29am    
Well explained +5
Hi shabadiveda,

You have added Both of your Datatable at ds2and Build relation On ds.

I think you have done a silly mistake of typing wrong variable name or mix up with other variable name.
 
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