'Must declare the scalar variable "@thisuser".' [closed]

You forgot to add the parameter to your SqlCommand. You command does not know which value to fill in for “@thisuser”.

You can add parameters like this:

sqlCommand.Parameters.Add("parameter_name", SqlDbType.TargetSqlType).Value = parameter_value;

So you code would look like this:

private void btnPersonalInfo_Click(object sender, EventArgs e)

    var username = "foo bar";


    string query = "SELECT score1, score2, score3 FROM tblLogin WHERE username = @thisuser";

    SqlCommand sda = new SqlCommand(query, sqlcon);
    sda.Parameters.Add("@thisuser", SqlDbType.NVarChar).Value = username;

    SqlDataReader da = sda.ExecuteReader();

    if (thisuser == da.GetValue(1).ToString())
        while (da.Read())
            MessageBox.Show("Your Personal Info are:  sda.GetValue(3).ToString()");


Thanks to Larnu for pointing out some problems with the AddWithValue-function. If you use the AddWithValue-function instead of the Add-function like above, the type-conversion is done automatically. This can lead to problems. For more information please check out the link in the comments.

Browse More Popular Posts

Leave a Comment