Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Webservices System.NullReferenceException
 

Webservices System.NullReferenceException


I have 2 webservice methods Load Window and Save Window.
When I call the webservice method frommy formsynchronously or assynchronously i am receiving the following error:

(System.NullReferenceException: Object reference not set to an instance)

I am being able to call the loadWindow webservice method without any problem but when i pass the dataset as parameter in the saveWindow im receiving the error above. Any solution will be appreciated.

//Form

private void SaveData()
{
try
{
SYS_WS sysws = new SYS_WS();
sysws.SaveWindowCompleted +=new SaveWindowCompletedEventHandler(sysws_SaveWindowCompleted);
sysws.SaveWindowAsync((SYSDataset)dsSystem.GetChanges());
}
catch (Exception ex)
{
throw (ex);
}
}

private void sysws_SaveWindowCompleted(object sender,
SaveWindowCompletedEventArgs e)
{
try
{
if (e.Error != null)
{
return;
}
this.dsSystem.AcceptChanges();

}
catch (Exception ex)
{
throw ex;
}
}




//Webservice Methods

[WebMethod(EnableSession = true)]
public bool SaveWindow(SYSDataset ds)
{
try
{
WindowController cont = new WindowController();
cont.SaveWindows(ds);
return true;
}
catch (Exception ex)
{
throw (ex);
}
}


[WebMethod(EnableSession = true)]
public SYSDataset LoadWindow(string WindowID)
{
WindowController cont = new WindowController();
return cont.LoadWindow(WindowID);
}
</pre>
SEVM  Monday, August 31, 2009 12:10 PM

Data in a DataRow has several different versions. First, there's the original version. Then, when it's being edited, it has a Proposed version and once it's done being edited, that becomes the Current version. Sometimes when editing, the row is left in the Proposed state and the Edit needs to be ended programmatically.

Here's a method I *always* call before I attempt to check for .HasChanges() before saving data:

protected virtual void CommitProposedChanges(DataSet ds)
{
	if (ds == null)
		return;

	for (int nTable = 0; nTable < ds.Tables.Count; nTable++)
	{
		for (int nRow = 0; nRow < ds.Tables[nTable].Rows.Count; nRow++)
		{
			if (ds.Tables[nTable].Rows[nRow].HasVersion(DataRowVersion.Proposed))
			{
				ds.Tables[nTable].Rows[nRow].EndEdit();
			}
		}
	}
}


~~Bonnie Berent [C# MVP]
  • Marked As Answer bySEVM Saturday, September 05, 2009 12:21 PM
  •  
BonnieB  Thursday, September 03, 2009 11:32 PM
Hi,
Before save your dataset just ensure

  if (ds.Tables[0].Rows.Count > 0 && ds.Tables[0] != null)
                {
                    cont.SaveWindows(ds);
                    return true;
                }
its not null .i hope it will help you
Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
Gnanadurai  Monday, August 31, 2009 12:19 PM
still same problem before calling the webservices the error

soap System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.NullReferenceException: Object reference not set to an instance of an object.

i checked the rows.count its 1....

anyone has any other suggestions please its important for me to solve this issue soon

the load method is loading the data

---- Stack Trace ----
RTR.SYS_V.Setup.frmWindow.SaveData()
frmWindow.cs: line 0091, col 17, IL 0079
RTR.SYS_V.Setup.frmWindow.btnSave_Click(sender As Object, e As EventArgs)
frmWindow.cs: line 0202, col 13, IL 0001
System.Windows.Forms.Control.OnClick(e As EventArgs)
MAIN.EXE: N 00111
System.Windows.Forms.Button.OnClick(e As EventArgs)
MAIN.EXE: N 00073
System.Windows.Forms.Button.OnMouseUp(mevent As MouseEventArgs)
MAIN.EXE: N 00171
System.Windows.Forms.Control.WmMouseUp(m As Message&, button As MouseButtons, clicks As Int32)
MAIN.EXE: N 00654
System.Windows.Forms.Control.WndProc(m As Message&)
MAIN.EXE: N 8788613
System.Windows.Forms.ButtonBase.WndProc(m As Message&)
MAIN.EXE: N 8807204
System.Windows.Forms.Button.WndProc(m As Message&)
MAIN.EXE: N 00031
System.Windows.Forms.ControlNativeWindow.OnMessage(m As Message&)
MAIN.EXE: N 00015
System.Windows.Forms.ControlNativeWindow.WndProc(m As Message&)
MAIN.EXE: N 00048
System.Windows.Forms.NativeWindow.Callback(hWnd As IntPtr, msg As Int32, wparam As IntPtr, lparam As IntPtr)
MAIN.EXE: N 00089
SEVM  Monday, August 31, 2009 7:29 PM
Hi,
Check the dataset contain any data or empty rows..
Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
Gnanadurai  Tuesday, September 01, 2009 4:38 AM

Hi Sir,
No the dataset contains a row, cuz i am trying to load a record then modify it and save.
The line that im receiving the error is before callingthe save webservice method.

SYS_WS sysws = new SYS_WS();
-->sysws.SaveWindowCompleted +=new SaveWindowCompletedEventHandler(sysws_SaveWindowCompleted);

What do you suggest the error can be?
Thank you

SEVM  Tuesday, September 01, 2009 5:35 AM
Hi
I noticed that the error is when i modify the changes inside the dataset and call getchanges() it is returning null.
sysws.SaveWindowAsync((SYSDataset)dsSystem.GetChanges());
Why getchanges() isreturning null? While after loading I am calling AcceptChanges().
What do u suggest?
SEVM  Wednesday, September 02, 2009 5:11 AM

Data in a DataRow has several different versions. First, there's the original version. Then, when it's being edited, it has a Proposed version and once it's done being edited, that becomes the Current version. Sometimes when editing, the row is left in the Proposed state and the Edit needs to be ended programmatically.

Here's a method I *always* call before I attempt to check for .HasChanges() before saving data:

protected virtual void CommitProposedChanges(DataSet ds)
{
	if (ds == null)
		return;

	for (int nTable = 0; nTable < ds.Tables.Count; nTable++)
	{
		for (int nRow = 0; nRow < ds.Tables[nTable].Rows.Count; nRow++)
		{
			if (ds.Tables[nTable].Rows[nRow].HasVersion(DataRowVersion.Proposed))
			{
				ds.Tables[nTable].Rows[nRow].EndEdit();
			}
		}
	}
}


~~Bonnie Berent [C# MVP]
  • Marked As Answer bySEVM Saturday, September 05, 2009 12:21 PM
  •  
BonnieB  Thursday, September 03, 2009 11:32 PM
thanks Bonnie I am using that function solved my problem :) I appreciate your help.
SEVM  Saturday, September 05, 2009 12:21 PM

You're welcome SEVM. Glad I could help! =0)


~~Bonnie Berent [C# MVP]
BonnieB  Saturday, September 05, 2009 3:59 PM

You can use google to search for other answers

Custom Search

More Threads

• How to access a dataset from the designer in code?
• datagridview cellvaluechanged event
• custom myToolStripTextbox
• How to duplicate records within the same table?
• Call event after a new row added
• I need a column or all columns of datagrid that are not read only
• how to trigger BindingCompleteState.DataError
• Binding to table
• Updating DataView bound to DataGridView from separate thread
• setting an Image column in a datagrid view based on a value in the database c#