Link to home
Start Free TrialLog in
Avatar of Johny Bravo
Johny Bravo

asked on

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?
Avatar of Johny Bravo
Johny Bravo

ASKER

I just missed one line,
 dsShipper_master = objShipperInfo.GetShipperInfo_Master();

GetShipperInfo_Master() is the method which retrives the data from the Stored procedure
If I set index 0 and 1,rather than giving table names,
I am getting an error "Cannot find table 1."
SOLUTION
Avatar of GiftsonDJohn
GiftsonDJohn
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Gautham Janardhan
how many tables are there in dsShipper_master
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.
GetShipperInfo_Master will fetch data from shipper_master table
and GetShipperInfo_MasterMoreContact is fetching data from shipper_morecontact table.
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

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");
       }
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

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
that means there is only one table in the dataset returnd from the method
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.
Thanks a lott
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
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());
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."
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.
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?
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.
Thanks GiftsonDJohn for your reply.
Aactually I am trying to display it in hierarchy.You have any idea of doing this with farpoint?
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.