Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am fetching selected row of a datagridview to textboxes and trying to update the row using dataset created to populate the datagridview.
I am NOT finding the row in the dataset even though the value of textbox (corrosponding to the PK column in the table) exists in the table row.
What am I doing wrong? I get NullReferenceException (on the statement marked in the code below). Please help.

My code is:


=============================================================
Private Sub btnUpdateClient_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdateClient.Click
Dim tblTemp As DataTable
tblTemp = ds.Tables("Clients")
Dim editRow As DataRow

Dim s As String = txtId.Text

editRow = ds.Tables("Clients").Rows.Find(s) 'Val to find MUST be a PK in table

editRow.BeginEdit() <<= Getting error here

editRow("ClientName") = txtName.Text
editRow("ClientAddress") = txtAddress.Text
editRow("City") = txtCity.Text
editRow("State") = txtState.Text
editRow("Zip") = txtZip.Text
editRow("ClientPhoneM") = txtPhLL.Text
editRow("ClientPhoneLL") = txtPhM.Text
editRow("ClientNotes") = txtNotes.Text

editRow.EndEdit()

Dim oledbCmdBuilder = New OleDbCommandBuilder(dataadapter)
dataadapter.Update(ds, "Clients")

MsgBox("Database Server updated successfully" & Chr(13) & "Check table to see changes.")

==================================================

The Exception and the stack trace is as below:
System.NullReferenceException was unhandled
HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=AdvantechClient
StackTrace:
at AdvantechClient.Form1.btnUpdateClient_Click(Object sender, EventArgs e) in C:\AdvantechClient\AdvantechClient\Form1.vb:line 157
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at AdvantechClient.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
Posted
Updated 15-Jul-15 23:54pm
v2
Comments
DamithSL 16-Jul-15 5:10am    
what is the full error( with stack trace)? put breakpoint in BeginEdit line and debug your application, check which object is null at runtime
Abcecedarian 16-Jul-15 5:58am    
Thanks @DamithSL, I have improved the question and added stack trace. Pls. see.

1 solution

refer below blog
use linq expression to update datatable

http://www.c-sharpcorner.com/Blogs/13947/[^]
 
Share this answer
 
Comments
Abcecedarian 16-Jul-15 5:58am    
Thanks @Dnyaneshwar_Ghoske, but that is NOT what I intend to do. Moreover Linq is something which I never worked on.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900