how to merge the dataset - c#

hai friends
I am one dataset like this ds like three columns this:
tblkey Empkey Empname
T101 E10 Natraj
T102 E11 Siva
T103 E14 ganesh
I am having another dataset ds1 only two columns like this:
Empkey Empname
E10 karthi
E11 thriu
E13 maran
i waant merge the dataset and check the values while checking if ds is not having E13 it should bind and show the result like this ds
tblkey Empkey Empname
T101 E10 Natraj
T102 E11 Siva
T103 E14 ganesh
E13 maran
here 'tblkey' comes empty
how to do:

To replicate your example entirely:
DataSet ds1 = new DataSet();
DataSet ds2 = new DataSet();
ds1.Tables.Add(new DataTable());
ds2.Tables.Add(new DataTable());
ds1.Tables[0].Columns.Add("tblkey");
ds1.Tables[0].Columns.Add("empkey");
ds1.Tables[0].Columns.Add("empname");
ds2.Tables[0].Columns.Add("empkey");
ds2.Tables[0].Columns.Add("empname");
ds1.Tables[0].Rows.Add("T101", "E10", "Natraj");
ds1.Tables[0].Rows.Add("T102", "E11", "Siva");
ds1.Tables[0].Rows.Add("T103", "E14", "ganesh");
ds2.Tables[0].Rows.Add("E10", "karthi");
ds2.Tables[0].Rows.Add("E11", "thriu");
ds2.Tables[0].Rows.Add("E13", "maran");
// primary keys must be set in order for the merge to work
ds1.Tables[0].PrimaryKey = new DataColumn[] { ds1.Tables[0].Columns["empkey"] };
ds2.Tables[0].PrimaryKey = new DataColumn[] { ds2.Tables[0].Columns["empkey"] };
// this is the critical line
ds1.Merge(ds2, true, MissingSchemaAction.Add);
Adding the missing schema (in this case, the tblkey column) is achieved by setting the third parameter correctly.

Related

Single Datagridview Master-Details, no error but only shows "(Collection)"

I am using SQLite and I would like to use Master-Detail in a single DataGridView. No error return, I check the Master Table ("ASM") and the Details Table ("PART") their data are good. However, my gridAsmPart only shows one row with the word (Collection) on column PART. Any help will be appreciated.
public void LoadData()
{
var asm = new SQLiteDataAdapter("SELECT * FROM ASM", dB.connectionString);
var part = new SQLiteDataAdapter("SELECT * FROM PART", dB.connectionString);
var ds = new DataSet();
asm.Fill(ds, "ASM");
part.Fill(ds, "PART");
DataRelation data_relation = new DataRelation(
"ASM_PART",
ds.Tables["ASM"].Columns["PART"],
ds.Tables["PART"].Columns["PART"]);
ds.Relations.Add(data_relation);
gridAsmPart.DataSource = ds;
}
Edit 1: It seems that if I use the old DataGrid, it works.
Well… on the line of code… gridAsmPart.DataSource = ds; … ds is a DataSet and when you assign a DataSet to the grid as a DataSource, then you also need to specify “which” table in the DataSet the grid should display. That is what the grids DataMember is for.
Have you tried to set the grids DataMember property to something like…?
gridAsmPart.DataSource = ds;
gridAsmPart.DataMember = “ASM”;
Or if you want the “Part” table to display….
gridAsmPart.DataSource = ds;
gridAsmPart.DataMember = “PART”;

Main dataset is getting modified when local dataset is modified

Below is my code.
DataSet ds = new DataSet();
ds = dsComplaints;
DataTable dt = new DataTable();
dt = ds.Tables[0];
DataRow dr = dt.NewRow();
dr.ItemArray = new object[] {"--Select Complaint--" };
dt.Rows.InsertAt(dr, 0);
dsComplaints is globally declared which has the main data. I am copying it to a local DataSet ds. Then I assign the table from ds to a datatable. I am adding a row(select complaint) in data table. But when the insertion happens, my main dataset i.e. dsComplaints is also added with a row. How can i avoid that. I don't know why this is happening.
You can use the Copy method to create a new copy of same DataSet instead of using the reference only of the original one to avoid this :
DataSet ds = dsComplaints.Copy();
From MSDN DOCS this is what Copy does:
Copies both the structure and data for this DataSet.
Now you can do whatever is needed with the copy and it would not effect the original version of DataSet.

