连接没有关闭。连接的当前状态是打开的

时间:2014-06-10 09:10:23

标签: asp.net vb.net sqlconnection

我必须尝试在 ASP.NET 上使用vb.net进行注册表单。我已经与我的数据库连接,我已经有了代码。不幸的是,我在尝试验证表单时收到错误消息: 连接未关闭。连接的当前状态已打开。

此外,它没有在数据库中插入数据。有谁可以帮助我吗。感谢。

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

2 个答案:

答案 0 :(得分:1)

尝试对Using使用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

答案 1 :(得分:0)

您需要关闭finally子句

上的连接
Finally con.Close()