查看: 1193|回复: 20
|
求助于 各位 VB.Net 高手!
[复制链接]
|
|
小弟是一个 IT学生,最近刚接触 VB.Net
由于学校要我们交 功课了,可是小弟 program 遇到了少少问题!
特地开贴,请教的
小弟的问题:add 不到 data 进 我的 database
If inc <> -1 Then
Dim cb As New OleDb.OleDbCommandBuilder(da)
Dim dsNewRow As DataRow
dsNewRow = ds.Tables("member").NewRow() <== error 所在
dsNewRow.Item("id") = txt_id.Text
dsNewRow.Item("name") = txt_name.Text
dsNewRow.Item("dob") = txt_dob.Text
dsNewRow.Item("sex") = cmb_sex.Text
dsNewRow.Item("ic") = txt_ic.Text
dsNewRow.Item("address") = txt_address.Text
dsNewRow.Item("postal code") = txt_postal.Text
dsNewRow.Item("city") = txt_city.Text
dsNewRow.Item("state") = txt_state.Text
dsNewRow.Item("house phone") = txt_housephone.Text
dsNewRow.Item("handphone") = txt_handphone.Text
dsNewRow.Item("email") = txt_email.Text
dsNewRow.Item("class") = cmb_class.Text
ds.Tables("member").Rows.Add(dsNewRow)
da.Update(ds, "member")
MsgBox("New Record added to the DataBase")
End If
End Sub
[ 本帖最后由 ET_Galaxy 于 13-11-2005 12:41 AM 编辑 ] |
|
|
|
|
|
|
|
楼主 |
发表于 13-11-2005 12:38 AM
|
显示全部楼层
问题2 :delete 不到 data
Private Sub btn_delete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_delete.Click
Dim cb As New OleDb.OleDbCommandBuilder(da)
If MessageBox.Show("Do you really want to Delete this Record?", _
"Delete", MessageBoxButtons.YesNo, _
MessageBoxIcon.Warning) = DialogResult.No Then
MsgBox("Operation Cancelled")
Exit Sub
End If
ds.Tables("member").Rows(inc).Delete()
MaxRows = MaxRows - 1
inc = 0
Navigaterecords()
da.Update(ds, "member")
End Sub 《== error 所在
[ 本帖最后由 ET_Galaxy 于 13-11-2005 12:41 AM 编辑 ] |
|
|
|
|
|
|
|
楼主 |
发表于 13-11-2005 12:40 AM
|
显示全部楼层
问题3:update 不到 data
Private Sub btn_update_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_update.Click
Dim cb As New OleDb.OleDbCommandBuilder(da)
ds.Tables("member").Rows(inc).Item(0) = txt_id.Text
ds.Tables("member").Rows(inc).Item(1) = txt_name.Text
ds.Tables("member").Rows(inc).Item(2) = txt_dob.Text
ds.Tables("member").Rows(inc).Item(3) = cmb_sex.Text
ds.Tables("member").Rows(inc).Item(4) = txt_ic.Text
ds.Tables("member").Rows(inc).Item(5) = txt_address.Text
ds.Tables("member").Rows(inc).Item(6) = txt_postal.Text
ds.Tables("member").Rows(inc).Item(7) = txt_city.Text
ds.Tables("member").Rows(inc).Item(8) = txt_state.Text
ds.Tables("member").Rows(inc).Item(9) = txt_housephone.Text
ds.Tables("member").Rows(inc).Item(10) = txt_handphone.Text
ds.Tables("member").Rows(inc).Item(11) = txt_email.Text
ds.Tables("member").Rows(inc).Item(12) = cmb_class.Text
da.Update(ds, "member") 《== error 所在
MsgBox("Data updated")
End Sub
[ 本帖最后由 ET_Galaxy 于 13-11-2005 12:42 AM 编辑 ] |
|
|
|
|
|
|
|
发表于 13-11-2005 06:05 AM
|
显示全部楼层
不是很能理解你的问题,至少有error message吧, 这是 access database 吗 ? |
|
|
|
|
|
|
|
发表于 13-11-2005 07:52 PM
|
显示全部楼层
回ET_Galaxy
据我猜测是dataset,data adapter和data connection没有弄好的结果。
由于没有完整的error message,所以也只能猜测罢了。
第一个问题,猜不到是那里出错。
第二个:
应该是这个error message
An unhandled exception of type 'System.InvalidOperationException' occurred in system.data.dll
Additional information: Update requires a valid DeleteCommand when passed DataRow collection with deleted rows.
第三个:
An unhandled exception of type 'System.InvalidOperationException' occurred in system.data.dll
Additional information: Update requires a valid InsertCommand when passed DataRow collection with new rows.
如果我猜对的话,那就表示你的data adapter没弄好。
另一个可能性是data connection没弄好。
你把完整的error message post上来,这样才有办法知道问题的来源。
另外,你给的coding也不完整,如果有较完整的coding,就容易猜得到那里出错。 |
|
|
|
|
|
|
|
楼主 |
发表于 14-11-2005 02:18 AM
|
显示全部楼层
谢谢各位的帮忙,我的问题得以解决了!
不过得到一个新的问题!
那就是 我只能 delete 一次!
如果第二次要 delete 就有问题了!
coding :
Dim con As New OleDb.OleDbConnection
Dim ds As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim sql As String
Dim inc As Integer
Dim MaxRows As Integer
Dim OleDbSelectCommand1 As System.Data.OleDb.OleDbCommand
Dim OleDbInsertCommand1 As System.Data.OleDb.OleDbCommand
Dim OleDbUpdateCommand1 As System.Data.OleDb.OleDbCommand
Dim OleDbDeleteCommand1 As System.Data.OleDb.OleDbCommand
Private Sub Frcarier_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
OpenConnection()
setParammeter()
MaxRows = ds.Tables("member").Rows.Count
inc = -1
End Sub
Private Sub Navigaterecords()
txt_id.Text = ds.Tables("member").Rows(inc).Item(0)
txt_name.Text = ds.Tables("member").Rows(inc).Item(1)
dtDOB.Value() = ds.Tables("member").Rows(inc).Item(2)
cmb_sex.Text = ds.Tables("member").Rows(inc).Item(3)
txt_ic.Text = ds.Tables("member").Rows(inc).Item(4)
txt_address.Text = ds.Tables("member").Rows(inc).Item(5)
txt_postal.Text = ds.Tables("member").Rows(inc).Item(6)
txt_city.Text = ds.Tables("member").Rows(inc).Item(7)
txt_state.Text = ds.Tables("member").Rows(inc).Item(8)
txt_housephone.Text = ds.Tables("member").Rows(inc).Item(9)
txt_handphone.Text = ds.Tables("member").Rows(inc).Item(10)
txt_email.Text = ds.Tables("member").Rows(inc).Item(11)
cmb_class.Text = ds.Tables("member").Rows(inc).Item(12)
End Sub
Private Sub btn_delete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_delete.Click
Dim cb As New OleDb.OleDbCommandBuilder(da)
If MessageBox.Show("Do you really want to Delete this Record?", _
"Delete", MessageBoxButtons.YesNo, _
MessageBoxIcon.Warning) = DialogResult.No Then
MsgBox("Operation Cancelled")
Exit Sub
End If
ds.Tables("member").Rows(inc).Delete()
MaxRows = MaxRows - 1
inc = 0
da.Update(ds, "member")
OpenConnection()
MaxRows = ds.Tables("member").Rows.Count
inc = 0
Navigaterecords()
End Sub
Private Sub OpenConnection()
Try
con.ConnectionString = "Provider = Microsoft.jet.oledb.4.0; Data Source= C:\Visual Studio Projects\DataBase\Century Book Store Database.mdb"
con.Open()
sql = "Select * from member"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "member")
con.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub setParammeter()
OleDbSelectCommand1 = New System.Data.OleDb.OleDbCommand
OleDbInsertCommand1 = New System.Data.OleDb.OleDbCommand
OleDbUpdateCommand1 = New System.Data.OleDb.OleDbCommand
OleDbDeleteCommand1 = New System.Data.OleDb.OleDbCommand
Me.OleDbInsertCommand1.CommandText = "INSERT INTO member(address, city, class, dob, email, handphone, [house phone], ic" & _
", id, name, [postal code], sex, state) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, " & _
"?, ?)"
Me.OleDbInsertCommand1.Connection = con
Me.OleDbInsertCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("address", System.Data.OleDb.OleDbType.VarWChar, 50, "address"))
Me.OleDbInsertCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("city", System.Data.OleDb.OleDbType.VarWChar, 50, "city"))
Me.OleDbInsertCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("class", System.Data.OleDb.OleDbType.VarWChar, 50, "class"))
Me.OleDbInsertCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("dob", System.Data.OleDb.OleDbType.DBDate, 0, "dob"))
Me.OleDbInsertCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("email", System.Data.OleDb.OleDbType.VarWChar, 50, "email"))
Me.OleDbInsertCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("handphone", System.Data.OleDb.OleDbType.VarWChar, 50, "handphone"))
Me.OleDbInsertCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("house_phone", System.Data.OleDb.OleDbType.VarWChar, 50, "house phone"))
Me.OleDbInsertCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("ic", System.Data.OleDb.OleDbType.VarWChar, 50, "ic"))
Me.OleDbInsertCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("id", System.Data.OleDb.OleDbType.Integer, 0, "id"))
Me.OleDbInsertCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("name", System.Data.OleDb.OleDbType.VarWChar, 50, "name"))
Me.OleDbInsertCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("postal_code", System.Data.OleDb.OleDbType.Integer, 0, "postal code"))
Me.OleDbInsertCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("sex", System.Data.OleDb.OleDbType.VarWChar, 50, "sex"))
Me.OleDbInsertCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("state", System.Data.OleDb.OleDbType.VarWChar, 50, "state"))
da.InsertCommand = OleDbInsertCommand1
'
'OleDbUpdateCommand1
'
Me.OleDbUpdateCommand1.CommandText = "UPDATE member SET address = ?, city = ?, class = ?, dob = ?, email = ?, handphone" & _
" = ?, [house phone] = ?, ic = ?, id = ?, name = ?, [postal code] = ?, sex = ?, s" & _
"tate = ? WHERE (id = ?)"
Me.OleDbUpdateCommand1.Connection = con
Me.OleDbUpdateCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("address", System.Data.OleDb.OleDbType.VarWChar, 50, "address"))
Me.OleDbUpdateCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("city", System.Data.OleDb.OleDbType.VarWChar, 50, "city"))
Me.OleDbUpdateCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("class", System.Data.OleDb.OleDbType.VarWChar, 50, "class"))
Me.OleDbUpdateCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("dob", System.Data.OleDb.OleDbType.DBDate, 0, "dob"))
Me.OleDbUpdateCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("email", System.Data.OleDb.OleDbType.VarWChar, 50, "email"))
Me.OleDbUpdateCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("handphone", System.Data.OleDb.OleDbType.VarWChar, 50, "handphone"))
Me.OleDbUpdateCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("house_phone", System.Data.OleDb.OleDbType.VarWChar, 50, "house phone"))
Me.OleDbUpdateCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("ic", System.Data.OleDb.OleDbType.VarWChar, 50, "ic"))
Me.OleDbUpdateCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("id", System.Data.OleDb.OleDbType.Integer, 0, "id"))
Me.OleDbUpdateCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("name", System.Data.OleDb.OleDbType.VarWChar, 50, "name"))
Me.OleDbUpdateCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("postal_code", System.Data.OleDb.OleDbType.Integer, 0, "postal code"))
Me.OleDbUpdateCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("sex", System.Data.OleDb.OleDbType.VarWChar, 50, "sex"))
Me.OleDbUpdateCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("state", System.Data.OleDb.OleDbType.VarWChar, 50, "state"))
Me.OleDbUpdateCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("Original_id", System.Data.OleDb.OleDbType.Integer, 0, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "id", System.Data.DataRowVersion.Original, Nothing))
da.UpdateCommand = OleDbUpdateCommand1
'
'OleDbDeleteCommand1
'
Me.OleDbDeleteCommand1.CommandText = "DELETE FROM member WHERE (id = ?) "
Me.OleDbDeleteCommand1.Connection = con
Me.OleDbDeleteCommand1.Parameters.Add(New System.Data.OleDb.OleDbParameter("Original_id", System.Data.OleDb.OleDbType.Integer, 0, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "id", System.Data.DataRowVersion.Original, Nothing))
da.DeleteCommand = OleDbDeleteCommand1
End Sub
End Class
[ 本帖最后由 ET_Galaxy 于 14-11-2005 02:20 AM 编辑 ] |
-
|
|
|
|
|
|
|
发表于 14-11-2005 09:41 AM
|
显示全部楼层
ds.Tables("member").Rows(inc).Delete()
MaxRows = MaxRows - 1
也许在这行之前, 先把 inc 打印出来看看 inc 当前已经变成什么数值. |
|
|
|
|
|
|
|
发表于 14-11-2005 07:39 PM
|
显示全部楼层
openconnection只要在form load那里做一次就可以了。
Private Sub btn_delete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_delete.Click
Dim cb As New OleDb.OleDbCommandBuilder(da)
If MessageBox.Show("Do you really want to Delete this Record?", _
"Delete", MessageBoxButtons.YesNo, _
MessageBoxIcon.Warning) = DialogResult.No Then
MsgBox("Operation Cancelled")
Exit Sub
End If
ds.Tables("member").Rows(inc).Delete()
MaxRows = MaxRows - 1
inc = 0
da.Update(ds, "member")
OpenConnection()
这一行是多余的。而且,你在openconnection里有作新的data adapter,这样做只会使到你的updatecommand,deletecommand和insertcommand不见掉。
你试试把它删掉或remark掉,看看能不能解决你的问题。
MaxRows = ds.Tables("member").Rows.Count
inc = 0
Navigaterecords()
End Sub
Private Sub OpenConnection()
Try
con.ConnectionString = "Provider = Microsoft.jet.oledb.4.0; Data Source= C:\Visual Studio Projects\DataBase\Century Book Store Database.mdb"
con.Open()
sql = "Select * from member"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "member")
con.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub |
|
|
|
|
|
|
|
楼主 |
发表于 14-11-2005 09:13 PM
|
显示全部楼层
|
|
|
|
|
|
|
发表于 14-11-2005 10:03 PM
|
显示全部楼层
|
|
|
|
|
|
|
发表于 15-11-2005 09:10 AM
|
显示全部楼层
原帖由 ET_Galaxy 于 14-11-2005 09:13 PM 发表
goatstudio 你要我做什么? 我不是很明白!:rf::rf::rf::rf:
至于 meemee 你教的方法,都行不通!
两个方法.
一个是雨吟所说的, 另一个是用你的 Visual Studio 里的 trace.
用 trace 的话, 先在这行放一个 check point,
ds.Tables("member").Rows(inc).Delete()
MaxRows = MaxRows - 1
然后再按 play 执行, 执行后会停在你的 check point, 然后你可以用 F11 慢慢一行一行执行, 然后找出 inc 这个数值, 怀疑它在第一次 delete 的时候就变化了, 或是你的code 算错你的 datattable rows array. |
|
|
|
|
|
|
|
发表于 15-11-2005 10:07 AM
|
显示全部楼层
ET_Galaxy
怎样了???你的问题... OK 了吗??? |
|
|
|
|
|
|
|
发表于 15-11-2005 12:06 PM
|
显示全部楼层
据我猜测inc是用来trace current record,但这一部分的coding并没有post上来。
因为我看他捉的图还有另一部分的coding,在他post的coding里看不到
另外,我看到他的error是出现在da.update,也就是说问题应该是出在data adapter那里。
inc应该还有另一部分的coding控制,不然的话,inc=-1,那么error应该是出现在ds.Tables("member").Rows(inc).Delete()那里了。
你们认为呢? |
|
|
|
|
|
|
|
发表于 15-11-2005 09:21 PM
|
显示全部楼层
|
|
|
|
|
|
|
楼主 |
发表于 16-11-2005 09:58 AM
|
显示全部楼层
谢谢各位的帮忙,我的问题得以解决了!
谢谢各位鼎立帮忙!
如果还有遇到问题,我在上来寻问你们! |
|
|
|
|
|
|
|
楼主 |
发表于 16-11-2005 08:17 PM
|
显示全部楼层
今天,我的 lecturer 又给我一个难题了!
要加一个 asp web interface !
各位,我要如何 加呢?我本身是完全没有头绪!
我打算在 asp form 里面 view 我的 latest book & promotion !
至于 data 呢,我打算在我的 Book database 中 retrieve 出来!
promotion 的话,就随便设计和制作!
各位,你们觉得可行吗?我该如何做起呢? |
|
|
|
|
|
|
|
发表于 16-11-2005 09:14 PM
|
显示全部楼层
|
|
|
|
|
|
|
楼主 |
发表于 16-11-2005 10:56 PM
|
显示全部楼层
|
|
|
|
|
|
|
发表于 16-11-2005 11:47 PM
|
显示全部楼层
我学校也有。。。。通常叫online & offline system....
以前我也是觉得怪怪的~不过,当作学习咯~
ASP.NET,你去我给的网址,都能够学习到基本的东西,靠它做出一个application是可以的..我的FYP也是靠它解决的....加油咯~ |
|
|
|
|
|
|
|
楼主 |
发表于 20-11-2005 01:13 PM
|
显示全部楼层
够力了,星期二要交了,可是我的 asp 还没有 setting 好!
有谁可以告诉我怎么 开 web application 啊?
IIS 我开了 ~~~ 可是我还是没有办法开 web application form!
有谁可以告诉我,到底要怎么做才能成功的开一个 web application form 啊? |
|
|
|
|
|
|
| |
本周最热论坛帖子
|