Johny Bravo
asked on
OBJECT REFERENCE WAS NOT SET
Hi I am getting the above error
dsShipper_master = new DataSet();
dsShipper_master = objShipperInfo.GetShipperI nfo_Master MoreContac t();
dsShipper_master.Relations .Add("Root ", dsShipper_master.Tables["T MSShipper_ Master"].C olumns["Sh ipperId"], dsShipper_master.Tables["T MSShipper_ MasterMore Contact"]. Columns["S hipperId"] );
I need to set the relation.What sholud be change?
dsShipper_master = new DataSet();
dsShipper_master = objShipperInfo.GetShipperI
dsShipper_master.Relations
I need to set the relation.What sholud be change?
ASKER
If I set index 0 and 1,rather than giving table names,
I am getting an error "Cannot find table 1."
I am getting an error "Cannot find table 1."
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
how many tables are there in dsShipper_master
ASKER
Hi I am doing it like this,
dsShipper_master = objShipperInfo.GetShipperI nfo_Master ();
dsShipper_master.Tables[0] .TableName = "TMSShipper_Master";
dsShipper_master = objShipperInfo.GetShipperI nfo_Master MoreContac t();
dsShipper_master.Tables[1] .TableName = "TMSShipper_MasterMoreCont act";
on the last line I am getting error "Cannot find table 1",but I am having data in the 3rd line.
dsShipper_master = objShipperInfo.GetShipperI
dsShipper_master.Tables[0]
dsShipper_master = objShipperInfo.GetShipperI
dsShipper_master.Tables[1]
on the last line I am getting error "Cannot find table 1",but I am having data in the 3rd line.
ASKER
GetShipperInfo_Master will fetch data from shipper_master table
and GetShipperInfo_MasterMoreC ontact is fetching data from shipper_morecontact table.
and GetShipperInfo_MasterMoreC
ASKER
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();
}
}
No no you should not do like that. It will overwrite the previous filled tables.
instead of doing like this
dsShipper_master = objShipperInfo.GetShipperI nfo_Master ();
dsShipper_master.Tables[0] .TableName = "TMSShipper_Master";
dsShipper_master = objShipperInfo.GetShipperI nfo_Master MoreContac t();
dsShipper_master.Tables[1] .TableName = "TMSShipper_MasterMoreCont act";
do as
dsShipper_master = new DataSet();
objShipperInfo. GetShipperInfo_Master(ref dsShipper_master);
objShipperInfo.GetShipperI nfo_Master MoreContac t(ref dsShipper_master);
inside the GetShipperInfo_Master function
public void GetShipperInfo_Master(ref DataSet ds)
{
.....
SqlAdp.Fill(ds, "TMSShipper_Master");
}
inside GetShipperInfo_MasterMoreC ontact function
public void GetShipperInfo_MasterMoreC ontact(ref DataSet ds)
{
....
SqlAdp.Fill(ds, "TMSShipper_MasterMoreCont act");
}
instead of doing like this
dsShipper_master = objShipperInfo.GetShipperI
dsShipper_master.Tables[0]
dsShipper_master = objShipperInfo.GetShipperI
dsShipper_master.Tables[1]
do as
dsShipper_master = new DataSet();
objShipperInfo. GetShipperInfo_Master(ref dsShipper_master);
objShipperInfo.GetShipperI
inside the GetShipperInfo_Master function
public void GetShipperInfo_Master(ref DataSet ds)
{
.....
SqlAdp.Fill(ds, "TMSShipper_Master");
}
inside GetShipperInfo_MasterMoreC
public void GetShipperInfo_MasterMoreC
{
....
SqlAdp.Fill(ds, "TMSShipper_MasterMoreCont
}
ASKER
Ok I have changed the code accordingly.
Table names are set accordingly.
Let me describe you in detail.
dsShipper_master = objShipperInfo.GetShipperI nfo_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.GetParam s("getShip perInfo_Ma ster");
dsShipper = Helpers.SqlHelper.ExecuteD ataset1("g etShipperI nfo_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.
Table names are set accordingly.
Let me describe you in detail.
dsShipper_master = objShipperInfo.GetShipperI
calls GetShipperInfo_Master(); on other page
it;s def.
public DataSet GetShipperInfo_Master()
{
DataSet dsShipperInfo = objShipper.GetShipperInfo_
return dsShipperInfo;
}
in this DataSet dsShipperInfo = objShipper.GetShipperInfo_
call other page
public DataSet GetShipperInfo_Master()
{
DataSet dsShipper = new DataSet();
SqlParameter[] storedParams = new SqlParameter[1];
storedParams = Helpers.SqlHelper.GetParam
dsShipper = Helpers.SqlHelper.ExecuteD
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;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
ASKER
Thanks a lott
ASKER
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
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
ASKER
Hi GiftsonDJohn;
a little problem with the code.
There is no data in dataset fro table TMSShipper_MasterMoreConta ct.
dsShipper_master.Tables.Ad d(objShipp erInfo.Get ShipperInf o_MasterMo reContact( ).Tables[" TMSShipper _MasterMor eContact"] .Clone());
a little problem with the code.
There is no data in dataset fro table TMSShipper_MasterMoreConta
dsShipper_master.Tables.Ad
ASKER
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."
"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.
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.
ASKER
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?
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.
ASKER
Thanks GiftsonDJohn for your reply.
Aactually I am trying to display it in hierarchy.You have any idea of doing this with farpoint?
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("<col umnName1"> ");
newTable.Columns.Add("<col umnName2"> ");
.
.
newTable.Columns.Add("<col umnNamen"> ");
foreach(DataRow dr in dsShipper_master.Tables["T MSShipper_ Master"].R ows)
{
foreach(DataRow cdr in dr.GetChildRows("Root"))
{
DataRow newRow = newTable.NewRow();
newRow["<columnName1>"]=dr [1];
newRow["<columnName2>"]=cd r[1];
newRow["<columnName3>"]=dr [2];
.
.
newRow["<columnNamen>"]=cd r[4];
newTable.Rows.add(newRow);
}
}
Now you can bind the newTable object to DataGrid.
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("<col
newTable.Columns.Add("<col
.
.
newTable.Columns.Add("<col
foreach(DataRow dr in dsShipper_master.Tables["T
{
foreach(DataRow cdr in dr.GetChildRows("Root"))
{
DataRow newRow = newTable.NewRow();
newRow["<columnName1>"]=dr
newRow["<columnName2>"]=cd
newRow["<columnName3>"]=dr
.
.
newRow["<columnNamen>"]=cd
newTable.Rows.add(newRow);
}
}
Now you can bind the newTable object to DataGrid.
ASKER
dsShipper_master = objShipperInfo.GetShipperI
GetShipperInfo_Master() is the method which retrives the data from the Stored procedure