Solved

OBJECT REFERENCE WAS NOT SET

Posted on 2009-04-07
21
215 Views
Last Modified: 2013-12-17
Hi I am getting the above error
dsShipper_master = new DataSet();
 dsShipper_master = objShipperInfo.GetShipperInfo_MasterMoreContact();
dsShipper_master.Relations.Add("Root", dsShipper_master.Tables["TMSShipper_Master"].Columns["ShipperId"], dsShipper_master.Tables["TMSShipper_MasterMoreContact"].Columns["ShipperId"]);

I need to set the relation.What sholud be change?
0
Comment
Question by:johny_bravo1
  • 12
  • 7
  • 2
21 Comments
 
LVL 8

Author Comment

by:johny_bravo1
ID: 24094388
I just missed one line,
 dsShipper_master = objShipperInfo.GetShipperInfo_Master();

GetShipperInfo_Master() is the method which retrives the data from the Stored procedure
0
 
LVL 8

Author Comment

by:johny_bravo1
ID: 24094412
If I set index 0 and 1,rather than giving table names,
I am getting an error "Cannot find table 1."
0
 
LVL 14

Assisted Solution

by:GiftsonDJohn
GiftsonDJohn earned 500 total points
ID: 24094429
Hi,

If you are filling the DataSet, though the table names are there in sql statements, the application will add like Table, Table1, Table2. What you can do is to provide table mapping while populating dataset using data adapter
 
     SqlDataAdapter sqlAdp = new SqlDataAdapter(SqlCmd, SqlCon);
     sqlAdp.TableMappings.Add("Table", "TMSShipper_Master");
     sqlAdp.TableMappings.Add("Table1", "TMSShipper_MasterMoreContact");

or After filling the DataSet

DataSet1.Tables[0].TableName="TMSShipper_Master";
DataSet1.Tables[1].TableName="TMSShipper_MasterMoreContact";

0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 24094437
how many tables are there in dsShipper_master
0
 
LVL 8

Author Comment

by:johny_bravo1
ID: 24094459
Hi I am doing it like this,
        dsShipper_master = objShipperInfo.GetShipperInfo_Master();
            dsShipper_master.Tables[0].TableName = "TMSShipper_Master";
            dsShipper_master = objShipperInfo.GetShipperInfo_MasterMoreContact();
            dsShipper_master.Tables[1].TableName = "TMSShipper_MasterMoreContact";

on the last line I am getting error "Cannot find table 1",but I am having data in the 3rd line.
0
 
LVL 8

Author Comment

by:johny_bravo1
ID: 24094468
GetShipperInfo_Master will fetch data from shipper_master table
and GetShipperInfo_MasterMoreContact is fetching data from shipper_morecontact table.
0
 
LVL 8

Author Comment

by:johny_bravo1
ID: 24094534
Here is the code for fetching data.
public DataSet GetShipperInfo_Master()

        {

            DataSet dsShipper = new DataSet();

            SqlParameter[] storedParams = new SqlParameter[1];

            storedParams = Helpers.SqlHelper.GetParams("getShipperInfo_Master");

            dsShipper = ExecuteDataset("getShipperInfo_Master ", storedParams);

            return dsShipper;

        }
 
 
 
 

 public static DataSet ExecuteDataset(string proc, params SqlParameter[] commandParameters)

        {

            DataSet ds = new DataSet();

            SqlConnection cn = new SqlConnection(connectionString);

            try

            {

                using (cn)

                {

                    // create and prepare command

                    SqlCommand cmd = new SqlCommand(proc, cn);

                    cmd.CommandTimeout = 600000;

                    cmd.CommandType = CommandType.StoredProcedure;

                    if (commandParameters != null)

                    {

                        cmd.Parameters.AddRange(commandParameters);

                    }

                    //SqlHelper.PrepareCommand(cmd, cn, proc, commandParameters);

                    // create data adapter, add table mappings and set selectcommand property

                    SqlDataAdapter sqlDa = new SqlDataAdapter(proc, cn);

                    sqlDa.SelectCommand = cmd;

                    sqlDa.Fill(ds);

                    return ds;

                }

            }

            catch (Exception ex)

            {

                //LogError(cmd, ex);

                cn.Close();

                throw ex;

            }

            finally

            {

                cn.Close();

            }
 

        }

