I’m currently making a front end to display license information for my companies software audit but im no pro with sql or asp.net so iv ran into a bit of trouble. I’m trying to get a sum of how many licenses there are across several rows so i can put it in a text box, but im getting the error ‘Must declare the scalar variable “@softwareID”.’
SqlConnection con1 = Connect.GetSQLConnection();
string dataEntry = softwareInputTxt.Text;
string result;
dataEntry = dataEntry + "%";
con1.Open();
SqlCommand Mycmd1;
Mycmd1 = new SqlCommand("select sum(license_quantity_owned) from licenses where software_ID like @softwareID", con1);
MyCmd.Parameters.AddWithValue("@softwareID", dataEntry);
result = (string)Mycmd1.ExecuteScalar();
licenseOwnedTxt.Text = result;
Could anyone point me in the right direction?
d’oh! Found it:
Two different commands!!!
You want:
(or assign
MyCmdandMycmd1to be the same thing)I think it might already be the default, but you could try setting the
.CommandTypetoCommandType.Textexplicitly. Also, note thatAddWithValuecan cause more query-plans than necessary to be created; I would explicitly create the variable with a known size (the size of the column plus spare for the%). The existing string-concat should already ensure it isn’tnull(which is usually the problem).Additionally, note that both
SqlConnectionandSqlCommandareIDisposable– both could be wrapped inusinghere.