Problem when running SQL query
foreach (DataRow masterRow in dst.Tables["Menu"].Rows)
{
MenuItem masterItem = new MenuItem((string)masterRow["Parentitem"]);
string mp = masterItem.Value;
SqlParameter parameter = new SqlParameter();
parameter.ParameterName = "@mp";
parameter.SqlDbType = SqlDbType.NVarChar;
parameter.Direction = ParameterDirection.Input;
parameter.Value = mp;
string q = "select aspnet_PersonalizationPerUser.hasRights
from Menu,aspnet_Users,aspnet_Paths, aspnet_PersonalizationPerUser
where Menu.Parentitem=@mp and Menu.Url = aspnet_Paths.Path
and aspnet_Paths.PathId =aspnet_PersonalizationPerUser.PathId
and aspnet_Users.UserName ='admin'
and aspnet_PersonalizationPerUser.UserId = aspnet_Users.userId ";
SqlCommand cm = new SqlCommand(q, conn);
string b = (string)cm.ExecuteScalar();
if (b == "true")
{
Menu1.Items.Add(masterItem);
}
So when I run the app it says need to declare scalar variable开发者_高级运维 mp .. can u let me know the mistake?
You need to add the parameter to the command.
SqlCommand cm = new SqlCommand(q, conn);
cmd.Parameters.Add(parameter);
string b = (string)cm.ExecuteScalar();
You need to actually add the parameter to the SqlCommand object.
Off the top of my head I think its something like:
cm.Parameters.Add(parameter);
Do this before you call ExecuteScalar
You are just creating a parameter, not adding it to your command. Try adding this before executing the command:
cm.Parameters.Add(parameter);
You have forgot to add the parameter @mp
to your command
cm.Parameters.Add(parameter)
精彩评论