Open in new window

0
 
LVL 14

Expert Comment

by:GiftsonDJohn
ID: 24094550
No no you should not do like that. It will overwrite the previous filled tables.

instead of doing like this

             dsShipper_master = objShipperInfo.GetShipperInfo_Master();
            dsShipper_master.Tables[0].TableName = "TMSShipper_Master";
            dsShipper_master = objShipperInfo.GetShipperInfo_MasterMoreContact();
            dsShipper_master.Tables[1].TableName = "TMSShipper_MasterMoreContact";

do as
           dsShipper_master = new DataSet();
           objShipperInfo. GetShipperInfo_Master(ref dsShipper_master);
           objShipperInfo.GetShipperInfo_MasterMoreContact(ref dsShipper_master);

inside the GetShipperInfo_Master function
 
          public void GetShipperInfo_Master(ref DataSet ds)
         {
              .....
              SqlAdp.Fill(ds, "TMSShipper_Master");
         }
           
inside GetShipperInfo_MasterMoreContact function

       public void GetShipperInfo_MasterMoreContact(ref DataSet ds)
       {
          ....
          SqlAdp.Fill(ds, "TMSShipper_MasterMoreContact");
       }
0
 
LVL 8

Author Comment

by:johny_bravo1
ID: 24094609
Ok I have changed the code accordingly.

Table names  are set accordingly.
Let me describe you in detail.
dsShipper_master = objShipperInfo.GetShipperInfo_Master();
calls GetShipperInfo_Master(); on other page
it;s def.
public DataSet GetShipperInfo_Master()
        {
            DataSet dsShipperInfo = objShipper.GetShipperInfo_Master();
            return dsShipperInfo;
        }

in this  DataSet dsShipperInfo = objShipper.GetShipperInfo_Master();
 call other page
public DataSet GetShipperInfo_Master()
        {
            DataSet dsShipper = new DataSet();
            SqlParameter[] storedParams = new SqlParameter[1];
            storedParams = Helpers.SqlHelper.GetParams("getShipperInfo_Master");
            dsShipper = Helpers.SqlHelper.ExecuteDataset1("getShipperInfo_Master ","TMSShipper_Master", storedParams);
            return dsShipper;
        }

from this ExecuteDataset1 on other page is called.I have change it according to you.
 
Now please tell me where should I change.


public static DataSet ExecuteDataset1(string proc,string tablename, params SqlParameter[] commandParameters)

        {

            DataSet ds = new DataSet();

            SqlConnection cn = new SqlConnection(connectionString);

            try

            {

                using (cn)

                {

                    // create and prepare command

                    SqlCommand cmd = new SqlCommand(proc, cn);

                    cmd.CommandTimeout = 600000;

                    cmd.CommandType = CommandType.StoredProcedure;

                    if (commandParameters != null)

                    {

                        cmd.Parameters.AddRange(commandParameters);

                    }

                    //SqlHelper.PrepareCommand(cmd, cn, proc, commandParameters);

                    // create data adapter, add table mappings and set selectcommand property

                    SqlDataAdapter sqlDa = new SqlDataAdapter(proc, cn);

                    sqlDa.SelectCommand = cmd;

                    sqlDa.Fill(ds,tablename);

                    return ds;

                }

            }

            catch (Exception ex)

            {

                //LogError(cmd, ex);

                cn.Close();

                throw ex;

            }

            finally

            {

                cn.Close();

            }
 

        }
 

public DataSet GetShipperInfo_Master()

        {

            DataSet dsShipper = new DataSet();

            SqlParameter[] storedParams = new SqlParameter[1];

            storedParams = Helpers.SqlHelper.GetParams("getShipperInfo_Master");

            dsShipper = ExecuteDataset1("getShipperInfo_Master ","TMSShipper_Master", storedParams);

            return dsShipper;

        }

        public DataSet GetShipperInfo_MasterMoreContact()

        {

            DataSet dsShipper1 = new DataSet();

            SqlParameter[] storedParams = new SqlParameter[1];

            storedParams = Helpers.SqlHelper.GetParams("getShipperInfo_MasterMoreContact");

            dsShipper1 = ExecuteDataset1("getShipperInfo_MasterMoreContact ","TMSShipper_MasterMoreContact", storedParams);

            return dsShipper1;

        }

