[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 241
  • Last Modified:

OBJECT REFERENCE WAS NOT SET

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
Johny Bravo
Asked:
Johny Bravo
  • 12
  • 7
  • 2
2 Solutions
 
Johny BravoAuthor Commented:
I just missed one line,
 dsShipper_master = objShipperInfo.GetShipperInfo_Master();

GetShipperInfo_Master() is the method which retrives the data from the Stored procedure
0
 
Johny BravoAuthor Commented:
If I set index 0 and 1,rather than giving table names,
I am getting an error "Cannot find table 1."
0
 
GiftsonDJohnCommented:
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Gautham JanardhanCommented:
how many tables are there in dsShipper_master
0
 
Johny BravoAuthor Commented:
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
 
Johny BravoAuthor Commented:
GetShipperInfo_Master will fetch data from shipper_master table
and GetShipperInfo_MasterMoreContact is fetching data from shipper_morecontact table.
0
 
Johny BravoAuthor Commented:
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
 
GiftsonDJohnCommented:
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
 
Johny BravoAuthor Commented:
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
 
GiftsonDJohnCommented:
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
 
Gautham JanardhanCommented:
that means there is only one table in the dataset returnd from the method
0
 
GiftsonDJohnCommented:
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
 
Johny BravoAuthor Commented:
Thanks a lott
0
 
Johny BravoAuthor Commented:
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
 
Johny BravoAuthor Commented:
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
 
Johny BravoAuthor Commented:
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
 
GiftsonDJohnCommented:
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
 
Johny BravoAuthor Commented:
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
 
GiftsonDJohnCommented:
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
 
Johny BravoAuthor Commented:
Thanks GiftsonDJohn for your reply.
Aactually I am trying to display it in hierarchy.You have any idea of doing this with farpoint?
0
 
GiftsonDJohnCommented:
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

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 12
  • 7
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now