-2
參照我的最後一個問題:「The connection was not closed. The connection's current state is open」。我正在嘗試使用ASP.NET的vb.net做一個註冊表單。解決了有關「密切聯繫」的問題後。我有問題將數據插入到我的數據庫。當我嘗試驗證它給我一個錯誤消息中的數據:必須聲明標量變量「@User_Name」
必須聲明標量變量「@User_Name」
有人可以幫我調試這個?謝謝
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 (User_Name,Fname,Lname,Email,Password,Gender,Dob,Mobile,Address) values
' (@User_Name,@Fname,@Lname,@Email,@Password,@Gender,@Dob,@Mobile,@Address)";
cmd.CommandText = "INSERT INTO tbl_user (User_Name, Fname, Lname, Email, Pwd, Street, Town, City, Tel) values(@User_Name,@Fname,@Lname,@Email,@Pwd,@Street,@Town,@City,@Tel)"
'adding parameters with value
cmd.Parameters.AddWithValue("@User_Name", txtUser_Name.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", txtTel.Text.ToString())
cmd.Parameters.Clear()
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()
Finally
con.Close()
'closing connection
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
End Class
不相關,但TextBox.Text和Exception.Message屬性已經是字符串了。你不需要*在它們上面調用'ToString',儘管這樣做並沒有害處。 – pmcoltrane
@pmcoltrane - 這也適用於所有文本框的Text Propery! –