all groups > dotnet compact framework > february 2005 >
You're in the

dotnet compact framework

group:

Databinding Exception



Databinding Exception victor
2/25/2005 5:21:16 PM
dotnet compact framework: hi guys
i got a very strange problem here. In my program, i create the databinding
between some textboxes ,comoboxes and a dataview. first i clear the
databinding then add the databinding again. then i changed the current row
in the dataview several times, the nullreferenceexception occurs.. really
dont why
any suggestion?
cheeers
victor



RE: Databinding Exception ilyatum NO[at]SPAM online.microsoft.com (
2/25/2005 7:01:29 PM
Most common reason for that problem is using control/binding row index as a
row index in DataTable.
Make sure you don't do that, use control/binding row index on DataView
controls are bound to instead.

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
[quoted text, click to view]
TK2MSFTNGXA02.phx.gbl!TK2MSFTNGXA01.phx.gbl!TK2MSFTNGXA03.phx.gbl!TK2MSFTNGP
08.phx.gbl!TK2MSFTNGP12.phx.gbl
[quoted text, click to view]
Re: Databinding Exception ilyatum NO[at]SPAM online.microsoft.com (
2/26/2005 6:50:26 PM
I see nothing wrong with this code fragment, but since it's not your entire
application, problem might be somewhere else.
Make sure you do not delete/modify rows from/in the DataTable directly,
only from/in DataView.
Also make sure DataTable is not modified from other threads.

Or come up with a repro (small, complete and compliable code) and post it
to the NG so I can investigate.

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
[quoted text, click to view]
TK2MSFTNGXA02.phx.gbl!TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp1
3.phx.gbl
[quoted text, click to view]
this.cboCustomerName.DataBindings.Add("SelectedValue",this.dvCall,"CustomerC
ode");
[quoted text, click to view]
this.dpScheduleStartTime.DataBindings.Add("Value",this.dvCall,"ScheduleStart
ed");
[quoted text, click to view]
this.dpScheduleEndTime.DataBindings.Add("Value",this.dvCall,"ScheduleEnded")
;
[quoted text, click to view]
this.txtAcutalStartTime.DataBindings.Add("Text",this.dvCall,"ActualStarted")
;
[quoted text, click to view]
this.cboCallStatus.DataBindings.Add("SelectedValue",this.dvCall,"CallStatusI
D");
[quoted text, click to view]
Re: Databinding Exception Victor
2/26/2005 8:41:12 PM
Hi Ilya
thanks for the reply. but i still get some confusion here. what do you mean
using control/binding row index as i row index in DataTable?
In my program. i write something like

public void AddDataBinding()
{
this.cboCustomerName.DataBindings.Add("SelectedValue",this.dvCall,"CustomerCode");
this.dpScheduleStartTime.DataBindings.Add("Value",this.dvCall,"ScheduleStarted");
this.dpScheduleEndTime.DataBindings.Add("Value",this.dvCall,"ScheduleEnded");

this.txtAcutalStartTime.DataBindings.Add("Text",this.dvCall,"ActualStarted");
this.txtAcutalEndTime.DataBindings.Add("Text",this.dvCall,"ActualEnded");

this.cboCallStatus.DataBindings.Add("SelectedValue",this.dvCall,"CallStatusID");

}

public void ClearDataBinding()
{
this.cboCustomerName.DataBindings.Clear();
this.dpScheduleStartTime.DataBindings.Clear();
this.dpScheduleEndTime.DataBindings.Clear();
this.cboCallStatus.DataBindings.Clear();
this.txtAcutalStartTime.DataBindings.Clear();
}

Did I do the right thing here?
Am i using the control/binding row index on DataView controls?

cheers
victor



















[quoted text, click to view]

Re: Databinding Exception ilyatum NO[at]SPAM online.microsoft.com (
2/27/2005 6:27:56 PM
That's a known bug in DataGrid discussed just recently.
As a workaround, add data to the DataTable directly.
To do it safely, create empty row, set all columns and add it to the table.
Or, better yet, use DataTable.Rows.Add() overload with array of objects to
set.
Do not set columns after row has been added.
I know it's confusing, sorry about that. This problem is fixed in CF V2.

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
[quoted text, click to view]
<Yd#vrx2GFHA.1136@TK2MSFTNGXA02.phx.gbl>
<e7CJNa9GFHA.3076@tk2msftngp13.phx.gbl>
<se6wTQDHFHA.1140@TK2MSFTNGXA02.phx.gbl>
[quoted text, click to view]
TK2MSFTNGXA02.phx.gbl!TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp1
3.phx.gbl
[quoted text, click to view]
Re: Databinding Exception ilyatum NO[at]SPAM online.microsoft.com (
2/27/2005 9:39:17 PM
That would work, but it's slower as grid would rebind every time you're
adding a row.
If performance is acceptable, you sure can do it this way.

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
[quoted text, click to view]
<Yd#vrx2GFHA.1136@TK2MSFTNGXA02.phx.gbl>
<e7CJNa9GFHA.3076@tk2msftngp13.phx.gbl>
<se6wTQDHFHA.1140@TK2MSFTNGXA02.phx.gbl>
<OeIz3ELHFHA.1096@tk2msftngp13.phx.gbl>
<tfg1PoPHFHA.1140@TK2MSFTNGXA02.phx.gbl>
[quoted text, click to view]
TK2MSFTNGXA02.phx.gbl!TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp1
3.phx.gbl
[quoted text, click to view]
Re: Databinding Exception Victor
2/27/2005 10:31:53 PM
hi Ilya
i found my problem is modify the underlying the datatable directly...
but i get a new problem there... after i new dataview.addnew() method.. i
get a indexofrangeexception in the datagrid..

can you help me out?
thank you
victor

[quoted text, click to view]

Re: Databinding Exception victor
2/28/2005 9:41:50 AM
hi Ilya..
I found another way to do this, but I am not sure it is the current one.
before I use dataview.addnew() , i set the datagrid.datasource = null.. then
i call addnew() and set the value to each column.. at last i set the
datagrid.datasource = dataview again.. seems the problem fixed.. is that a
possible way?
cheers
victor


[quoted text, click to view]
Re: Databinding Exception victor
2/28/2005 10:54:13 AM
HI Ilya
sorry i have another question.. u said in the pervious post that we should
do the changes in the dataview, not the underlying datatable. so can I call
the datarowview.row.rejectchanges() method?
i have a databinding between the dataview and datagrid. i change the value
in the datarowview then i call datarowview.canceledit() but the data will
not back to the oringal value...
is there some way to reject the changes to the dataview?
cheers
victor

[quoted text, click to view]
Re: Databinding Exception ilyatum NO[at]SPAM online.microsoft.com (
2/28/2005 5:55:51 PM
CancelEdit() won't work if you bind to a bunch of textboxes. It should be
OK to use RejectChanges() on the row instead.

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
[quoted text, click to view]
<Yd#vrx2GFHA.1136@TK2MSFTNGXA02.phx.gbl>
<e7CJNa9GFHA.3076@tk2msftngp13.phx.gbl>
<se6wTQDHFHA.1140@TK2MSFTNGXA02.phx.gbl>
<OeIz3ELHFHA.1096@tk2msftngp13.phx.gbl>
<tfg1PoPHFHA.1140@TK2MSFTNGXA02.phx.gbl>
<OE$sEzQHFHA.2616@tk2msftngp13.phx.gbl>
<C01OLTRHFHA.1140@TK2MSFTNGXA02.phx.gbl>
[quoted text, click to view]
TK2MSFTNGXA02.phx.gbl!TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP1
2.phx.gbl
[quoted text, click to view]
Re: Databinding Exception ilyatum NO[at]SPAM online.microsoft.com (
2/28/2005 11:43:29 PM
I would be happy to help, but I'll need short, but complete and compliable
repro I can actually run.
If you'd come up with one, I'll take a look.

Oh, by the way, there's no need to loop through rows to update SQL CE.
Just use SqlCeDataAdapter.Update(), it would it for you, just set
update/delete/insert commands as needed or use CommandBuilder (works if you
have primary key).

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
[quoted text, click to view]
<Yd#vrx2GFHA.1136@TK2MSFTNGXA02.phx.gbl>
<e7CJNa9GFHA.3076@tk2msftngp13.phx.gbl>
<se6wTQDHFHA.1140@TK2MSFTNGXA02.phx.gbl>
<OeIz3ELHFHA.1096@tk2msftngp13.phx.gbl>
<tfg1PoPHFHA.1140@TK2MSFTNGXA02.phx.gbl>
<OE$sEzQHFHA.2616@tk2msftngp13.phx.gbl>
<C01OLTRHFHA.1140@TK2MSFTNGXA02.phx.gbl>
<#aHchbRHFHA.3200@TK2MSFTNGP12.phx.gbl>
<sW4CC7bHFHA.2824@TK2MSFTNGXA02.phx.gbl>
[quoted text, click to view]
TK2MSFTNGXA02.phx.gbl!cpmsftngxa10.phx.gbl!TK2MSFTFEED02.phx.gbl!TK2MSFTNGP0
8.phx.gbl!TK2MSFTNGP15.phx.gbl
[quoted text, click to view]
SQLCEManager.GetDatabaseString(row["CustomerContactMobilePhone"].ToString())
);
[quoted text, click to view]
SQLCEManager.GetDatabaseString(row["CustomerContactMobilePhone"].ToString())
,
[quoted text, click to view]
this.txtCustomerContactName.DataBindings.Add("Text",dvCustomerContacts,"Cust
omerContactName");
[quoted text, click to view]
this.txtCustomerContactposition.DataBindings.Add("Text",dvCustomerContacts,"
CustomerContactposition");
[quoted text, click to view]
Re: Databinding Exception ilyatum NO[at]SPAM online.microsoft.com (
3/1/2005 2:05:13 AM
You can just remove "online" from my e-mail below. However, it would be
best if you can cut out irrelevant code.
I certainly do not need any confidential data you might have in your data
base, besides short repro speeds up the process.

For example, here's a repro for IndexOutOfRange exception in a grid.
All I had to do is to drop grid and button on a form and add 8 lines of
code.
Just click of a button twice and you'll see the exception:

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

namespace SmartDeviceApplication1
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.MainMenu mainMenu1;

private DataTable dt;

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

//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
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.mainMenu1 = new System.Windows.Forms.MainMenu();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.button1 = new System.Windows.Forms.Button();
//
// dataGrid1
//
this.dataGrid1.Location = new System.Drawing.Point(0, 8);
this.dataGrid1.Size = new System.Drawing.Size(240, 200);
this.dataGrid1.Text = "dataGrid1";
//
// button1
//
this.button1.Location = new System.Drawing.Point(96, 224);
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.Controls.Add(this.button1);
this.Controls.Add(this.dataGrid1);
this.Menu = this.mainMenu1;
this.Text = "Form1";

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>

static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{
if (dt == null)
{
dt = new DataTable();
dt.Columns.Add("Column");
this.dataGrid1.DataSource = dt;
}
dt.DefaultView.AddNew();
}
}
}


Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
[quoted text, click to view]
<Yd#vrx2GFHA.1136@TK2MSFTNGXA02.phx.gbl>
<e7CJNa9GFHA.3076@tk2msftngp13.phx.gbl>
<se6wTQDHFHA.1140@TK2MSFTNGXA02.phx.gbl>
<OeIz3ELHFHA.1096@tk2msftngp13.phx.gbl>
<tfg1PoPHFHA.1140@TK2MSFTNGXA02.phx.gbl>
<OE$sEzQHFHA.2616@tk2msftngp13.phx.gbl>
<C01OLTRHFHA.1140@TK2MSFTNGXA02.phx.gbl>
<#aHchbRHFHA.3200@TK2MSFTNGP12.phx.gbl>
<sW4CC7bHFHA.2824@TK2MSFTNGXA02.phx.gbl>
<OI48ESeHFHA.2936@TK2MSFTNGP15.phx.gbl>
<aa2oT9eHFHA.1140@TK2MSFTNGXA02.phx.gbl>
[quoted text, click to view]
TK2MSFTNGXA02.phx.gbl!cpmsftngxa10.phx.gbl!TK2MSFTFEED02.phx.gbl!TK2MSFTNGP0
8.phx.gbl!TK2MSFTNGP15.phx.gbl
[quoted text, click to view]
Re: Databinding Exception victor
3/1/2005 11:26:13 AM
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,"CustomerContactName");
this.txtCustomerContactposition.DataBindings.Add("Text",dvCustomerContacts,"CustomerContactposition");
this.txtCustomerContactmobilePhone.DataBindings.Add("Text",dvCustomerContacts,"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;

Re: Databinding Exception victor
3/1/2005 1:11:29 PM
Hi Ilya
would you mind give me you email address so that i can send the project
file...? In order to run the program.. i think i have to send you the whole
form..

thank you
victor

[quoted text, click to view]
AddThis Social Bookmark Button