请帮助-添加记录女士访问权限 [英] Please Help - Add Record Ms Access
问题描述
嗨
我在表单上有10个文本框,允许用户填写并添加到msaccess数据库中.但我收到以下行中的错误未将对象引用设置为对象的实例.-未处理NullReferenceExcption".
cmd.Connection = con-请您帮忙解决造成此问题的原因?
Hi
I have 10 textbox on a form that allow user to fill in and adds to the msaccess database. but i am recieving an error " Object reference not set to an instance of an object. - NullReferenceExcption Was Unhandled" at the following line
cmd.Connection = con - Please could you help with what is causing this issue?
Dim con As New OleDb.OleDbConnection
Dim cmd As OleDb.OleDbCommand
Dim eep As New String("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\FHand\Db\FedUp.mdb")
con.ConnectionString = eep
If con.State = ConnectionState.Open Then
con.Open()
End If
''Add To Table
cmd.Connection = con
cmd.CommandText = "INSERT INTO Field_Yard(ID, Field_ID, Field_Name, Field_Title, Field_Description, Field_Type, Field_Spec, CON_Nodeid, Field_Lenght, Field_Width)" & _
" VALUES (" & Me.txtid.Text & ",''" & Me.txtFID.Text & "'',''" & Me.txtfname.Text & "'',''" & Me.txtftitle.Text & "'',''" & Me.txtfdescription.Text & "'',''" & Me.txtftype.Text & "'',''" & Me.txtfspec.Text & "'',''" & Me.txtconnode.Text & "'',''" & Me.TXTlenght.Text & "'',''" & Me.txtWidth.Text & "'')"
cmd.ExecuteNonQuery()
con.Close()
End Sub
推荐答案
我认为您的问题是命令对象.
你有这个:
I think your problem is that the command object.
You have this:
Dim cmd As OleDb.OleDbCommand
但是您需要该对象的新实例,因此请尝试以下操作:
But you need a new instance of the object, so try this instead:
Dim cmd As New OleDb.OleDbCommand
希望对您有所帮助.
Hope this helps.
对VB不太了解,但乍一看我认为问题出在这里:-
Don''t know much of VB but at 1st look i think problem is here:-
If con.State = ConnectionState.Open Then
con.Open()
我想检查是否需要使用相等性(==),如果连接已经打开,则需要再次打开它
所以尝试这样的事情:-
I suppose to check equality you need to use (==)and if connection is already open what''s the need of opening it again
so try something like this:-
If con.State==ConnectionState.Closed Then
con.Open()
祝您好运;
Best of luck;
使用以下代码结构在访问数据库中添加值
Use following code structure to add values in access database
'Creating Connection object
Public con As New OleDb.OleDbConnection
'Creating data adapter object
Public objda As New OleDb.OleDbDataAdapter
If con.State = ConnectionState.Open Then 'Checking Connection State
con.Close() 'Closing Connection
End If
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "\DatabaseName.mdb"
Try
con.Open() 'Opening Connection
Catch ex As Exception 'Providing Error Message
MessageBox.Show("Please Contact Application Developer", "Database Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End 'Closing Application
End Try
objda.SelectCommand = New OleDbCommand
objda.SelectCommand.CommandText = "Insert into TableName(Field1, Field2) values(@Field1, @Field2)"
objda.SelectCommand.Parameters.AddWithValue("@Field1", Textbox1.Text)
objda.SelectCommand.Parameters.AddWithValue("@Field2",Textbox2.Text)
'Assigning value of TextField to variable @Eid
Try 'To handle OleDb Exceptions
objda.SelectCommand.ExecuteNonQuery()
Catch ex As OleDb.OleDbException
MessageBox.Show(ex.Message)
End Try
这篇关于请帮助-添加记录女士访问权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!