Parameter Problem while running sql query in C#
Error :
The SqlParameterCollection only accepts non-null SqlParameter type objects, not String objects
This is the error I'm getting when I run this code
MenuItem masterItem = new MenuItem((string)masterRow["Parentitem"]);
string mp =(string) masterItem.Value;
SqlP开发者_StackOverflow中文版arameter 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);
cm.Parameters.Add(mp);
string b = (string)cm.ExecuteScalar();
I'm getting the exception when I'm adding parameter to the command.. Can u let me know the mistake..
Change this:
cm.Parameters.Add(mp);
to this:
cm.Parameters.Add(parameter);
精彩评论