access database is not updating from vb.net
i have a access database named login.mdb,which has many tables.i want to update table named "try".in tat table i have two fields viz name and rollnumber.i want to update rollnum of corresponding name.my code is:
Public Class Form11
Inherits System.Windows.Forms.Form
Dim MyConnection1 As System.Data.OleDb.OleDbConnection
Dim myCommand1 As New System.Data.OleDb.OleDbCommand
Dim sql As String
''# ....
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
MyConnection1.ConnectionString = "PROVIDER=Microsoft.JET.OLEDB.4.0;Data Source = C:\Documents and Settings\1001\Desktop\Subhedar Sir\WindowsApplication1\bin\login.mdb"
MyConnection1.Open()
myCommand1.Connection = MyConnection1
myCommand1.CommandText = "UPDATE try SET rollnumber = '" & TextBox1.Text & "' WHERE nam = '" & TextBox2.Text &开发者_运维问答 "';"
myCommand1.CommandType = CommandType.Text
myCommand1.Connection = MyConnection1
myCommand1.ExecuteNonQuery()
MsgBox("done")
MyConnection1.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
End Class
please tell me where mam going wrong.i am getting error: system.nullreferenceexception:object refernce not set to an instance of an object.
Several issues here:
- "Try" is a reserved word in some databases. Access is fine, but if this ever moves it will cause problems. Best to be prepared early.
- No query parameters (vulnerable to sql injection hacks). Try entering
';DROP Table [Try];--
in one of your textboxes with your current code. - A common db connection for the whole class can cause contention issues and be a bottleneck
- Did not close the connection in a finally block, which will potentially leave connection open and eventually may even cause your database to be unavailable
- Absolute path to the db in the connection string will fail at deployment
- Never created an instance of the connection object (that's the error from your exception)
You code should look more like this:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Using cn As New OleDbConnection("PROVIDER=Microsoft.JET.OLEDB.4.0;Data Source = login.mdb"), _
cmd As New OleDbCommand("UPDATE [try] SET rollnumber= ? WHERE nam= ? ;", cn)
''# Note: I normally don't use AddWithValue(), but I don't know your data types
cmd.Parameters.AddWithValue("?", TextBox1.Text)
cmd.Parameters.AddWithValue("?", TextBox2.Text)
Try
cn.Open()
cmd.ExecuteNonQuery()
MsgBox("done")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Using
End Sub
This code accounts for all of the issues listed above, not just the one causing your current exception.
On which line do you get your error?
MyConnection1 doesn't look instanciated (could cause it)
; at the end of sql is correct?
Also please try to learn about concept of command parameters. creating sql like this is just plain wrong.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Using MyConnection1 As New System.Data.OleDb.OleDbConnection("PROVIDER=Microsoft.JET.OLEDB.4.0;Data Source = C:\Documents and ings\1001\Desktop\Subhedar Sir\WindowsApplication1\bin\login.mdb"), _
myCommand1 As New System.Data.OleDb.OleDbCommand("UPDATE try SET rollnumber = @rollnumber WHERE nam = @nam", MyConnection1)
myCommand1.Parameters.AddWithValue("@rollnumber", TextBox1.Text)
myCommand1.Parameters.AddWithValue("@nam", TextBox2.Text)
MyConnection1.Open()
myCommand1.ExecuteNonQuery()
MsgBox("done")
End Using
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
this should be more like it :)
精彩评论