using DataSet programatically without an sql server database

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andy B

    using DataSet programatically without an sql server database

    I need to custom build and use a dataset in c# to use with xml. Does anybody
    know where I can find out how to do something like this? I was going to
    create a class that generated the dataset and its data to hide all of the
    complex (or should I say the huge chuncks of code) from the page itself.



  • =?Utf-8?B?TWFuaXNo?=

    #2
    RE: using DataSet programatically without an sql server database

    Hi Andy,

    I am not quite sure what your requirement is but if you want to populate
    your dataset from some XML and not from some database in the SQL Server then
    you can create a new dataset in your code in C# and then read the XML file
    into that dataset. Please find the code below.

    DataSet ds= new DataSet();
    ds.ReadXml("pat h of the XML file");

    Regards,
    Manish
    .NET UI controls for desktop, mobile, and web applications. Build stunning apps in WinForms, WPF, ASP.NET, and more. Try ComponentOne today.



    "Andy B" wrote:
    I need to custom build and use a dataset in c# to use with xml. Does anybody
    know where I can find out how to do something like this? I was going to
    create a class that generated the dataset and its data to hide all of the
    complex (or should I say the huge chuncks of code) from the page itself.
    >
    >
    >
    >

    Comment

    • Andy B

      #3
      Re: using DataSet programatically without an sql server database

      I am mainly looking for a way to programatically create and populate columns
      and rows manually instead of with xml or a database.


      "Manish" <Manish@discuss ions.microsoft. comwrote in message
      news:3FB4A54E-96D6-489E-84C4-0741B1C4CE09@mi crosoft.com...
      Hi Andy,
      >
      I am not quite sure what your requirement is but if you want to populate
      your dataset from some XML and not from some database in the SQL Server
      then
      you can create a new dataset in your code in C# and then read the XML file
      into that dataset. Please find the code below.
      >
      DataSet ds= new DataSet();
      ds.ReadXml("pat h of the XML file");
      >
      Regards,
      Manish
      .NET UI controls for desktop, mobile, and web applications. Build stunning apps in WinForms, WPF, ASP.NET, and more. Try ComponentOne today.

      >
      >
      "Andy B" wrote:
      >
      >I need to custom build and use a dataset in c# to use with xml. Does
      >anybody
      >know where I can find out how to do something like this? I was going to
      >create a class that generated the dataset and its data to hide all of the
      >complex (or should I say the huge chuncks of code) from the page itself.
      >>
      >>
      >>
      >>

      Comment

      • Mark Rae [MVP]

        #4
        Re: using DataSet programatically without an sql server database

        "Andy B" <a_borka@sbcglo bal.netwrote in message
        news:uN9RlTKvIH A.4912@TK2MSFTN GP03.phx.gbl...

        [top-posting corrected]
        >>I need to custom build and use a dataset in c# to use with xml. Does
        >>anybody
        >>know where I can find out how to do something like this? I was going to
        >>create a class that generated the dataset and its data to hide all of
        >>the
        >>complex (or should I say the huge chuncks of code) from the page itself.
        >>
        >I am not quite sure what your requirement is but if you want to populate
        >your dataset from some XML and not from some database in the SQL Server
        >then
        >you can create a new dataset in your code in C# and then read the XML
        >file
        >into that dataset. Please find the code below.
        >>
        >DataSet ds= new DataSet();
        >ds.ReadXml("pa th of the XML file");
        >
        I am mainly looking for a way to programatically create and populate
        columns and rows manually instead of with xml or a database.
        using (DataTable objDT = new DataTable())
        {
        objDT.Columns.A dd("First", typeof(string)) ;
        objDT.Columns.A dd("Second", typeof(string)) ;
        objDT.Columns.A dd("Third", typeof(string)) ;
        objDT.Columns.A dd("Fourth", typeof(string)) ;
        objDT.Rows.Add( new object[4] { "R1C1", "R1C2", "R1C3", "R1C4" });
        objDT.Rows.Add( new object[4] { "R2C1", "R2C2", "R2C3", "R2C4" });
        objDT.Rows.Add( new object[4] { "R3C1", "R3C2", "R3C3", "R3C4" });
        objDT.Rows.Add( new object[4] { "R4C1", "R4C2", "R4C3", "R4C4" });
        MyGridView.Data Source = objDT;
        MyGridView.Data Bind();
        }


        --
        Mark Rae
        ASP.NET MVP


        Comment

        • Andy B

          #5
          Re: using DataSet programatically without an sql server database

          using (DataTable objDT = new DataTable())
          {
          objDT.Columns.A dd("First", typeof(string)) ;
          objDT.Columns.A dd("Second", typeof(string)) ;
          objDT.Columns.A dd("Third", typeof(string)) ;
          objDT.Columns.A dd("Fourth", typeof(string)) ;
          objDT.Rows.Add( new object[4] { "R1C1", "R1C2", "R1C3", "R1C4" });
          objDT.Rows.Add( new object[4] { "R2C1", "R2C2", "R2C3", "R2C4" });
          objDT.Rows.Add( new object[4] { "R3C1", "R3C2", "R3C3", "R3C4" });
          objDT.Rows.Add( new object[4] { "R4C1", "R4C2", "R4C3", "R4C4" });
          MyGridView.Data Source = objDT;
          MyGridView.Data Bind();
          }
          >
          >
          --
          Mark Rae
          ASP.NET MVP
          http://www.markrae.net
          1. What is the using for? and
          2. Why add more than 1 row in the same position with the same values?



          Comment

          • Mark Rae [MVP]

            #6
            Re: using DataSet programatically without an sql server database

            "Andy B" <a_borka@sbcglo bal.netwrote in message
            news:OoA3t1OvIH A.1236@TK2MSFTN GP02.phx.gbl...
            >using (DataTable objDT = new DataTable())
            >{
            > objDT.Columns.A dd("First", typeof(string)) ;
            > objDT.Columns.A dd("Second", typeof(string)) ;
            > objDT.Columns.A dd("Third", typeof(string)) ;
            > objDT.Columns.A dd("Fourth", typeof(string)) ;
            > objDT.Rows.Add( new object[4] { "R1C1", "R1C2", "R1C3", "R1C4" });
            > objDT.Rows.Add( new object[4] { "R2C1", "R2C2", "R2C3", "R2C4" });
            > objDT.Rows.Add( new object[4] { "R3C1", "R3C2", "R3C3", "R3C4" });
            > objDT.Rows.Add( new object[4] { "R4C1", "R4C2", "R4C3", "R4C4" });
            > MyGridView.Data Source = objDT;
            > MyGridView.Data Bind();
            >}
            >
            1. What is the using for? and

            2. Why add more than 1 row in the same position with the same values?
            Where did I do that...?


            --
            Mark Rae
            ASP.NET MVP


            Comment

            • Andy B

              #7
              Re: using DataSet programatically without an sql server database

              Then what does

              objDT.Rows.Add( new object[4]...

              mean? to be more exact... what does object[4] do


              "Mark Rae [MVP]" <mark@markNOSPA Mrae.netwrote in message
              news:%23$Wy%23% 23OvIHA.548@TK2 MSFTNGP06.phx.g bl...
              "Andy B" <a_borka@sbcglo bal.netwrote in message
              news:OoA3t1OvIH A.1236@TK2MSFTN GP02.phx.gbl...
              >
              >>using (DataTable objDT = new DataTable())
              >>{
              >> objDT.Columns.A dd("First", typeof(string)) ;
              >> objDT.Columns.A dd("Second", typeof(string)) ;
              >> objDT.Columns.A dd("Third", typeof(string)) ;
              >> objDT.Columns.A dd("Fourth", typeof(string)) ;
              >> objDT.Rows.Add( new object[4] { "R1C1", "R1C2", "R1C3", "R1C4" });
              >> objDT.Rows.Add( new object[4] { "R2C1", "R2C2", "R2C3", "R2C4" });
              >> objDT.Rows.Add( new object[4] { "R3C1", "R3C2", "R3C3", "R3C4" });
              >> objDT.Rows.Add( new object[4] { "R4C1", "R4C2", "R4C3", "R4C4" });
              >> MyGridView.Data Source = objDT;
              >> MyGridView.Data Bind();
              >>}
              >>
              >1. What is the using for? and
              >

              >
              >2. Why add more than 1 row in the same position with the same values?
              >
              Where did I do that...?
              >
              >
              --
              Mark Rae
              ASP.NET MVP
              http://www.markrae.net

              Comment

              • Mark Rae [MVP]

                #8
                Re: using DataSet programatically without an sql server database

                "Andy B" <a_borka@sbcglo bal.netwrote in message
                news:OemvaQPvIH A.5832@TK2MSFTN GP02.phx.gbl...

                [top-posting corrected]
                >>2. Why add more than 1 row in the same position with the same values?
                >>
                >Where did I do that...?
                >
                Then what does
                >
                objDT.Rows.Add( new object[4]...
                >
                mean? to be more exact... what does object[4] do
                It passes an object array (containing four elements) as the first argument
                of the Add method of the datatable's Rows collection...


                --
                Mark Rae
                ASP.NET MVP


                Comment

                • Andy B

                  #9
                  Re: using DataSet programatically without an sql server database

                  Is this the only way you can do it? or is there some other way. I used
                  NewDataTableRow () before... is this possible without sql server?


                  "Mark Rae [MVP]" <mark@markNOSPA Mrae.netwrote in message
                  news:OyG9vaPvIH A.2032@TK2MSFTN GP02.phx.gbl...
                  "Andy B" <a_borka@sbcglo bal.netwrote in message
                  news:OemvaQPvIH A.5832@TK2MSFTN GP02.phx.gbl...
                  >
                  [top-posting corrected]
                  >
                  >>>2. Why add more than 1 row in the same position with the same values?
                  >>>
                  >>Where did I do that...?
                  >>
                  >Then what does
                  >>
                  >objDT.Rows.Add (new object[4]...
                  >>
                  >mean? to be more exact... what does object[4] do
                  >
                  It passes an object array (containing four elements) as the first argument
                  of the Add method of the datatable's Rows collection...
                  >
                  >
                  --
                  Mark Rae
                  ASP.NET MVP
                  http://www.markrae.net

                  Comment

                  • Mark Rae [MVP]

                    #10
                    Re: using DataSet programatically without an sql server database

                    "Andy B" <a_borka@sbcglo bal.netwrote in message
                    news:eKowu6PvIH A.5472@TK2MSFTN GP06.phx.gbl...

                    [top-posting corrected again]
                    >It passes an object array (containing four elements) as the first
                    >argument of the Add method of the datatable's Rows collection...
                    >
                    Is this the only way you can do it? or is there some other way.
                    Probably - there's usually more than one way to do most things in the .NET
                    Framework...
                    I used NewDataTableRow () before... is this possible without sql server?
                    Sorry, I don't know. I always use the method I outlined...


                    --
                    Mark Rae
                    ASP.NET MVP


                    Comment

                    • Andy B

                      #11
                      Re: using DataSet programatically without an sql server database

                      I saw it possible on an msdn article. Actually I think it was
                      DataTable.NewRo w method home page in its example. Now, I tried to create a
                      dataset with a table and 2 columns. When I ran the page I got this problem:

                      Server Error in '/' Application.
                      --------------------------------------------------------------------------------

                      Object reference not set to an instance of an object.
                      Description: An unhandled exception occurred during the execution of the
                      current web request. Please review the stack trace for more information
                      about the error and where it originated in the code.

                      Exception Details: System.NullRefe renceException: Object reference not set
                      to an instance of an object.

                      Source Error:

                      Line 20: DataColumn IDColumn = new DataColumn("ID" );
                      Line 21: IDColumn.DataTy pe = Type.GetType("S ystem.Int32");
                      Line 22: DataSet.Tables["Table"].Columns.Add(ID Column);
                      Line 23: DataColumn ItemColumn = new DataColumn("Ite m");
                      Line 24: ItemColumn.Data Type = typeof(String);

                      Source File: C:\Documents and Settings\Andy\M y Documents\Visua l Studio
                      2008\Projects\E ternityRecordsW ebsite\Contract s\WebForm1.aspx .cs Line: 22

                      Stack Trace:

                      [NullReferenceEx ception: Object reference not set to an instance of an
                      object.]
                      Contracts.WebFo rm1.Page_Load(O bject sender, EventArgs e) in C:\Documents
                      and Settings\Andy\M y Documents\Visua l Studio
                      2008\Projects\E ternityRecordsW ebsite\Contract s\WebForm1.aspx .cs:22
                      System.Web.Util .CalliHelper.Ev entArgFunctionC aller(IntPtr fp, Object o,
                      Object t, EventArgs e) +15
                      System.Web.Util .CalliEventHand lerDelegateProx y.Callback(Obje ct sender,
                      EventArgs e) +33
                      System.Web.UI.C ontrol.OnLoad(E ventArgs e) +99
                      System.Web.UI.C ontrol.LoadRecu rsive() +47
                      System.Web.UI.P age.ProcessRequ estMain(Boolean
                      includeStagesBe foreAsyncPoint, Boolean includeStagesAf terAsyncPoint) +1436



                      --------------------------------------------------------------------------------
                      Version Information: Microsoft .NET Framework Version:2.0.507 27.1433;
                      ASP.NET Version:2.0.507 27.1433

                      How do I fix it? The references to DataType = Type.GetType("S ystem.Int32")
                      and DataType=Typeof (String) were left that way on purpose for trying to
                      figure out the problem. I had both of the columns datatype set to
                      Typeof(...) but changed the IDColumns to Type.GetType(.. .) when I originally
                      got this problem. Any idea what the problem is? Is it possible I can't add
                      columns after the table is added to the dataset?


                      "Mark Rae [MVP]" <mark@markNOSPA Mrae.netwrote in message
                      news:%23oAe3IQv IHA.3384@TK2MSF TNGP03.phx.gbl. ..
                      "Andy B" <a_borka@sbcglo bal.netwrote in message
                      news:eKowu6PvIH A.5472@TK2MSFTN GP06.phx.gbl...
                      >
                      [top-posting corrected again]
                      >
                      >>It passes an object array (containing four elements) as the first
                      >>argument of the Add method of the datatable's Rows collection...
                      >>
                      >Is this the only way you can do it? or is there some other way.
                      >
                      Probably - there's usually more than one way to do most things in the .NET
                      Framework...
                      >
                      >I used NewDataTableRow () before... is this possible without sql server?
                      >
                      Sorry, I don't know. I always use the method I outlined...
                      >
                      >
                      --
                      Mark Rae
                      ASP.NET MVP
                      http://www.markrae.net

                      Comment

                      • George Ter-Saakov

                        #12
                        Re: using DataSet programatically without an sql server database

                        Looks like you did not create the table "Table".

                        At some point you need to have
                        DataTable dt = new DataTable();
                        DataSet.Tables["Table"] = dt;

                        Something like that.

                        George.

                        "Andy B" <a_borka@sbcglo bal.netwrote in message
                        news:Ojr5ZaQvIH A.576@TK2MSFTNG P05.phx.gbl...
                        >I saw it possible on an msdn article. Actually I think it was
                        >DataTable.NewR ow method home page in its example. Now, I tried to create a
                        >dataset with a table and 2 columns. When I ran the page I got this problem:
                        >
                        Server Error in '/' Application.
                        --------------------------------------------------------------------------------
                        >
                        Object reference not set to an instance of an object.
                        Description: An unhandled exception occurred during the execution of the
                        current web request. Please review the stack trace for more information
                        about the error and where it originated in the code.
                        >
                        Exception Details: System.NullRefe renceException: Object reference not set
                        to an instance of an object.
                        >
                        Source Error:
                        >
                        Line 20: DataColumn IDColumn = new DataColumn("ID" );
                        Line 21: IDColumn.DataTy pe = Type.GetType("S ystem.Int32");
                        Line 22: DataSet.Tables["Table"].Columns.Add(ID Column);
                        Line 23: DataColumn ItemColumn = new DataColumn("Ite m");
                        Line 24: ItemColumn.Data Type = typeof(String);
                        >
                        Source File: C:\Documents and Settings\Andy\M y Documents\Visua l Studio
                        2008\Projects\E ternityRecordsW ebsite\Contract s\WebForm1.aspx .cs Line:
                        22
                        >
                        Stack Trace:
                        >
                        [NullReferenceEx ception: Object reference not set to an instance of an
                        object.]
                        Contracts.WebFo rm1.Page_Load(O bject sender, EventArgs e) in C:\Documents
                        and Settings\Andy\M y Documents\Visua l Studio
                        2008\Projects\E ternityRecordsW ebsite\Contract s\WebForm1.aspx .cs:22
                        System.Web.Util .CalliHelper.Ev entArgFunctionC aller(IntPtr fp, Object o,
                        Object t, EventArgs e) +15
                        System.Web.Util .CalliEventHand lerDelegateProx y.Callback(Obje ct sender,
                        EventArgs e) +33
                        System.Web.UI.C ontrol.OnLoad(E ventArgs e) +99
                        System.Web.UI.C ontrol.LoadRecu rsive() +47
                        System.Web.UI.P age.ProcessRequ estMain(Boolean
                        includeStagesBe foreAsyncPoint, Boolean includeStagesAf terAsyncPoint) +1436
                        >
                        >
                        >
                        --------------------------------------------------------------------------------
                        Version Information: Microsoft .NET Framework Version:2.0.507 27.1433;
                        ASP.NET Version:2.0.507 27.1433
                        >
                        How do I fix it? The references to DataType = Type.GetType("S ystem.Int32")
                        and DataType=Typeof (String) were left that way on purpose for trying to
                        figure out the problem. I had both of the columns datatype set to
                        Typeof(...) but changed the IDColumns to Type.GetType(.. .) when I
                        originally got this problem. Any idea what the problem is? Is it possible
                        I can't add columns after the table is added to the dataset?
                        >
                        >
                        "Mark Rae [MVP]" <mark@markNOSPA Mrae.netwrote in message
                        news:%23oAe3IQv IHA.3384@TK2MSF TNGP03.phx.gbl. ..
                        >"Andy B" <a_borka@sbcglo bal.netwrote in message
                        >news:eKowu6PvI HA.5472@TK2MSFT NGP06.phx.gbl.. .
                        >>
                        >[top-posting corrected again]
                        >>
                        >>>It passes an object array (containing four elements) as the first
                        >>>argument of the Add method of the datatable's Rows collection...
                        >>>
                        >>Is this the only way you can do it? or is there some other way.
                        >>
                        >Probably - there's usually more than one way to do most things in the
                        >.NET Framework...
                        >>
                        >>I used NewDataTableRow () before... is this possible without sql server?
                        >>
                        >Sorry, I don't know. I always use the method I outlined...
                        >>
                        >>
                        >--
                        >Mark Rae
                        >ASP.NET MVP
                        >http://www.markrae.net
                        >
                        >

                        Comment

                        • Andy B

                          #13
                          Re: using DataSet programatically without an sql server database

                          Actually, I managed to figure out what the problem was. I was looking at the
                          DataTable.NewRo w() method example on msdn and writing sample code of my own
                          to see if I could get things to work right. Msdn used "Table" for the table
                          name and I used "Start". Go figure... So by instinct I used "Table" for all
                          of the references after naming the table something else....


                          "George Ter-Saakov" <gt-nsp@cardone.com wrote in message
                          news:uzS0HxQvIH A.1504@TK2MSFTN GP05.phx.gbl...
                          Looks like you did not create the table "Table".
                          >
                          At some point you need to have
                          DataTable dt = new DataTable();
                          DataSet.Tables["Table"] = dt;
                          >
                          Something like that.
                          >
                          George.
                          >
                          "Andy B" <a_borka@sbcglo bal.netwrote in message
                          news:Ojr5ZaQvIH A.576@TK2MSFTNG P05.phx.gbl...
                          >>I saw it possible on an msdn article. Actually I think it was
                          >>DataTable.New Row method home page in its example. Now, I tried to create a
                          >>dataset with a table and 2 columns. When I ran the page I got this
                          >>problem:
                          >>
                          >Server Error in '/' Application.
                          >--------------------------------------------------------------------------------
                          >>
                          >Object reference not set to an instance of an object.
                          >Description: An unhandled exception occurred during the execution of the
                          >current web request. Please review the stack trace for more information
                          >about the error and where it originated in the code.
                          >>
                          >Exception Details: System.NullRefe renceException: Object reference not
                          >set to an instance of an object.
                          >>
                          >Source Error:
                          >>
                          >Line 20: DataColumn IDColumn = new DataColumn("ID" );
                          >Line 21: IDColumn.DataTy pe = Type.GetType("S ystem.Int32");
                          >Line 22: DataSet.Tables["Table"].Columns.Add(ID Column);
                          >Line 23: DataColumn ItemColumn = new DataColumn("Ite m");
                          >Line 24: ItemColumn.Data Type = typeof(String);
                          >>
                          >Source File: C:\Documents and Settings\Andy\M y Documents\Visua l Studio
                          >2008\Projects\ EternityRecords Website\Contrac ts\WebForm1.asp x.cs Line:
                          >22
                          >>
                          >Stack Trace:
                          >>
                          >[NullReferenceEx ception: Object reference not set to an instance of an
                          >object.]
                          > Contracts.WebFo rm1.Page_Load(O bject sender, EventArgs e) in
                          >C:\Documents and Settings\Andy\M y Documents\Visua l Studio
                          >2008\Projects\ EternityRecords Website\Contrac ts\WebForm1.asp x.cs:22
                          > System.Web.Util .CalliHelper.Ev entArgFunctionC aller(IntPtr fp, Object o,
                          >Object t, EventArgs e) +15
                          > System.Web.Util .CalliEventHand lerDelegateProx y.Callback(Obje ct sender,
                          >EventArgs e) +33
                          > System.Web.UI.C ontrol.OnLoad(E ventArgs e) +99
                          > System.Web.UI.C ontrol.LoadRecu rsive() +47
                          > System.Web.UI.P age.ProcessRequ estMain(Boolean
                          >includeStagesB eforeAsyncPoint , Boolean includeStagesAf terAsyncPoint)
                          >+1436
                          >>
                          >>
                          >>
                          >--------------------------------------------------------------------------------
                          >Version Information: Microsoft .NET Framework Version:2.0.507 27.1433;
                          >ASP.NET Version:2.0.507 27.1433
                          >>
                          >How do I fix it? The references to DataType =
                          >Type.GetType(" System.Int32") and DataType=Typeof (String) were left that
                          >way on purpose for trying to figure out the problem. I had both of the
                          >columns datatype set to Typeof(...) but changed the IDColumns to
                          >Type.GetType(. ..) when I originally got this problem. Any idea what the
                          >problem is? Is it possible I can't add columns after the table is added
                          >to the dataset?
                          >>
                          >>
                          >"Mark Rae [MVP]" <mark@markNOSPA Mrae.netwrote in message
                          >news:%23oAe3IQ vIHA.3384@TK2MS FTNGP03.phx.gbl ...
                          >>"Andy B" <a_borka@sbcglo bal.netwrote in message
                          >>news:eKowu6Pv IHA.5472@TK2MSF TNGP06.phx.gbl. ..
                          >>>
                          >>[top-posting corrected again]
                          >>>
                          >>>>It passes an object array (containing four elements) as the first
                          >>>>argument of the Add method of the datatable's Rows collection...
                          >>>>
                          >>>Is this the only way you can do it? or is there some other way.
                          >>>
                          >>Probably - there's usually more than one way to do most things in the
                          >>.NET Framework...
                          >>>
                          >>>I used NewDataTableRow () before... is this possible without sql server?
                          >>>
                          >>Sorry, I don't know. I always use the method I outlined...
                          >>>
                          >>>
                          >>--
                          >>Mark Rae
                          >>ASP.NET MVP
                          >>http://www.markrae.net
                          >>
                          >>
                          >
                          >

                          Comment

                          Working...