更新数据集的行 [英] Updating row of a dataset
问题描述
我正在将数据网格视图的选定行提取到文本框,并尝试使用为填充数据网格视图而创建的数据集来更新行。
我找不到数据集中的行,即使表格行中存在文本框的值(与表中的PK列相对应)。
我做错了什么?我得到NullReferenceException(在下面的代码中标记的语句)。请帮忙。
我的代码是:
======= ================================================== ====
Private Sub btnUpdateClient_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)处理btnUpdateClient.Click
Dim tblTemp As DataTable
tblTemp = ds.Tables(客户)
Dim editRow As DataRow
Dim s As String = txtId.Text
editRow = ds.Tables(Clients)。Rows.Find(s)'Val to find必须是表中的PK
editRow.BeginEdit()<< =在此处获取错误
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 =新的OleDbCommandBuilder(dataadapter)
dataadapter.Update(ds,客户)
MsgBox (数据库服务器已成功更新& Chr(13)& 查看表格以查看更改。)
========================= =========================
异常和堆栈跟踪如下:
System.NullReferenceException未处理
HResult = -2147467261
消息=对象引用未设置为对象的实例。
来源= AdvantechClient
StackTrace:
at AdvantechClient.Form1.btnUpdateClient_Click(Object sender,EventArgs e)位于C:\ AdvantechClient \ AdvantechClient \ Form1.vb:第157行
在System.Windows.Forms.Control.OnClick(EventArgs e)
在System.Windows.Forms.Button.OnClick(EventArgs e)
在System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
在System.Windows.Forms.Control.WmMouseUp(消息& m,MouseButtons按钮,Int32点击)
在System.Windows.Forms.Control.WndProc(消息& m)
at System.Windows.Forms.ButtonBase.WndProc(消息&安培; m)
在System.Windows.Forms.Button.WndProc(消息& m)
在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(消息& m)
在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg, IntPtr wparam,IntPtr lparam)
在System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
在System.Windows.Forms.Application.ComponentManager.System。 Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID,Int32 reason,Int32 pvLoopData)
在System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason,ApplicationContext context)
System.Windows.Forms.Application的System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason,ApplicationContext context)
。运行(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:第81行
在System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly,String [] args)
在System.AppDomain.ExecuteAssembly(String assemblyFile,Evidence assemblySecurity,字符串[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
在System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext,ContextCallback回调,对象状态,布尔值preserveSyncCtx)
在System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback callback,Object state,Boolean preserveSyncCtx)
在System.Threading.ExecutionContext。运行(ExecutionContext executionContext,ContextCallback回调,对象状态)
在System.Threading.ThreadHelper.ThreadStart()
InnerException:
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:
推荐答案
参考下面的博客
使用linq表达式更新数据表
http://www.c-sharpcorner.com/Blogs/13947/ [ ^ ]
refer below blog
use linq expression to update datatable
http://www.c-sharpcorner.com/Blogs/13947/[^]
这篇关于更新数据集的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!