Error on sql database “Must declare the scalar variable”

前端 未结 2 1704
难免孤独
难免孤独 2020-12-07 03:13

I have a problem with my login system. I made a simple system which worked well, but I wanted to make it more advanced and display a different start page depending on a user

相关标签:
2条回答
  • 2020-12-07 03:37

    You have passed in @Login and @Password as parameters to your query, but you have not passed in @UserType as a parameter to your query.

    0 讨论(0)
  • 2020-12-07 03:57

    As the error states: Must declare the scalar variable "@UserType"

    In other words, the SQL command has a parameter for @UserType, but there are no parameters declared in the parameters collection.

    SqlCommand cmd = new SqlCommand(
    "select * from dbo.UserInfo where Login =@Login and UserType=@UserType and Password=@Password and UserType=@UserType", con);
    cmd.Parameters.AddWithValue("@Login", txtUserName.Text);
    cmd.Parameters.AddWithValue("@Password", txtPWD.Text+".123");
    

    Add another parameter for the @UserType parameter:

    cmd.Parameters.AddWithValue("@UserType", "User Type");
    

    Also, the SQL contains a duplicate reference to for @UserType:

    select * from dbo.UserInfo 
    where Login=@Login 
          and UserType=@UserType 
          and Password=@Password 
          and UserType=@UserType
    

    Should probably be:

    select * from dbo.UserInfo 
    where Login=@Login 
          and Password=@Password
          and UserType=@UserType 
    
    0 讨论(0)
提交回复
热议问题