Home > Blockchain >  How to fix 'Cannot convert from 'string' to System.Data.SqlDbType' in a Windows
How to fix 'Cannot convert from 'string' to System.Data.SqlDbType' in a Windows

Time:07-07

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);
        }

    }

CodePudding user response:

Try anyone from below:

Approach 1:

command.Parameters.Add("@ID", SqlDbType.Int);
command.Parameters["@ID"].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.

  • Related