我做错了什么 - 试着更新 [英] What am I doing wrong - Trying to update

查看:50
本文介绍了我做错了什么 - 试着更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我附上了我创建的示例代码。我想在员工中读取
数据,并修改几个字段。我试图全局声明我需要的对象

,但我仍然遇到问题。我想更新一个单独的

子程序,似乎有问题。请帮助。


----------------------------------- --------

公共类Form1


继承System.Windows.Forms.Form

Public cn As OleDb.OleDbConnection

Public ds As DataSet

Public da As OleDb.OleDbDataAdapter

Public rowEmployee As DataRow


Private Sub Form1_Load(ByVal sender As System.Object,ByVal e As

System.EventArgs)处理MyBase.Load


尝试

Dim strConn As String

strConn =" Provider = Microsoft.Jet.OLEDB.4.0; Data

Source = C:\ dadetsbs \ SampleDBs \\ \\ nnwind.mdb;"


Dim cn As New OleDb.OleDbConnection(strConn)


Dim strSQL As String

strSQL =" SELECT EmployeeID,FirstName,LastName,Address,City,

Region," &安培; _

"来自员工的PostalCode ORDER BY LastName,FirstName"


Dim da =新OleDb.OleDbDataAdapter(strSQL,strConn)

Dim ds作为新数据集


da.Fill(ds," Employees")


Dim tbl As DataTable = ds.Tables (0)


''rowEmployee =新DataRow

rowEmployee = tbl.Rows(0)

txtFirstName.Text = rowEmployee (" FirstName")

txtLastName.Text = rowEmployee(" LastName")

txtAddress.Text = rowEmployee(" Address")


Catch ex As Exception

MessageBox.Show(ex.Message&" ::"& ex.Source)

最后

结束尝试

End Sub


Private Sub btnUpdate_Click(ByVal sender As System.Object,ByVal e As

System.EventArgs)处理btnUpdate.Click

尝试

rowEmployee(" LastName")= txtLastName.Text

da.Update( ds)


Catch ex As Exception


