0
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?

1
  • try mp without the @ in the parameterName Commented Aug 15, 2011 at 19:07

4 Answers 4

3

You need to add the parameter to the command.

SqlCommand cm = new SqlCommand(q, conn);
cmd.Parameters.Add(parameter);
string b = (string)cm.ExecuteScalar();
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

You are just creating a parameter, not adding it to your command. Try adding this before executing the command:

cm.Parameters.Add(parameter);

Comments

0

You have forgot to add the parameter @mp to your command

cm.Parameters.Add(parameter)

1 Comment

do u mean this way... parameter.ParameterName = "mp";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.