DateTime and DbNull Values

TeeChart for Microsoft Visual Studio .NET, Xamarin Studio (Android, iOS & Forms) & Monodevelop.
Post Reply
tomster
Newbie
Newbie
Posts: 20
Joined: Tue Aug 02, 2005 4:00 am
Location: Stockholm, Sweden

DateTime and DbNull Values

Post by tomster » Thu Oct 06, 2005 7:25 am

Hi there, everybody. Using the 2.0.2078.14844 build of TeeChart v2 I discovered some more problems with DbNull values.

I managed to get it to display the label for a certain point as stated in a previous thread here. Well, now I discovered that when using DateTime values from a DataTable as XValues for a series and using these as DateTime values (DateTime = true) and if that data contains DbNull values, the dates are corrupted, lost, whatever.

So, in a demo project I am able to demonstrate this very well, the code of which I post below. What happens is that if the dataset contains DbNull values, the dates become 1900-01-01 etc instead of their actual values. In the demo project you can see both behaviours by changing the variable withNulls to true or false between runs.

Is this a known bug or am I just doing something seriously wrong?

Cheers,
Tommie

Code: Select all

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace TeeChartLabelProblemDemo
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private Steema.TeeChart.TChart tChart1;
		private DevExpress.XtraEditors.SimpleButton btnAddSeries;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private bool withNulls = true;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.tChart1 = new Steema.TeeChart.TChart();
			this.btnAddSeries = new DevExpress.XtraEditors.SimpleButton();
			this.SuspendLayout();
			// 
			// tChart1
			// 
			// 
			// tChart1.Header
			// 
			this.tChart1.Header.Lines = new string[] {
														 "TeeChart"};
			// 
			// tChart1.Legend
			// 
			// 
			// tChart1.Legend.Title
			// 
			// 
			// tChart1.Legend.Title.Font
			// 
			this.tChart1.Legend.Title.Font.Bold = true;
			// 
			// tChart1.Legend.Title.Pen
			// 
			this.tChart1.Legend.Title.Pen.Visible = false;
			this.tChart1.Location = new System.Drawing.Point(8, 8);
			this.tChart1.Name = "tChart1";
			this.tChart1.Size = new System.Drawing.Size(576, 576);
			this.tChart1.TabIndex = 0;
			// 
			// btnAddSeries
			// 
			this.btnAddSeries.Location = new System.Drawing.Point(640, 16);
			this.btnAddSeries.Name = "btnAddSeries";
			this.btnAddSeries.Size = new System.Drawing.Size(96, 32);
			this.btnAddSeries.TabIndex = 1;
			this.btnAddSeries.Text = "Add Series";
			this.btnAddSeries.Click += new System.EventHandler(this.btnAddSeries_Click);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(888, 598);
			this.Controls.Add(this.btnAddSeries);
			this.Controls.Add(this.tChart1);
			this.Name = "Form1";
			this.Text = "Form1";
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private DataTable dt = null;
		private void InitDataSet() 
		{
			dt = new DataTable();
			AddLabelColumn(dt);
		}

		private void AddLabelColumn(DataTable table)
		{
			table.Columns.Add(new DataColumn("Label", System.Type.GetType("System.DateTime")));
		}

		private void AddColumn(DataTable table, string columnName) 
		{
			table.Columns.Add(new DataColumn(columnName, System.Type.GetType("System.Decimal")));
		}

		private void SetData(DataTable table) 
		{
			Random rnd = new Random();

			table.Rows.Clear();
			for(int i = 0; i < 50; i++) 
			{
				DataRow dr = table.NewRow();
				foreach(DataColumn dc in table.Columns) 
				{
					if(dc.DataType == System.Type.GetType("System.DateTime")) 
					{
						dr[dc] = DateTime.Now.AddDays(i);
					} 
					else if(dc.DataType == System.Type.GetType("System.Decimal")) 
					{
						
						if((i < 15 || i > 40) && withNulls)
						{
							dr[dc] = DBNull.Value; 
						} 
						else 
						{
							dr[dc] = Convert.ToDecimal(rnd.Next(10, 100));
						}
					}
				}
				table.Rows.Add(dr);
			}
		}

		private void btnAddSeries_Click(object sender, System.EventArgs e)
		{
			if(dt == null) 
			{
				InitDataSet();
			}
			if(dt.Columns.Count == 0) 
			{
				AddLabelColumn(dt);
			} 
			Steema.TeeChart.Styles.FastLine fl = new Steema.TeeChart.Styles.FastLine(tChart1.Chart);
			AddColumn(dt, fl.Title);
			SetData(dt);
			fl.DataSource = dt;

			fl.XValues.DataMember = "Label";
			fl.XValues.DateTime = true;
			
			fl.XValues.FillSequence();			
			fl.YValues.DataMember = fl.Title;
			
			fl.IgnoreNulls = false;

			fl.CheckDataSource(); 
			tChart1.Series.Add(fl);
			tChart1.Axes.Bottom.Labels.Style = Steema.TeeChart.AxisLabelStyle.Value;
		}
	}
}

Christopher
Site Admin
Site Admin
Posts: 1349
Joined: Thu Jan 01, 1970 12:00 am
Location: Riudellots de la Selva, Catalonia
Contact:

Post by Christopher » Thu Oct 06, 2005 9:05 am

Hello tomster,

I've been able to reproduce your problem with DateTime values and DBNull and have fixed it. The fix also fixes the problem you were experiencing with Series Labels and DBNull values.

A new Debug build with these fixes in it will be released later today and will be available from the Customer Download pages.
Thank you!

Christopher Ireland (Steema crew)
Please be aware of the newsgroup archives:
http://www.teechart.net/support/search.php
http://groups.google.com
http://codenewsfast.com/

DougJ
Newbie
Newbie
Posts: 5
Joined: Mon Oct 04, 2004 4:00 am

Re: DateTime and DbNull Values

Post by DougJ » Tue Apr 07, 2015 1:07 pm

I have this same issue with V1 Build 1.1.2531 I was wondering if it has been resolved in later versions.

Christopher
Guru
Posts: 1603
Joined: Fri Nov 15, 2002 12:00 am

Re: DateTime and DbNull Values

Post by Christopher » Wed Apr 08, 2015 8:25 am

Yes, this works as expected in the latest publicly available version of TeeChart for .NET. Running the code Tommie posted gives this chart:
dbnulls.png
dbnulls.png (38.51 KiB) Viewed 7892 times
Best Regards,
Christopher Ireland / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Instructions - How to post in this forum

Post Reply