C# .NET - seperate datasets based on a parameter

Asked By samaira samaira
03-Feb-12 08:46 AM
generate two datasets based on a parameter
if datset row[n]0[]  value is abc create a seperate dataset
if datset row[n]0[]  value is bcd create a seperate dataset
  [)ia6l0 iii replied to samaira samaira
03-Feb-12 12:28 PM
Perhaps, you start like this.

if(dataset !=null) && (dataset.Tables.Count > 0)
{
DataTable table = dataset.Tables[0];
if (table.Rows.Count > 0)
{
string value = tables.Rows[rownumber][columnname].ToString();
if (value.ToLower() == "abc")
{
//populate one datatable
}
else if (value.ToLower() == "bcd")
{
//populate another datatable
}

}
}

And Note that you shouldn't create separate datasets on either of the condition. Instead, You should create datasets outside these statements. And add the appropriate DataRows to it based on the value.

Like,
DataSet abcDataSet = new DataSet();
DataSet bcdDataSet = new DataSet();

DataTable abcDataTable = new DataTable();
abcDataSet.Tables.Add(abcDataTable);

DataTable bcdDataTable = new DataTable();
bcdDataSet.Tables.Add(bcdDataTable);


And then add the rows based on your if condition to appropriate datatables. 

I hope it helps.






  kalpana aparnathi replied to samaira samaira
03-Feb-12 12:52 PM
The main idea is to copy current dataset and remove active records from first and inactive records from second dataset.

DataSet
abc = new DataSet();
abc.ReadXml(UsersXmlReader);

DataSet bcd= ds.Copy();//Copy to your another dataset
Remove(abc.Tables[0], "0");//Remove all bcd(where Active = 0) records from abc DataSet
Remove(dsInactive.Tables[0], "1");//Remove all abc(where Active = 1) records from dsInactive DataSet

private void Remove(DataTable table, string active)
{
   
for (int i = 0; i < table.Rows.Count; i++)
   
{
       
if (table.Rows[i]["Active"].Equals(active))
       
{
            table
.Rows[i].Delete();
            i
= i - 1;//Removed record so we need to check same index
       
}
   
}
}
  samaira samaira replied to [)ia6l0 iii
06-Feb-12 04:38 AM
Thanks..it worked!!
  [)ia6l0 iii replied to samaira samaira
06-Feb-12 04:54 AM
Glad to help!
Create New Account
help
just in Label !! - -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --. how i can put this method in DropdownList ??? DataSet dataSet = new DataSet(); DataTable dataTable = new DataTable("table1"); dataTable.Columns.Add("col1", typeof(string)); dataSet.Tables.Add(dataTable); System.IO.StringReader xmlSR = new System.IO.StringReader(" http: / / www.webservicex.net / country.asmx?op GetCountries " ); dataSet.ReadXml(xmlSR, XmlReadMode.IgnoreSchema); Now assign dataset to dropdownlist Dropdownlist.datasource = dataset Please refer below link
2007 file. first i kept all the data of the gridview into an " System.Data.DataTable" datatable. Then i by processing one by one record i am inserting these records to an cmd.CommandType = CommandType.Text; / / load all of the records from the Excel sheet into a DataTable object da = new OleDbDataAdapter(cmd); DataTable dt = new DataTable(); da.Fill(dt); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { if (conn.State = = ConnectionState to DataBase using SqlBulkCopy() method you van do this- code- DataSet reportData = new DataSet(); reportData.ReadXml(Server.MapPath(”report.xml”)); SqlConnection connection = new SqlConnection(”CONNECTION STRING”); SqlBulkCopy sbc = new SqlBulkCopy(connection cost_USD”); connection.Open(); / / table 4 is the main table in this dataset sbc.WriteToServer(reportData.Tables[4]); connection.Close(); Use this code and let me know. Hi , You can use a line at the bottom: / / Create the data set and table DataSet ds = new DataSet("New_DataSet"); DataTable dt = new DataTable("New_DataTable"); / / Set the locale for each ds.Locale = System.Threading.Thread.CurrentThread
want to create a dataset from a xml file (stored in a folder) thnx Using ReadXml method of Dataset you can load XMl file in Dataset. Use this code DataSet ds = new DataSet(); ds.ReadXml("xml path"); After excution of this code xml file will be loaded to dataset. thnx a lot. Hi, just use this method for read xml file in datatable in C#.net, which is working for me public DataTable ReadXML( string file) { / / create the DataTable that will hold the data DataTable table = new DataTable( "XmlData" ); try { / / open the file using a Stream using (Stream stream = new FileStream(file, FileMode
how to copy tables from dataset to datatable in c#.net In my below code i have two tables in dataset which is named as _DSFootnotes. . . . i have to copy tht two tables which are in dataset to new datatable . . .so tht i can get two tables in datatable. . in first table i have to do defalutview.sort and second table no need to at last i have to clear tht datatset _DSFootnotes and add tht first and second tables to _DSFootnotes . . . . . _DSFootnotes = oBF.GetFootnotes(sParams, ref _error); if (_DSFootnotes ! = null && _DSFootnotes.Tables[0].Rows
DataTable Hi! Could you explain the DataTable concept with examples or related links Thanks Dot net allows us to create our own Tables and work with them. The DataTable is an in-memory representation of a block of data. We can create our own tables in code using a DataSet and the types defined in the System.Data.OleDb or Data.SqlClient namespaces. The following are the core properties that are used while creating a DataTable. CaseSensitive: Indicates whether string comparisons in the table are case-sensitive or not. ChildRelations: Returns the collection of child relations of the DataTable (if any). Columns: Returns the collection of columns that belong to this table. Constraints: Gets the name of the table. refer this link for details http: / / www.startvbdotnet.com / ado / datatable.aspx http: / / dotnetjunkies.com / Article / 3532F83F-DB3D-4B3D-8E90-11D090060966.dcik The DataTable is a