Because you have just initialized the DataSet that has NO schema
(tables with columns, relations, etc.)
If you follow the Cerebrus then you can do as,
DataTable dt = ds.Tables["TBL_MFQueryResult"];
DataView dv = dt.DefaultView;
DataSet dsFilterDump = new DataSet(); // use one Dataset containing
many tables
// ***** Note that dsFilterDump does NOT have any schema *******
// Filter the view according the value in the MF_TYPE column.
// You only require the single quotes if the column is a char Sql
Type.
dv.RowFilter = "MF_TYPE = '0'";
dsFilterDump.Tables.Add(dv.ToTable("tblRenewal")); // ***** here we
are adding tables *******
// Filter the view according the value in the MF_TYPE column.
dv.RowFilter = "MF_TYPE = '1'";
dsFilterDump.Tables.Add(dv.ToTable("tblNewActive"));
etc.
And if you like to do the way you are currently doing then you should
first add the tables with columns in the dsFilterDump before the loop.
Regards,
Arsalan Tamiz
On Nov 13, 2:56 pm, Benj Nunez <[EMAIL PROTECTED]> wrote:
> Hello everyone,
>
> Thank you all for your input. I decided to try Usman's suggestion and
> decided to stick
> to just one Dataset. After all, a dataset can house several tables
> right?
>
> My code now looks like this:
>
> DataSet dsFilterDump = new DataSet(); // use one Dataset containing
> many tables
>
> foreach (DataRow dr in ds.Tables["TBL_MFQueryResult"].Rows)
> {
> switch (dr["MF_TYPE"].ToString())
> {
> case "0":
> dsFilterDump.Tables["tblRenewal"].ImportRow(dr);
> break;
> case "1":
> dsFilterDump.Tables["tblNewActive"].ImportRow(dr);
> break;
> case "2":
> dsFilterDump.Tables["tblNewEmployee"].ImportRow(dr);
> break;
> case "3":
> dsFilterDump.Tables["tblNewPensioner"].ImportRow(dr);
> break;
> case "4":
> dsFilterDump.Tables["tblNewSurvivor"].ImportRow(dr);
> break;
> } // of switch
>
> } // of foreach
>
> It compiles just fine, but when I run it I get a
> NullReferenceException. :(
> I suspect that I still need to create a DataTable, put some values to
> it then insert it into the dataset. Am I right?
>
> How do I express this in code? Thanks again.
>
> On Nov 13, 2:57 pm, Cerebrus <[EMAIL PROTECTED]> wrote:
>
>
>
> > Thank you for the awesome rating and the correction, Sallushan ! :-)
>
> > As for the Add() method, I just modified the OP's code without really
> > thinking about it. It should instead be :
>
> > ---
> > dsRenewal.Tables[0].Rows.Add(dr.ItemArray);
> > ---
>
> > Of course, we can both agree that that code just doesn't perform well
> > enough.
>
> > On Nov 13, 11:01 am, sallushan <[EMAIL PROTECTED]> wrote:
>
> > > In my opinion Cerebrus' approach (using DataView) is best.
>
> > > But I can't find the 'Add()' method in DataTable Class. Also Following
> > > things should be noted:
>
> > > 1) 'dr.ItemArray' will return the array of Values in each column of
> > > 'dr'
> > > 2) If a DataRow which is already added in a DataTable canNOT be added
> > > in some other DataTable, using its Reference. One should create a copy
> > > of that DataRow (using ItemArray or some other method) and Add that
> > > copy in other DataTable.
>
> > > On Nov 12, 4:56 pm, Cerebrus <[EMAIL PROTECTED]> wrote:
>
> > > > I would rather do it this way :
>
> > > > ---
> > > > DataTable dt = ds.Tables["TBL_MFQueryResult"];
> > > > DataView dv = dt.DefaultView;
>
> > > > // Filter the view according the value in the MF_TYPE column.
> > > > // You only require the single quotes if the column is a char Sql
> > > > Type.
> > > > dv.RowFilter = "MF_TYPE = '0'";
> > > > DataTable dtRenewal = dv.ToTable("Renewal");
>
> > > > // Filter the view according the value in the MF_TYPE column.
> > > > dv.RowFilter = "MF_TYPE = '1'";
> > > > DataTable dtNewActive = dv.ToTable("New Active");
>
> > > > ---
>
> > > > If you must do it your way, you already have the reference to the
> > > > required DataRow in your For-Each block, so why do you need to
> > > > reference it from the DataTable ?
>
> > > > ---
> > > > dsRenewal.Tables[0].Add(dr.ItemArray);
> > > > ---
>
> > > > On Nov 12, 2:45 pm, Benj Nunez <[EMAIL PROTECTED]> wrote:
>
> > > > > Hello experts,
>
> > > > > Currently I'm stuck in a situation in which I need to copy a record
> > > > > from one dataset
> > > > > to another. Can anyone help me out with this?
>
> > > > > My goal is to loop through a dataset and check if a certain field
> > > > > meets a criterion,
> > > > > if there's a match, that record goes through another dataset. Here's
> > > > > what my code looks like:
>
> > > > > DataSet dsRenewal = new DataSet();
> > > > > DataSet dsNewActive = new DataSet();
> > > > > ...
>
> > > > > foreach (DataRow dr in ds.Tables["TBL_MFQueryResult"].Rows)
> > > > > {
> > > > > if (dr["MF_TYPE"] == '0') // '0' means its a renewal, '1'
> > > > > New Active and so on...
> > > > > {
> > > > > dsRenewal.Tables[0].Add(ds.Tables["TBL_MFQueryResult"].Rows
> > > > > [?].ItemArray); // huh?
> > > > > }
> > > > > else if (dr[...])
> > > > > {
> > > > > dsNewActive.Tables[]....
> > > > > }
> > > > > ...
>
> > > > > }
>
> > > > > Cheers!
>
> > > > > Benj- Hide quoted text -
>
> > > > - Show quoted text -- Hide quoted text -
>
> > > - Show quoted text -- Hide quoted text -
>
> - Show quoted text -