Return Multiple DataTable in C# 2005

I have table1 and table2 in a class..
public DataTable sampletable (DataTable table1,DataTable table2)
{
// How to return the two table(table1 and table2)
}
Advance thank you
public DataTable[] sampletable (DataTable table1,DataTable table2)
{
return new DataTable[] { table1, table2 };
}
Use an array. And to retrieve a particular table:
DataTable[] dtArray = sampletable (YourFirstDt, YourSecondDt);
DataTable table1 = dtArray[0];
DataTable table2 = dtArray[1];
Assuming they have the same schema, you can use the DataTable.Merge Method
public DataTable sampletable(DataTable table1, DataTable table2)
{
table1.Merge(table2);
return table1;
}
The Merge method is used to merge two DataTable objects that have
largely similar schemas. A merge is typically used on a client
application to incorporate the latest changes from a data source into
an existing DataTable. This allows the client application to have a
refreshed DataTable with the latest data from the data source.
The merge operation takes into account only the original table, and the
table to be merged. Child tables are not affected or included. If a
table has one or more child tables, defined as part of a relationship,
each child table must be merged individually.
When merging a new source DataTable into the target, any source rows
with a DataRowState value of Unchanged, Modified, or Deleted, is
matched to target rows with the same primary key values. Source rows
with a DataRowState value of Added are matched to new target rows with
the same primary key values as the new source rows.
You can use DataSet , Create a new DataSet and Add the multiple tables to it ,
For Eg-
DataSet Ds = new DataSet();
DataTable Dt1= new DataTable();
Ds.Tables.Add(Dt1)
you can add multiple tables and to access the datatable you can use the index ( eg -
Ds.Tables[0])
Hope this answers your question!!.
public DataSet Getdatasettables()
{
DataSet ds = new DataSet();
DataTable dt1 = new DataTable();
DataTable dt2 = new DataTable();
ds.Tables.Add(dt1);
ds.Tables.Add(dt2);
return ds;
}

Binding a DataGridView to a DataSet directly fails

A DataGridView bound by this code does not display information as expected:
dataGridView1.DataSource = ds;
here's the code for ds:
public DataSet ConnectandReadList()
{
DataSet ds = new DataSet();
string connection_string="Data Source=hermes;database=qcvalues; Integrated Security=SSPI;";
using (var myConnection = new SqlConnection(connection_string))
{
myConnection.Open();
var command = new SqlCommand(InitializeQuery(), myConnection);
var adapter = new SqlDataAdapter(command);
adapter.Fill(ds);
}
return ds;
}
Try binding to the table inside the dataset: dataGridView1.DataSource = ds.Tables[0];
From the documentation for the DataGridView.DataSource property, you can also bind to a DataSet and use the DataMember property:
When binding to a data source that contains multiple lists or tables, you must set the DataMember property to a string that specifies the list or table to bind to.
I think in this case the table name would be "Table" since you are not naming it explicitly.
Is ds a DataSet?
If so, try setting your DGV's DataMember to a DataTable within the DataSet or specifying your DataSet's DataTable for the DataSource.

How to add data to a new formed column

I am trying to add data to a newly created column...
DataSet ds = new DataSet;
ds.Tables[0].Columns.Add("Count");
Data.DataSource = ds;
Data.DataBind();
Where do I add, a hardcoded count? (I want count column to have '4')
You add data to rows - so:
DataSet ds = new DataSet();
DataTable table = ds.Tables.Add();
table.Columns.Add("Count", typeof(int));
table.Rows.Add(4);
However, there are probably easier options; a DataSet seems massively overkill to bind a single value.
It technically is a repeater function but if I get the hardcoded function... I can make the code to make it flexible. I got it to work. I was adding a column to an existing table. This is what I did...
for int i=0; i<ds.tables[0].Rows.Count; i++)
{
ds.tables[0].Rows[i]["Count"] = "4";
}

Categories