Click here to Skip to main content
15,923,051 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
con.Open();


using (SqlCommand comm = new SqlCommand("select [Add] from Role1 where Role_Name=@Role and Pages=@Pages ", con))
{ // 2. define parameters used in command object
SqlParameter para = new SqlParameter();
para.ParameterName = "@Role"; para.Value = Session["Role"];
para.ParameterName = "@Pages"; para.Value = "Checkin";// 3. add new parameter to command object
comm.Parameters.Add(para); // // Read in the SELECT results. //
SqlDataReader reade = comm.ExecuteReader();
while (reade.Read())
{ Session["Add"] = Convert.ToString(reade["Add"]); }
// Button Add = (Button)PreviousPage.FindControl("Button2");
if (Session["Add"].ToString() == "Y")
{
Button2.Visible = true;
}
else
{
Button2.Visible = false;
}
con.Close();
}
Posted
Comments
[no name] 17-Apr-14 7:10am    
The error is telling you exactly what the problem is. You did not add a parameter named @Role to the parameter collection.

You are creating one single SqlParameter variable and try to use it to define two parameters.
That cannot work.
Your code should better look like:
C#
con.Open();
using (SqlCommand comm = new SqlCommand("select [Add] from Role1 where Role_Name=@Role and Pages=@Pages", con))
{
  // 2. define parameters used in command object
  comm.Parameters.AddWithValue("@Role", Session["Role"]);
  comm.Parameters.AddWithValue("@Pages", "Checkin");
  SqlDataReader reade = comm.ExecuteReader();
  while (reade.Read())
  {
    Session["Add"] = Convert.ToString(reade["Add"]);
  }
  // Button Add = (Button)PreviousPage.FindControl("Button2");
  Button2.Visible = (Session["Add"].Equals("Y", StringComparison.InvariantCultureIgnoreCase));
}
con.Close();


Hope this helps. Good luck.
 
Share this answer
 
v2
Comments
Member 10578683 17-Apr-14 7:57am    
Button2.Visible = (Session["Add"].Equals("Y", StringComparison.InvariantCultureIgnoreCase));
thi line showing error
phil.o 17-Apr-14 8:07am    
Which error?
Do write your code properly. You didn't pass the pass value for @Role.
C#
SqlParameter para = null;

//Pass @Role parameter
para = new SqlParameter();
para.ParameterName = "@Role"; 
para.Value = Session["Role"];
comm.Parameters.Add(para);

//Pass @Pages parameter
para = new SqlParameter();
para.ParameterName = "@Pages"; 
para.Value = "Checkin";
comm.Parameters.Add(para);
 
Share this answer
 
Comments
Member 10578683 17-Apr-14 8:07am    
Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900