• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

DataTable Class

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

    DataTable Class

    Msdn DataTable Class:
    https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx
    DataTable Examples:
    http://www.dotnetperls.com/datatable

    I’d like to use a data table as a multidimensional dynamic array inside ninja 7 but have been unable to implement it. Does anyone have any experience with this or another data structure that will store vars like excel? ( In columns and rows and is dynamic )
    Initializing and storing vars in the table is no problem. Reading the data is a problem. The MSDN recommended syntax doesn’t work inside ninja.
    Code:
    using System.Data;
            protected override void OnBarUpdate()
            {
    			// Declare DataTable
    			DataTable table = new DataTable();
    			// Add columns 
    			table.Columns.Add("Dosage", typeof(int));
    			table.Columns.Add("Drug", typeof(string));
    			table.Columns.Add("Patient", typeof(string));
    			table.Columns.Add("Date", typeof(DateTime));
    
    			// Add five DataRows.
    			table.Rows.Add(25, "Indocin", "David", DateTime.Now);
    			table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
    			table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
    			table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
    			table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
    			
    
    			// no matter what syntax I try I'm unable to read the data
    			//	Print(table.Rows[0][0]);
    	
            }
    Thanks for any pointers or advice!
    Westsider

    #2
    Any errors being reported?

    Also, did you try DotNetPerls method for looping through the data to write it?

    DataTable is very dynamic in that if you want data out of it, you need to get the type for it as well.

    Code:
    table.Rows[0].Field<string>(0)
    Last edited by Calonious; 04-26-2016, 08:36 AM.

    Comment


      #3
      Hi westsider,

      Calonious has the right idea. The item likely needs to be converted to a string.

      Are you getting any error messages in the Log tab of the Control Center?
      If so, what do these say?
      Chelsea B.NinjaTrader Customer Service

      Comment


        #4
        Originally posted by Calonious View Post
        Any errors being reported?

        Also, did you try DotNetPerls method for looping through the data to write it?

        DataTable is very dynamic in that if you want data out of it, you need to get the type for it as well.

        Code:
        table.Rows[0].Field<string>(0)
        Thanks for looking at my issue!. Yes I did try every method on DotNetPerls including looping through. In the case of
        Code:
        foreach (DataRow row in table.Rows)
        				{
        					// ... Write value of first field as integer.
        					Print( table.Rows[0].Field<string>(0) );
        				}
        I get an error -- CS1061 - click for info
        'System.Data.DataRow' does not contain a definition for 'Field' and no extension method 'Field' accepting a first argument of type 'System.Data.DataRow' could be found (are you missing a using directive or an assembly reference?)

        Comment


          #5
          I assume you have added the System.Data and System.Data.DataSetExtensions
          reference to your NT Editor correct?

          Field is an extension in the latter reference. If you don't wanna use that, then try using Item[string] instead to get the cell value -
          https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx
          Last edited by Calonious; 04-26-2016, 09:01 AM.

          Comment


            #6
            Ahh - Sorry, Didn't realize I needed System.Data.DataSetExtensions - when I add this I get the error:

            The type or namespace name 'DataSetExtensions' does not exist in the namespace 'System.Data' (are you missing an assembly reference?)

            I'm assuming I need add this assembly reference but haven't done this before.

            Comment


              #7
              Found it here and have added it successfully.

              C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\ System.Data.DataSetExtensions.dll

              Comment


                #8
                Are you still getting compile errors now? Or are you able to move forward to testing?

                Comment


                  #9
                  Moving forward now. Thanks very much for the help.

                  I was missing the compiler directive. Here is the complete code if someone in the future wants to use a datatable;

                  Code:
                  // add these references
                       using System.Data;
                       using System.Data.DataSetExtensions;
                  
                  			// Declare DataTable
                  			DataTable table = new DataTable();
                  			// Add columns 
                  			table.Columns.Add("Dosage", typeof(int));
                  			table.Columns.Add("Drug", typeof(string));
                  			table.Columns.Add("Patient", typeof(string));
                  			table.Columns.Add("Date", typeof(DateTime));
                  
                  			// Add five DataRows.
                  			table.Rows.Add(25, "Indocin", "David", DateTime.Now);
                  			table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
                  			table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
                  			table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
                  			table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
                  			
                                          // Loop thru to read the data
                  			foreach (DataRow row in table.Rows)
                  				{
                  					Print(row.Field<int>(0));
                  				}

                  Comment


                    #10
                    Thanks so much Calonious. I was stuck on that one for a while !!! I really appreciate the help.

                    Here is the full loop and print Syntax to read each column and row

                    Code:
                    foreach (DataRow row in table.Rows)
                    	{
                    		Print("Dose: " +row.Field<int>(0) +" Drug: " + row.Field<string>(1)+
                    			"Patient: " +row.Field<string>(2) +" Date: " + row.Field<DateTime>(3));			
                    	}

                    Comment


                      #11
                      Well, I can't get this to work. It compiles just fine, but when I run the script I get the following error message on the Output window: "AssemblyResolveEvent handlers cannot return Assemblies loaded for reflection only."

                      Any suggestions as to how to fix that ?
                      Last edited by spottysallrite; 04-15-2018, 04:00 PM. Reason: clarification

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by PN720, 12-08-2018, 01:20 PM
                      1 response
                      26 views
                      0 likes
                      Last Post NinjaTrader_PatrickG  
                      Started by Mr.Trader, Today, 04:11 AM
                      1 response
                      12 views
                      0 likes
                      Last Post NinjaTrader_PaulH  
                      Started by Traderjoe2017, Today, 06:17 AM
                      0 responses
                      5 views
                      0 likes
                      Last Post Traderjoe2017  
                      Started by tony98, Today, 06:05 AM
                      1 response
                      4 views
                      0 likes
                      Last Post NinjaTrader_PatrickG  
                      Started by 3lliot, Today, 01:00 AM
                      2 responses
                      12 views
                      0 likes
                      Last Post 3lliot
                      by 3lliot
                       
                      Working...
                      X