none
Reference a dataset and add rows to it RRS feed

  • Question

  • I have the following problem, I have a string[] list that contains values I need to put in a DataTable.  Which DataTable depends on the first value in the string[] list.

     

    I have this code:

     

    Code Snippet

    DataTable dt;

    string[] FieldList;

    ...

    switch (FieldList.GetValue(0).ToString())

    {

    case "01": { dt = Common.CareMomentsDataTable; }

    case "02": { dt = Common.UsersDataTable; }

    case "03": { dt = Common.ActionListDataTable; }

    }

     

     

    Afterwards I want to add rows like:

     

    dt.AddRow ...

     

    I get error:

    Error 1 'ReadVars.Common.CareMomentsDataTable' is a 'type', which is not valid in the given context Form1.cs 50 46 ReadVars

    How can I make a reference to the DataTable objects (in Common.xsd and then use that reference?

     

    Thanks in advance.

    Thursday, January 24, 2008 11:38 PM

All replies

  • As the error indicates the Common.CareMomentsDataTable is a datatype.
    From the naming convension I am assuming that you have derived that type from the DataTable class.

    To instantiate the object "dt"
    you might have to include the "new" keyword;

    Let me know if this solves the prob and lets u proceed ahead with what you want to do.

    ...
    case "01": { dt = new Common.CareMomentsDataTable(); }
    ...

    Friday, January 25, 2008 12:15 AM
  • Hi kprabu,

     

    Thanks for your answer, well you code doesn't give an error message, but this creates a new datatable?  Is this good?

     

    Also: So if I get this right, CareMomentsDataTable is a type of DataTable but not a created one?  Then what is the best way to create a real DataTable that I can fill with rows and use throughout the program?  Can I do that in the xsd?  Sorry for all the newbie questions, I'm trying to understand what's going on here.

    Friday, January 25, 2008 12:30 AM
  • It depends on how u want to fill the datatable.
    Once a datatable is created, you can insert rows to it by doing something like this

    DataRow dr = dt->NewRow()
    dr[0]="col1 value";
    dr[2]="col2 value";
    ...
    once you fill all the columns add the datarow to the table
    dt->Rows->Add(dr)

    You can put the above in a for loop to add the rows.

     

    Friday, January 25, 2008 12:45 AM
  • Ok kprabu but now suppose I have like 10 forms.  And I want to use 3 readonly DataTables throughout the program that are read in the main form.  I thought I could put them in common.xsd but that's only the type definition, not the actual DataTable, they are not created there.  You create them by doing

     

    dt = new Common.CareMomentsDataTable();

     

    Where do I create dt so all forms are able to access it?  What is best practise for this? 

    Friday, January 25, 2008 1:52 AM
  • I am still searching on this one, in the meantime I have found out about singleton and made this class:

     

    Code Snippet

    public sealed class DataStorage

    {

    static readonly DataStorage _instance = null;

    // Explicit static constructor to tell C# compiler

    // not to mark type as beforefieldinit

    static DataStorage()

    {

    }

    DataStorage()

    {

    }

    public static DataStorage Instance

    {

    get

    {

    return _instance;

    }

    }

    public DataTable CareMoments = new Common.CareMomentsDataTable();

    public DataTable Users = new Common.UsersDataTable();

    public DataTable ActionList = new Common.ActionListDataTable();

    }

     

     

    In the main code I now have:

     

     

    Code Snippet

    DataTable dt;

     

    switch (FieldList.GetValue(0).ToString())

    {

    case "01": { dt = DataStorage.Instance.CareMoments; break; }

    case "02": { dt = DataStorage.Instance.Users; break; }

    case "03": { dt = DataStorage.Instance.ActionList; break; }

    default: { dt = DataStorage.Instance.CareMoments; break; }

    }

    DataRow dr = dt.NewRow(); //create a new datarow in the appropriate table

    for (int y = 1; y < FieldList.Length; y++)

    {

    if (dr[y].GetType() == typeof(bool))

    {

    if (FieldList[y].ToString() == "0")

    { dr[y] = false; }

    else

    { dr[y] = true; }

    }

    else

    { dr[y] = FieldList[y]; }

    }

     

    So my goal is to write new rows to CareMoments, Users and ActionList and use those DataTables throughout the program.  The code above doesn't seem to work however ("NullReferenceException").  What I'm trying to do is reference DataStorage.Instance.CareMoments, not actually create a new one.  How do I do this?
    Friday, January 25, 2008 1:01 PM