错误:"参数化查询需要未提供的参数@UserName"

时间:2018-03-08 08:38:08

标签: c# sql-server ado.net

当我运行我的应用程序时说我没有提供参数@UserName时出错。我尝试了不同的方法,例如使用参数的add方法并定义它,我也检查了如果数据库中有空值但是什么都没有,我仍然会得到相同的错误。 这是我的错误产生的代码: 的 LoginViewModel.cs

using LastAndFinalVersion.Helpers;
using LastAndFinalVersion.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using System.Windows;


namespace LastAndFinalVersion.ViewModel
 {
  class LoginViewModel : INotifyPropertyChanged
{

    private DelegateCommand loginCommand;
    private  Login _UserName;
    private  Login _pwd;
    private Login CurrentUser { get; set; }

    public event PropertyChangedEventHandler OnPropertyChanged;
    public event PropertyChangedEventHandler PropertyChanged;

    public Login UserName
    {
        get { return _UserName; }
        set
        {
            _UserName = value;
           NotifyPropertyChanged("UserName");
        }


    }


public Login pwd
    {
        get { return _pwd; }
        set
        {
            if(_pwd!=value)
            {
                _pwd = value;
                NotifyPropertyChanged("pwd");
            }
        }
    }


public LoginViewModel():base()
{
    ButtonCommand = new DelegateCommand(SubmitButton);



}
public DelegateCommand ButtonCommand
{
    get { return loginCommand; }
    set
    {
        if (loginCommand != value)
            loginCommand = value;
        NotifyPropertyChanged("ButtonCommand");
    }
}

protected void NotifyPropertyChanged(string propertyName)
{
    if (OnPropertyChanged != null)
    {
        OnPropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

    public void SubmitButton()
    {

        SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\User\source\repos\VIAApp2Demo\VIAApp2Demo\DB\DatabaseStudents.mdf;Integrated Security=True;Connect Timeout=30");
        try
        {
            if (conn.State == System.Data.ConnectionState.Closed)
                conn.Open();
            String query = "SELECT COUNT (*) FROM Login WHERE UserName=@UserName AND pwd=@pwd";
            SqlCommand cmd = new SqlCommand(query, conn);
            cmd.CommandType = CommandType.Text;
         SqlParameter Username=cmd.Parameters.AddWithValue("@UserName", _UserName);
            if(Username==null)
            {
                Username.Value = DBNull.Value;
            }
            SqlParameter Pwd = cmd.Parameters.AddWithValue("@pwd", _pwd);
            if(Pwd==null)
            {
                Pwd.Value = DBNull.Value;
            }
            int count = Convert.ToInt32(cmd.ExecuteScalar());
            if (count > 0)

            {
                MainWindow main = new MainWindow();
                main.Show();
                main.Hide();
            }
            else
            {
                MessageBox.Show("Username or password is incorrect!");
            }

        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            conn.Close();
        }
   }
  }
   }

任何帮助都将不胜感激。谢谢!

@Mustafa这是Login.cs模型类:

 namespace LastAndFinalVersion.Model
{
using System;
using System.Collections.ObjectModel;

public partial class Login
{
public Nullable<int> SNTeacher { get; set; }
public string UserName { get; set; }
public int pwd { get; set; }

public virtual RegisterTeacher RegisterTeacher { get; set; }
 }
 }

0 个答案:

没有答案