1
我必須嘗試使用vb.net在ASP.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
非常感謝它的工作原理。 – Banner