问题
I have to try to do a Registration Form using vb.net on ASP.NET. I already have connection with my database and I already have the code. Unfortunately, I received an error message when trying to validate the form: The connection was not closed. The connection's current state is open.
Moreover, it did not insert the data in the database. Can someone help me please. Thanks.
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Partial Class Register2
Inherits System.Web.UI.Page
'declaring connection string and command
'here we are extracting connection string from web.config file
Private con As New SqlConnection(ConfigurationManager.ConnectionStrings("MauriAuctions").ToString())
Private cmd As New SqlCommand()
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Try
cmd.Connection = con
'assigning connection to command
cmd.CommandType = CommandType.Text
'representing type of command
'cmd.CommandText = "INSERT INTO UserDetails (Fname,Lname,Email,Password,Gender,Dob,Mobile,Address) values
' (@Fname,@Lname,@Email,@Password,@Gender,@Dob,@Mobile,@Address)";
cmd.CommandText = "INSERT INTO tbl_user values(@User_Name,@Fname,@Lname,@Email,@Pwd,@Street,@Town,@City,@Tel)"
'adding parameters with value
cmd.Parameters.AddWithValue("@User_Name", txtUserName.Text.ToString())
cmd.Parameters.AddWithValue("@Fname", txtFirstName.Text.ToString())
cmd.Parameters.AddWithValue("@Lname", txtLastName.Text.ToString())
cmd.Parameters.AddWithValue("@Email", txtEmail.Text.ToString())
cmd.Parameters.AddWithValue("@Pwd", txtPassword.Text.ToString())
cmd.Parameters.AddWithValue("@Street", txtStreet.Text.ToString())
cmd.Parameters.AddWithValue("@Town", txtTown.Text.ToString())
cmd.Parameters.AddWithValue("@City", txtCity.Text.ToString())
cmd.Parameters.AddWithValue("@Tel", txtMobile.Text.ToString())
con.Open()
'opening connection
cmd.ExecuteNonQuery()
'executing query
con.Close()
'closing connection
lblMsg.Text = "Registered Successfully.."
Catch ex As Exception
lblMsg.Text = ex.Message.ToString()
End Try
End Sub
Protected Sub btnClear_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClear.Click
'refreshing/reloading page to clear all the controls
Page.Response.Redirect(Page.Request.Url.ToString(), True)
End Sub
回答1:
You need to close the connection on a finally
clause
Finally con.Close()
回答2:
Try to use Using
for youre sqlconnection
Read this
Using con As New SqlConnection(ConfigurationManager.ConnectionStrings("MauriAuctions").ToString())
cmd.Connection = con
'assigning connection to command
cmd.CommandType = CommandType.Text
'representing type of command
'cmd.CommandText = "INSERT INTO UserDetails (Fname,Lname,Email,Password,Gender,Dob,Mobile,Address) values
' (@Fname,@Lname,@Email,@Password,@Gender,@Dob,@Mobile,@Address)";
cmd.CommandText = "INSERT INTO tbl_user values(@User_Name,@Fname,@Lname,@Email,@Pwd,@Street,@Town,@City,@Tel)"
'adding parameters with value
cmd.Parameters.AddWithValue("@User_Name", txtUserName.Text.ToString())
cmd.Parameters.AddWithValue("@Fname", txtFirstName.Text.ToString())
cmd.Parameters.AddWithValue("@Lname", txtLastName.Text.ToString())
cmd.Parameters.AddWithValue("@Email", txtEmail.Text.ToString())
cmd.Parameters.AddWithValue("@Pwd", txtPassword.Text.ToString())
cmd.Parameters.AddWithValue("@Street", txtStreet.Text.ToString())
cmd.Parameters.AddWithValue("@Town", txtTown.Text.ToString())
cmd.Parameters.AddWithValue("@City", txtCity.Text.ToString())
cmd.Parameters.AddWithValue("@Tel", txtMobile.Text.ToString())
con.Open()
cmd.ExecuteNonQuery()
End Using
来源:https://stackoverflow.com/questions/24137033/the-connection-was-not-closed-the-connections-current-state-is-open