Open in new window

0
 
LVL 14

Accepted Solution

by:
GiftsonDJohn earned 500 total points
ID: 24094652
Okay. We shall do it very simple.

You have two functions now which both of them returns a DataSet

GetShipperInfo_Master  and GetShipperInfo_MasterMoreContact

Do as follows

dsShipper_master = objShipperInfo.GetShipperInfo_Master();
dsShipper_master.Tables.Add(objShipperInfo.GetShipperInfo_MasterMoreContact().Tables["TMSShipper_MasterMoreContact"].Clone());

The first function will return the dataset with Master Details.
since the second function will also returns a dataset, assigning the return value directly into the dsShipper_master object it will overrite the master tables. So what I am doing is, clone the contacts tables from the GetShipperInfo_MasterMoreContact functions return value and add to the dsShipper_master object.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 24094670
that means there is only one table in the dataset returnd from the method
0
 
LVL 14

Expert Comment

by:GiftsonDJohn
ID: 24094708
Yes. they way you are filling the dataset on every function has only one table populated because you are creating different dataset on each function. If you  use the same dataset for populating on both the functions you will have all the tables inside the same dataset.
0
 
LVL 8

Author Closing Comment

by:johny_bravo1
ID: 31567896
Thanks a lott
0
 
LVL 8

Author Comment

by:johny_bravo1
ID: 24094756
please have look at http://www.clubfarpoint.com/FarPointSupportSite/Modules/Docs/SpreadWin4.0/spwin-databind-hierarchy.html

I want to do something like this.
I have opened a question with subject "Farpoint Hierarchical Display".
If you can help me in this that would be great.
Thanks
0
 
LVL 8

Author Comment

by:johny_bravo1
ID: 24094897
Hi GiftsonDJohn;

a little problem with the code.

There is no data in dataset fro table TMSShipper_MasterMoreContact.

dsShipper_master.Tables.Add(objShipperInfo.GetShipperInfo_MasterMoreContact().Tables["TMSShipper_MasterMoreContact"].Clone());
0
 
LVL 8

Author Comment

by:johny_bravo1
ID: 24094915
i use the Copy() method instead of clone().Now I am getting the data for both the table but while creating relation I am getting an exception
"This constraint cannot be enabled as not all values have corresponding parent values."
0
 
LVL 14

Expert Comment

by:GiftsonDJohn
ID: 24094951
Yes. You are correct. Copy() will copy all the data including structure and Clone() will copy only the structure().

This error will appear if not all the foreign key reference has parent key reference. Make sure that all the related rows are fetched from database.
0
 
LVL 8

Author Comment

by:johny_bravo1
ID: 24095133
Yes GiftsonDJohn,
that solved my problem.I just want to ask you.What if I bind that dataset to any control like farpoint or datagrid.What data should I see?
0
 
LVL 14

Expert Comment

by:GiftsonDJohn
ID: 24095180
If you bind directly to a datagrid you will see the rows from the first table i.e. TMSShipper_Master. If you want to combine both the tables and output as a single table you have to write some lines of code to achieve this.
0
 
LVL 8

Author Comment

by:johny_bravo1
ID: 24095193
Thanks GiftsonDJohn for your reply.
Aactually I am trying to display it in hierarchy.You have any idea of doing this with farpoint?
0
 
LVL 14

Expert Comment

by:GiftsonDJohn
ID: 24095258
I have not used farpoint.

What you have to do is to create another DataTable object, configure all the columns you want to display. Start Iterating your master table

DataTable newTable = new DataTable();
newTable.Columns.Add("<columnName1">");
newTable.Columns.Add("<columnName2">");
.
.
newTable.Columns.Add("<columnNamen">");

foreach(DataRow dr in dsShipper_master.Tables["TMSShipper_Master"].Rows)
{
      foreach(DataRow cdr in dr.GetChildRows("Root"))
      {
             DataRow newRow = newTable.NewRow();
             newRow["<columnName1>"]=dr[1];
             newRow["<columnName2>"]=cdr[1];
             newRow["<columnName3>"]=dr[2];
             .
             .
             newRow["<columnNamen>"]=cdr[4];

             newTable.Rows.add(newRow);
      }
}

Now you can bind the newTable object to DataGrid.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This video discusses moving either the default database or any database to a new volume.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now