Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have a problem with my code,specifically at the login form.I want to sign into my application,but when i enter the credentials,I get the error"Must declare the variable @UserName".I set the breakpoint to see if the value is null,but it's not,everything works except for the UserName.I looked over the binding and the sintax and everything is ok.This is the VM:
public void SubmitButton(object param)
       {

           SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\user0909\Documents\AttendanceListStudents.mdf;Integrated Security=True;Connect Timeout=30");
           try
           {



               if (conn.State == System.Data.ConnectionState.Closed)
                   conn.Open();
               String query = "SELECT COUNT (*) FROM RegisterTeacher WHERE UserName=@UserName,pwd=@pwd and pwd2=@pwd2";
               SqlCommand cmd = new SqlCommand(query, conn);
               cmd.CommandType = CommandType.Text;


               cmd.Parameters.AddWithValue("@UserName", SqlDbType.NVarChar).Value =_UserName;//here I have the error
               SqlParameter Pwd = cmd.Parameters.AddWithValue("@pwd", SqlDbType.Char);

               cmd.Parameters["@pwd"].Value = TestPassword;
               if (Pwd.Value == null)
               {
                   Pwd.Value = DBNull.Value;
               }
               SqlParameter Pwd2 = cmd.Parameters.AddWithValue("@pwd2", SqlDbType.Char);
               //cmd.Parameters["@pwd"].Value = _pwd.pwd;
               cmd.Parameters["@pwd2"].Value = pwd2;
               if (Pwd2.Value == null)
               {
                   Pwd2.Value = DBNull.Value;
               }
               SqlDataAdapter da = new SqlDataAdapter(query, conn);
               DataTable dt = new DataTable();
               da.Fill(dt);
               if(dt.Rows.Count==1)
               {
                   TestPassword = dt.Rows[0][1].ToString();
                   byte[] hashBytes = Convert.FromBase64String(TestPassword);
                   byte[] salt = new byte[16];
                   Array.Copy(hashBytes, 0, salt, 0, 16);
                   var pbkdf2 = new Rfc2898DeriveBytes(TestPassword, salt, 10000);
                   byte[] hash = pbkdf2.GetBytes(20);
                   int count = Convert.ToInt32(cmd.ExecuteScalar());
                   for (int i = 0; i < 20; i++)
                   {
                       if(hashBytes[i+16]!=hash[i])
                           {
                           count = 0;
                       }if(count==1)
                       {
                           var app = new TextBoxFocusView();
                           var context = new TextBoxFocusDb();
                           app.DataContext = context;
                           app.Show();
                       }
                   }
               }

And this is the datacontext int the View:
<Label Content="Username" Foreground="White"/>
        <TextBox Name="txtusername" Text="{Binding Path=UserName}"   Background="#545d6a" Foreground="White" FontSize="18"/>
        <Label Content="Password"     Foreground="White"/>
        <PasswordBox x:Name="passwordBox"
                     MaxLength="10"
                local:PasswordHelper.BindPassword="true"
                     local:PasswordHelper.BoundPassword="{Binding Path=TestPassword, Mode=TwoWay, ValidatesOnDataErrors=True,UpdateSourceTrigger=PropertyChanged}"
            PasswordChar="*"
                     Background="#545d6a"
                     Foreground="White"
         FontSize="18"/>

Can someone please help me understand what the issue is?Thank you in advance!

What I have tried:

I have tried multiple things to make it work,I tried creating another variable and assigning the value to it,I tried this:
<a href="https://stackoverflow.com/questions/8933634/must-declare-the-scalar-variable-username">c# - Must declare the scalar variable "@UserName" - Stack Overflow</a>[<a href="https://stackoverflow.com/questions/8933634/must-declare-the-scalar-variable-username" target="_blank" title="New Window">^</a>]

c# - Must declare the scalar variable "@username". How to overcome it - Stack Overflow[^]
Posted
Updated 8-May-18 23:00pm
Comments
Herman<T>.Instance 9-May-18 4:41am    
What is the value in _UserName? If that is NULL the parameter is not send to the database. To overcome this problem set the NULL value to DBNull.Value. The parameter is send to the database in that case.
Daniel Andrei Popescu 9-May-18 4:44am    
Thank you for your response.It is not null.It holds the value,that's the problem.When I set the breakpoint and i insert the value in the textbox and then debug it,I get the value without any problem.

1 solution

AddwithValue isn't written like that:
cmd.Parameters.AddWithValue("@UserName", SqlDbType.NVarChar).Value =_UserName;//here I have the error
It shoudl be:
cmd.Parameters.AddWithValue("@UserName", _UserName);

Fix that, it should work.And fix this as well:
cmd.ParametersAddWithValue("@pwd", TestPassword);

But don't do passwords like that! Never store passwords in clear text - it is a major security risk. There is some information on how to do it here: Password Storage: How to do it.[^] - it'#s probably a bit advanced for you, but just use the code, and don't worry about how it works for the moment.
 
Share this answer
 
Comments
Daniel Andrei Popescu 9-May-18 5:25am    
Thank you for the advice.Regarding my password,yes,I have tried to encrypt it and check if the two hash values are the same,as i have it in my function.I dont' understand the sintax"store password in clear text".I have a password box which I use in the view,I have a passwordhelper class from which I bind the passwordbox and also I encrypt it when I store the values into the db and when I select them from the database. As for the UserName parameter,it still doesn't work.I have also tried this.So basically it reads the value,but won't accept the credentials because i didn't set the variable.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900