Here I will post problems I and my colleagues met and solutions we found.

Wednesday, March 16, 2005

Expressions, Parent, Master-details

This is not so well known problem. Probably because it is weird to do it so. Anyway, it does not work.

I had master/detail relationship between two tables. In detail table I had expressions like
Parent("groups_users").Name
Which means I tried to get value from parent record using relation "groups_users". Unfortunately, when I inserted record into parent table, and then inserted record into detail table without posting data to server, I got exception that "Original value is not available". It is true, that original value is not available for new records, it is just unclear why current value is not taken.

I could not find workaround with expressions and gave up, retrieving data manually.

ReadOnly Exception Error when Inserting with an Expression Column

This is well known problem. That means this is an issue for many people and we could not pass it.

The problem is that if DataTable has expression columns, and Insert (or Update) SQLCommand returns some values, exception will be raised that ReadOnly property cannot be changed.

There is workaround. I may try manually refresh data and updat field values. Something like this:


SqlDataReader r = employeeRecordCommand.ExecuteReader(CommandBehavior.SingleRow);
if (r.HasRows)
{
r.Read();
for (int i = 0; i < r.FieldCount; i++)
{
string s = r.GetName(i);
if (string.Compare(s, "contact_id", true) != 0)
{
if (dataRow.Table.Columns.Contains(s))
dataRow[s] = r[i];
}
}
}
r.Close();


Here is the link to description of the problem:
http://www.error-bank.com/microsoft.public.dotnet.framework.windowsforms.databinding/_35_HCSYiV0DHA.2328@TK2MSFTNGP10.phx.gbl_Thread.aspx

Wednesday, March 02, 2005

DesignMode and LicenseManager.UsageMode in constructor and Event handlers

Today I found why my forms behaved weirdly in design-time.

I have two forms, one is parent in terms of inheritance, another is child one. Example:

public class Form1 : System.Windows.Forms.Form
{
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
// DesignMode = false if you open ChildForm
MessageBox.Show("Form1 constructor DesignMode = " + DesignMode.ToString());
//Form1 constructor LicenseManager.UsageMode == DesignMode
MessageBox.Show("Form1 constructor LicenseManager.UsageMode = " + LicenseManager.UsageMode.ToString());}


private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
}


private void Form1_Load(object sender, System.EventArgs e)
{
// DesignMode = true if you open ChildForm
MessageBox.Show("Form1_Load DesignMode = " + DesignMode.ToString());
//Form1 constructor LicenseManager.UsageMode == RunTime
MessageBox.Show("Form1_Load constructor LicenseManager.UsageMode = " + LicenseManager.UsageMode.ToString());}

}


public class ChildForm : DesignModeExample.Form1
{
public ChildForm()
{
// This call is required by the Windows Form Designer.
InitializeComponent();
}}

If you open Child form in design time you will see two dialog. One will say that DesignMode in Form1 constructor = false. Another that DesignMode in Load event = true. LicenseManager.UsageMode works opposite. It works properly in constructor but lies in Load event handler.

Why is it so? I don't know, but I just tested it. It works this way.