Subject: Re: Databinding Exception
Date: Tue, 1 Mar 2005 11:26:13 +1300
Lines: 343
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2527
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2527
X-RFC2646: Format=Flowed; Original
Message-ID: <
[email protected]>
Newsgroups: microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: 203.97.94.50
Path: TK2MSFTNGXA02.phx.gbl!cpmsftngxa10.phx.gbl!TK2MSFTFEED02.phx.gbl!TK2MSFTNGP0
8.phx.gbl!TK2MSFTNGP15.phx.gbl
Xref: TK2MSFTNGXA02.phx.gbl microsoft.public.dotnet.framework.compactframework:72101
X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework
Hi Ilya,
First I want to say sorry to bother you here. I wrote some threads about the
NullReferenceException occurred in my program. And you replied them. Now I
have change all bindings to dataview and do not modify underlying datatable
directly.. but the NullReferenceException still will come up.. I attached
the codewith the NullReferenceException here¡ would you mind have a little
look and give me some advice..
Thank you very much
victor
public class NewContactForm : System.Windows.Forms.Form
{
# region User defined variables
private bool isNewMode = false;
private ReadOnlyDataGrid dgCustomerContacts;
private int customerCode;
private DataView dvCustomerContacts;
private SQLCEManager manager;
#endregion
public NewContactForm(int customerCode, ref DataTable dt)
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
SIPCustomers = new Microsoft.WindowsCE.Forms.InputPanel();
SIPCustomers.EnabledChanged +=new
EventHandler(SIPCustomers_EnabledChanged);
manager = new SQLCEManager();
this.customerCode = customerCode;
this.dvCustomerContacts = new DataView(dt);
SetGridStyle();
this.dgCustomerContacts.DataSource = dvCustomerContacts;
if (dvCustomerContacts.Count > 0)
{
this.dgCustomerContacts.Select(0);
}
AddCustomerContactDataBindings();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
//delete windows generate code
private void SaveChanges()
{
try
{
foreach(DataRow row in this.dvCustomerContacts.Table.Rows)
{
if (row.RowState == DataRowState.Added)
{
string cmd = String.Format("INSERT INTO CustomerContact" +
"(CustomerCode, " +
"CustomerContactName, " +
"CustomerContactposition, " +
"CustomerContactMobilePhone) VALUES (" +
"'{0}','{1}','{2}','{3}')",
SQLCEManager.GetDatabaseString(row["CustomerCode"].ToString()),
SQLCEManager.GetDatabaseString(row["CustomerContactName"].ToString()),SQLCEManager.GetDatabaseString(row["CustomerContactposition"].ToString()),SQLCEManager.GetDatabaseString(row["CustomerContactMobilePhone"].ToString())
);
manager.ExecuteNonQuery(cmd);
}
else if (row.RowState == DataRowState.Deleted)
{
row.RejectChanges();
string cmd = String.Format("DELETE FROM CustomerContact WHERE
CustomerContactCode = '{0}'",row["CustomerContactCode"]);
manager.ExecuteNonQuery(cmd);
}
else if (row.RowState == DataRowState.Modified )
{
string cmd = String.Format("UPDATE CustomerContact " +
"SET CustomerCode ={0}, "+
"CustomerContactName ='{1}', " +
"CustomerContactposition ='{2}', " +
"CustomerContactMobilePhone ='{3}'" +
"WHERE CustomerContactCode = '{4}'",
SQLCEManager.GetDatabaseString(row["CustomerCode"].ToString()),
SQLCEManager.GetDatabaseString(row["CustomerContactName"].ToString()),SQLCEManager.GetDatabaseString(row["CustomerContactposition"].ToString()),SQLCEManager.GetDatabaseString(row["CustomerContactMobilePhone"].ToString())
,SQLCEManager.GetDatabaseString(row["CustomerContactCode"].ToString())
);
manager.ExecuteNonQuery(cmd);
}
}
this.dvCustomerContacts.Table.AcceptChanges();
}
catch(SqlCeException ex)
{
MessageBox.Show(ex.Message);
}
finally
{
manager.Close();
}
}
private void SetGridStyle()
{
this.dgCustomerContacts.RowHeadersVisible = false;
this.dgCustomerContacts.ContextMenu = this.mnuDelContact;
this.pnlCustomerDetails.Controls.Add(this.dgCustomerContacts);
DataGridTableStyle table = new DataGridTableStyle();
table.MappingName = "CustomerContacts";
DataGridColumnStyle cName = new DataGridTextBoxColumn ();
cName.HeaderText = "Name";
cName.MappingName = "CustomerContactName";
cName.Width = 150;
DataGridColumnStyle cPosition = new DataGridTextBoxColumn();
cPosition.HeaderText = "Position";
cPosition.MappingName = "CustomerContactposition";
cPosition.Width = 77;
table.GridColumnStyles.Add(cName);
table.GridColumnStyles.Add(cPosition);
this.dgCustomerContacts.TableStyles.Add(table);
}
private void AddCustomerContactDataBindings()
{
if (dvCustomerContacts != null)
{
this.txtCustomerContactName.DataBindings.Add("Text",dvCustomerContacts,"Cust
omerContactName");this.txtCustomerContactposition.DataBindings.Add("Text",dvCustomerContacts,"
CustomerContactposition");this.txtCustomerContactmobilePhone.DataBindings.Add("Text",dvCustomerContact
s,"CustomerContactmobilePhone");
}
}
private void CleaCustomerContactDataBindings()
{
this.txtCustomerContactmobilePhone.DataBindings.Clear();
this.txtCustomerContactName.DataBindings.Clear();
this.txtCustomerContactposition.DataBindings.Clear();
this.txtCustomerContactName.Text = "";
this.txtCustomerContactposition.Text = "";
this.txtCustomerContactmobilePhone.Text = "";
}
private void NewContactForm_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
this.TopLevelControl.Focus();
if (this.dvCustomerContacts.Count > 0
&& this.dgCustomerContacts.CurrentRowIndex >= 0)
{
this.dvCustomerContacts[this.dgCustomerContacts.CurrentRowIndex].EndEdit();
}
isNewMode = false;
SaveChanges();
}
private void tbCustomerContact_ButtonClick(object sender,
System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
if (e.Button == this.btnAddContact )
{
if (isNewMode == false)
{
isNewMode = true;
CleaCustomerContactDataBindings();
this.txtCustomerContactName.Focus();
this.btnAddContact.ImageIndex = 1;
}
else
{
isNewMode = false;
this.txtCustomerContactName.Text = "";
this.txtCustomerContactmobilePhone.Text = "";
this.txtCustomerContactposition.Text = "";
AddCustomerContactDataBindings();
this.btnAddContact.ImageIndex = 0;
this.TopLevelControl.Focus();
}
}
else if (e.Button == this.btnSaveContact)
{
if (isNewMode == true)
{
if (this.txtCustomerContactName.Text != "")
{
DataRow dtRow = this.dvCustomerContacts.Table.NewRow();
dtRow["CustomerCode"] = this.customerCode;
dtRow["CustomerContactName"] = this.txtCustomerContactName.Text ;
dtRow["CustomerContactposition"] =
this.txtCustomerContactposition.Text;
dtRow["CustomerContactmobilePhone"] =
this.txtCustomerContactmobilePhone.Text;
this.dvCustomerContacts.Table.Rows.Add(dtRow);
this.dgCustomerContacts.CurrentRowIndex = dvCustomerContacts.Count -
this.dgCustomerContacts.Select(this.dgCustomerContacts.CurrentRowIndex);
this.dvCustomerContacts[this.dgCustomerContacts.CurrentRowIndex].EndEdit();
this.txtCustomerContactName.Focus();
AddCustomerContactDataBindings();
isNewMode = false;
this.TopLevelControl.Focus();
this.btnAddContact.ImageIndex = 0;
}
else
{
MessageBox.Show("Please enter the contact's
name.","Warning",MessageBoxButtons.OK,MessageBoxIcon.Exclamation,MessageBoxD
efaultButton.Button1);
}
}
else
{
if (this.dvCustomerContacts.Count > 0)
{
int iRowIndex = this.dgCustomerContacts.CurrentRowIndex;
this.dvCustomerContacts[iRowIndex].EndEdit();
this.TopLevelControl.Focus();
this.dgCustomerContacts.Select(iRowIndex);
}
}
}
}
private void TextBox_GotFocus(object sender, System.EventArgs e)
{
if (!this.SIPCustomers.Enabled)
{
this.SIPCustomers.Enabled = true;
}
}
private void TextBox_LostFocus(object sender, System.EventArgs e)
{
if (this.SIPCustomers.Enabled)
{
this.SIPCustomers.Enabled = false;
}
}
private void SIPCustomers_EnabledChanged(object sender, System.EventArgs
e)
{
if (this.SIPCustomers.Enabled)
{
this.pnlCustomerDetails.Top -= this.SIPCustomers.Bounds.Height;
}
else
{
this.pnlCustomerDetails.Top += this.SIPCustomers.Bounds.Height;
}
}
private void mnuDel_Click(object sender, System.EventArgs e)
{
if (MessageBox.Show("Do you really want to delete this
contact?","Confirm",MessageBoxButtons.YesNo,MessageBoxIcon.Exclamation,Messa
geBoxDefaultButton.Button1)
== DialogResult.Yes)
{
int iRowIndex = this.dgCustomerContacts.CurrentRowIndex;
if (isNewMode)
{
MessageBox.Show("You can not delete and add contact at the same
time.");
}
else
{
this.dvCustomerContacts.Delete(iRowIndex);
this.TopLevelControl.Focus();
}
}
}
}