必须声明标量变量" @ User_Name"

时间:2014-06-10 14:14:30

标签: asp.net sql-server vb.net registration

参考我的上一个问题:" The connection was not closed. The connection's current state is open"。我正在尝试使用带有ASP.NET的vb.net进行注册表单。解决了关闭'关闭连接的问题后。我有问题将数据插入我的数据库。当我尝试验证数据时,它给出了一条错误消息:

  

必须声明标量变量" @ 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

1 个答案:

答案 0 :(得分:5)

添加参数后即清除参数。尝试在最后一个cmd.Parameters.Clear()之前放置.AddWithValue,在最后一个之后放置而不是