问题
I want to create an application where I can register person info. but I am having a problem updating/edit the data in my gridview. Below is the set of code which I've created.
Imports System.Data.SqlClient
Public Class Form1
Dim connectionString As String
Dim cnn As SqlConnection
Dim cmd As SqlCommand
Dim sql As String
below is my ADD statement and it work just fine
Private Sub btnADD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnADD.Click
connectionString = "Data Source=XXXX;Initial Catalog=XXXX;User ID=XXXX;Password=XXXX"
cnn = New SqlConnection(connectionString)
Try
cnn.Open()
cmd = New SqlCommand("INSERT INTO tbl_info (ID,Name) VALUES (@ID,@Name)")
cmd.Connection = cnn
With cmd.Parameters
.AddWithValue("ID", txtID.Text)
.AddWithValue("Name", txtName.Text)
End With
cmd.ExecuteNonQuery()
MsgBox("has been inserted successfully")
Catch ex As Exception
MsgBox(ex.Message())
End Try
txtID.Clear()
txtName.Clear()
End Sub
below is gridview witch is link to my database
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'TestDataSet.tbl_info' table. You can move, or remove it, as needed.
Me.Tbl_infoTableAdapter.Fill(Me.TestDataSet.tbl_info)
End Sub
below is my update statement witch i'm having a hard time to figure what is wrong.
Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
cnn = New SqlConnection(connectionString)
If Not cnn.State = ConnectionState.Open Then
cnn.Open()
End If
Try
cmd = New SqlCommand("Update tble_info set Name = @Name Where ID = @ID)")
cmd.Connection = cnn
With cmd.Parameters
.AddWithValue("Name", txtName.Text)
.AddWithValue("ID", txtID.Text)
End With
cmd.ExecuteNonQuery()
MsgBox("has been update successfully")
Catch ex As Exception
MsgBox(ex.Message())
End Try
End Sub
End Class
and this is the error I encountered when I execute the program
InvalidOperationExeption was unhadled
The connectionString property has not been initialize
its pointing to cnn.open()
回答1:
I'm not sure what exactly is wrong, I can only imagine that connectionStringI
is somehow Null
when you are trying to use it, but I think it is good practice to close the connection every time you are done using it.
So when you fetch the data instead of leaving the connection open as you do now, do something like
With New SqlConnection(connectionString)
Try
.Open()
' Do Stuff
Catch
' Handle your exception
Finally
.Close()
End Try
End With
That way your connection will always be closed and you won't have to worry about checking whether or not it is already open.
UPDATE
I have taken the liberty of rewriting your code the way I would do it. Can you please give this a try and see if it works for you/
You connection string property:
Private ReadOnly Property connectionString As String
Get
Return "Data Source=XXXX;Initial Catalog=XXXX;User ID=XXXX;Password=XXXX"
End Get
End Property
The sub which inserts the data:
Private Sub btnADD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnADD.Click
Dim transaction As SqlTransaction = Nothing
With New SqlConnection(connectionString)
Try
Call .Open()
transaction = .BeginTransaction()
With .CreateCommand()
.Transaction = transaction
.CommandText = "INSERT INTO [tbl_info] ([ID], [Name]) VALUES (@ID,@Name);"
With .Parameters
.AddWithValue("ID", txtID.Text)
.AddWithValue("Name", txtName.Text)
End With
Call .ExecuteNonQuery()
Call transaction.Commit()
Call MessageBox.Show("has been inserted successfully")
End With
Catch ex As Exception
Call transaction.Rollback()
Call MessageBox.Show(ex.Message, "Error")
Finally
Call .Close()
End Try
End With
Call txtID.Clear()
Call txtName.Clear()
End Sub
The sub which updates the data:
Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
Dim transaction As SqlTransaction = Nothing
With New SqlConnection(connectionString)
Try
Call .Open()
transaction = .BeginTransaction()
With .CreateCommand()
.Transaction = transaction
.CommandText = "UPDATE [tble_info] SET [Name]=@Name WHERE [ID]=@ID;"
With .Parameters
.AddWithValue("Name", txtName.Text)
.AddWithValue("ID", txtID.Text)
End With
Call .ExecuteNonQuery()
Call transaction.Commit()
Call MessageBox.Show("has been update successfully")
End With
Catch ex As Exception
Call transaction.Rollback()
Call MessageBox.Show(ex.Message, "Error")
Finally
Call .Close()
End Try
End With
End Sub
If you make the connection string a (read-only) property, assuming it doesn't change, then you won't have to worry about whether it is has a value or not when you use it. Then all you have to worry about is make sure that the connection string is correct.
Note that I haven't tested this code, but it should work as is I think.
来源:https://stackoverflow.com/questions/15423898/having-trouble-updating-edit-database