MessageBox.Show(ex.Message& " :: &安培; ex.Source)


结束尝试


结束次级

结束班级

---------------------------------------


当它运行时,我收到以下错误:


对象引用未设置为对象的实例。


on btnUpdate_Click例程中的da.Update(ds)行。


帮助。


谢谢,


Gary

I have enclosed the sample code that I created. I want to read in employee
data, and modify a few fields. I have tried to globally declare the objects
that I need but I still am having problems. I want to update in a seperate
subroutine and seem to have problems. HELP please.

-------------------------------------------
Public Class Form1

Inherits System.Windows.Forms.Form
Public cn As OleDb.OleDbConnection
Public ds As DataSet
Public da As OleDb.OleDbDataAdapter
Public rowEmployee As DataRow

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Try
Dim strConn As String
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\adonetsbs\SampleDBs\nwind.mdb;"

Dim cn As New OleDb.OleDbConnection(strConn)

Dim strSQL As String
strSQL = "SELECT EmployeeID, FirstName, LastName, Address, City,
Region, " & _
"PostalCode from Employees ORDER BY LastName, FirstName"

Dim da = New OleDb.OleDbDataAdapter(strSQL, strConn)
Dim ds As New DataSet

da.Fill(ds, "Employees")

Dim tbl As DataTable = ds.Tables(0)

''rowEmployee = New DataRow
rowEmployee = tbl.Rows(0)
txtFirstName.Text = rowEmployee("FirstName")
txtLastName.Text = rowEmployee("LastName")
txtAddress.Text = rowEmployee("Address")

Catch ex As Exception
MessageBox.Show(ex.Message & " :: " & ex.Source)
Finally
End Try
End Sub

Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnUpdate.Click
Try
rowEmployee("LastName") = txtLastName.Text
da.Update(ds)

Catch ex As Exception

MessageBox.Show(ex.Message & " :: " & ex.Source)

End Try

End Sub
End Class
---------------------------------------

When it runs I get the following error:

"Object reference not set to an instance of an object."

on the da.Update(ds) line in the btnUpdate_Click routine.

HELP.

Thanks,

Gary

推荐答案

Gary,


在Form1_Load中你创建了局部变量,如da, ds等等


相反,你需要使用全局信息。或者你有形式级变量

在'声明部分的形式中创建。


在Form1_Load中,你只需要做类似的事情:


ds =新数据集


而不是再次标注变量。


Kerry Moorman
" Gary Paris"写道:
Gary,

In Form1_Load you are creating local variables such as da, ds, etc.

Instead, you need to use the "global" or form level variables that you have
creted in the form''s declarations section.

In Form1_Load, you just need to do something like:

ds = New DataSet

instead of dimensioning the variable again.

Kerry Moorman
"Gary Paris" wrote:
我附上了我创建的示例代码。我想阅读员工数据,并修改几个字段。我试图全局声明我需要的对象,但我仍然遇到问题。我想更新一个单独的
子程序,似乎有问题。请帮助。

--------------------------------------- ----
Public Class Form1

继承System.Windows.Forms.Form
Public cn As OleDb.OleDbConnection
Public ds As DataSet
Public da As OleDb.OleDbDataAdapter
Public rowEmployee As DataRow
私有Sub Form1_Load(ByVal sender As System.Object,ByVal e As
System.EventArgs)处理MyBase.Load

尝试
Dim strConn As String
strConn =" Provider = Microsoft.Jet.OLEDB.4.0; Data
Source = C:\ adonetsbs \ SampleDBs \ nwind .mdb;"

Dim cn As New OleDb.OleDbConnection(strConn)
将strim作为字符串调整
strSQL =" SELECT EmployeeID,FirstName,LastName,地址,城市,
地区, &安培; _
"来自员工的PostalCode ORDER BY LastName,FirstName"

Dim da = New OleDb.OleDbDataAdapter(strSQL,strConn)
Dim ds As New DataSet

da.Fill(ds," Employees")

Dim tbl As DataTable = ds.Tables(0)

''rowEmployee = New DataRow rowEmployee = tbl.Rows(0)
txtFirstName.Text = rowEmployee(" FirstName")
txtLastName.Text = rowEmployee(" LastName")
txtAddress.Text = rowEmployee(" ;地址)

Catch ex As Exception> MessageBox.Show(ex.Message&" ::"& ex.Source)
最后
结束尝试
结束子

私有子btnUpdate_Click(ByVal发送者As System.Object,ByVal e As
System.EventArgs)处理btnUpdate.Click
尝试
r owEmployee(" LastName")= txtLastName.Text
da.Update(ds)

抓住ex Exception

MessageBox.Show(ex.Message& " :: &安培; ex.Source)

结束尝试

结束子
结束课程
---------------- -----------------------

运行时我收到以下错误:

"对象引用未设置为对象的实例。

在btnUpdate_Click例程中的da.Update(ds)行。

帮助。
<谢谢,

Gary
I have enclosed the sample code that I created. I want to read in employee
data, and modify a few fields. I have tried to globally declare the objects
that I need but I still am having problems. I want to update in a seperate
subroutine and seem to have problems. HELP please.

-------------------------------------------
Public Class Form1

Inherits System.Windows.Forms.Form
Public cn As OleDb.OleDbConnection
Public ds As DataSet
Public da As OleDb.OleDbDataAdapter
Public rowEmployee As DataRow

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Try
Dim strConn As String
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\adonetsbs\SampleDBs\nwind.mdb;"

Dim cn As New OleDb.OleDbConnection(strConn)

Dim strSQL As String
strSQL = "SELECT EmployeeID, FirstName, LastName, Address, City,
Region, " & _
"PostalCode from Employees ORDER BY LastName, FirstName"

Dim da = New OleDb.OleDbDataAdapter(strSQL, strConn)
Dim ds As New DataSet

da.Fill(ds, "Employees")

Dim tbl As DataTable = ds.Tables(0)

''rowEmployee = New DataRow
rowEmployee = tbl.Rows(0)
txtFirstName.Text = rowEmployee("FirstName")
txtLastName.Text = rowEmployee("LastName")
txtAddress.Text = rowEmployee("Address")

Catch ex As Exception
MessageBox.Show(ex.Message & " :: " & ex.Source)
Finally
End Try
End Sub

Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnUpdate.Click
Try
rowEmployee("LastName") = txtLastName.Text
da.Update(ds)

Catch ex As Exception

MessageBox.Show(ex.Message & " :: " & ex.Source)

End Try

End Sub
End Class
---------------------------------------

When it runs I get the following error:

"Object reference not set to an instance of an object."

on the da.Update(ds) line in the btnUpdate_Click routine.

HELP.

Thanks,

Gary



Gary,


还有很多东西还不对,但是让我们先解决这个问题。

您的错误信息对我来说很奇怪,我不会直接看到。

但是你需要在dataadapter中有一个更新命令。

这对于一个简单的select语句来说很容易做到。

查看我内联粘贴的代码(一行) )

再试一次。我现在不明白你得到的错误,但是至少需要



当你运行它时,不要看看

数据绑定

\\\

cma = DirectCast(BindingContext(dataset1.Tables(0)),CurrencyManager)

textbox1.DataBindings.Add(New Binding(" Text",dataset1.Tables(0),

" LastName"))

///

cma.position比你使用的行给你,你可以通过使用表单上的按钮来影响




我希望这会有所帮助,


Cor
Gary,

There is a lot still not right, however let us first take this problem.
Your error message is strange for me, that I don''t directly see.
However you needs an update command in the dataadapter.
That is easy to do for a simple select statement as you have.
See the code I have pasted inline (one row)
And try than again. I now don''t understand the error you get, however that
is at least needed.

When you have it running have than a look at
Databinding
\\\
cma = DirectCast(BindingContext(dataset1.Tables(0)), CurrencyManager)
textbox1.DataBindings.Add(New Binding("Text", dataset1.Tables(0),
"LastName"))
///
The cma.position gives you than the row that is used and you can affect that
by using buttons on your form.

I hope this helps,

Cor
公共类Form1

继承System.Windows.Forms。表格
Public cn As OleDb.OleDbConnection
Public ds As DataSet
Public da As OleDb.OleDbDataAdapter
Public rowEmployee As DataRow


Private Sub Form1_Load(ByVal sender As System.Object,ByVal e As
System.EventArgs)处理MyBase.Lo ad

尝试
Dim strConn As String
strConn =" Provider = Microsoft.Jet.OLEDB.4.0; Data
Source = C:\ adonetsbs \ SampleDBs \windwind.mdb;"

Dim cn As New OleDb.OleDbConnection(strConn)
将strim作为字符串调整
strSQL =" SELECT EmployeeID, FirstName,LastName,Address,
城市,地区," &安培; _
"来自员工的PostalCode ORDER BY LastName,FirstName"

Dim da = New OleDb.OleDbDataAdapter(strSQL,strConn)
Dim ds As New DataSet

da.Fill(ds," Employees")


dim cmb as new OleDb.OleDbCommandbuilder(da)

Dim tbl As DataTable = ds .Tables(0)

''rowEmployee =新DataRow
rowEmployee = tbl.Rows(0)
txtFirstName.Text = rowEmployee(" FirstName")
txtLastName.Text = rowEmployee(" LastName")
txtAddress.Text = rowEmployee(" Address")

Catch ex As Exception> MessageBox.Show(ex.Message& ;" ::"& ex.Source)
最后结束尝试
结束Sub

私有子btnUpdate_Click(ByVal sender As System.Object,ByVal e As
System.EventArgs)处理btnUpda te。点击
尝试
rowEmployee(" LastName")= txtLastName.Text
da.Update(ds)

Catch ex As Exception
<消息框.Show(ex.Message& " :: &安培; ex.Source)

结束尝试

结束子
结束课程
---------------- -----------------------

运行时我收到以下错误:

"对象引用未设置为对象的实例。

在btnUpdate_Click
例程中的da.Update(ds)行。

帮助。

谢谢,

Gary
Public Class Form1

Inherits System.Windows.Forms.Form
Public cn As OleDb.OleDbConnection
Public ds As DataSet
Public da As OleDb.OleDbDataAdapter
Public rowEmployee As DataRow

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Try
Dim strConn As String
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\adonetsbs\SampleDBs\nwind.mdb;"

Dim cn As New OleDb.OleDbConnection(strConn)

Dim strSQL As String
strSQL = "SELECT EmployeeID, FirstName, LastName, Address,
City, Region, " & _
"PostalCode from Employees ORDER BY LastName, FirstName"

Dim da = New OleDb.OleDbDataAdapter(strSQL, strConn)
Dim ds As New DataSet

da.Fill(ds, "Employees")
dim cmb as new OleDb.OleDbCommandbuilder(da)
Dim tbl As DataTable = ds.Tables(0)

''rowEmployee = New DataRow
rowEmployee = tbl.Rows(0)
txtFirstName.Text = rowEmployee("FirstName")
txtLastName.Text = rowEmployee("LastName")
txtAddress.Text = rowEmployee("Address")

Catch ex As Exception
MessageBox.Show(ex.Message & " :: " & ex.Source)
Finally
End Try
End Sub

Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnUpdate.Click
Try
rowEmployee("LastName") = txtLastName.Text
da.Update(ds)

Catch ex As Exception

MessageBox.Show(ex.Message & " :: " & ex.Source)

End Try

End Sub
End Class
---------------------------------------

When it runs I get the following error:

"Object reference not set to an instance of an object."

on the da.Update(ds) line in the btnUpdate_Click
routine.

HELP.

Thanks,

Gary



Kerry,

就是这样,但是Gary也需要我的答案来解决他的下一个问题。


:-)


Cor
Kerry,

That is it, however Gary needs my answer as well for his next problem.

:-)

Cor


这篇关于我做错了什么 - 试着更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