0

This only happens when I created a basic login screen for a sample application I'm building. I have various other functions in the application that read/write/update rows in a database without errors.

private void btnLogin_Click(object sender, EventArgs e)
    {
        try
        {
            if(txtUname.Text == "" && txtPW.Text == "")
            {
                MessageBox.Show("Please fill in required fields!");
            }
            else
            {
                SqlCommand cmd = new SqlCommand("select * from LoginUsers where U_Name=@Name and U_Pass=@Pass", con);

                cmd.Parameters.Add("@Name", txtUname.Text); <- Error on textbox string
                cmd.Parameters.Add("@Pass", txtPW.Text);    <- Error on textbox string

                SqlDataAdapter adpt = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                adpt.Fill(ds);

                int count = ds.Tables[0].Rows.Count;
                if(count == 1   )
                {
                    MessageBox.Show("Successfully Logged in!");
                }
                else
                {
                    MessageBox.Show("Incorrect username or password!");
                }
            }

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

    }
AddieCaddy
  • 21
  • 7

1 Answers1

0

Try anyone from below:

Approach 1:

command.Parameters.Add("@ID", SqlDbType.Int).Value = customerID;

Approach 2:

command.Parameters.AddWithValue("@demographics", demoXml);

Just a random code based on the classification defined, Do let me know if you still face an issue after using any of the above mentioned approach.

Charlieface
  • 52,284
  • 6
  • 19
  • 43
dLcreations
  • 357
  • 2
  • 14
  • 2
    Please do not recommend using `AddWithValue`. https://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/ – gunr2171 Jul 06 '22 at 16:48
  • What is wrong with AddWithValue, that is what actually solved my issue. – AddieCaddy Jul 06 '22 at 16:52
  • @AddieCaddy - It's to avoid the Type Mismatch issue. and most of the time, these types of mismatches don’t matter if you are sure about what you are passing. Correct gunr2171 ? – dLcreations Jul 06 '22 at 16:56
  • @AddieCaddy The first option is the one you always want, at least when it comes to strings, dates and decimals – Charlieface Jul 06 '22 at 18:35