Reader not reading

  • Thread starter Thread starter Andrew Banks
  • Start date Start date
A

Andrew Banks

I've got the following code in a control and the reader doesn't seem to be
working. The connection details are correct.

Any ideas? I am reading data from this database to log the user and a lot of
the details are the same.

public class AdminNavigation : System.Web.UI.UserControl

{

protected System.Web.UI.WebControls.HyperLink hypProducts;

protected System.Web.UI.WebControls.Label lblDiv1;

protected System.Web.UI.WebControls.HyperLink hypOrders;

protected System.Web.UI.WebControls.Label lblDiv2;

protected System.Web.UI.WebControls.HyperLink hypSales;

protected System.Web.UI.WebControls.Label lblDiv3;

protected System.Web.UI.WebControls.HyperLink hypReviews;

protected System.Web.UI.WebControls.Label lblDiv4;

protected System.Web.UI.WebControls.HyperLink hypEmail;

protected System.Web.UI.WebControls.Label lblDiv5;

protected System.Web.UI.WebControls.HyperLink hypUsers;

protected System.Web.UI.WebControls.Label lblDiv6;

protected System.Web.UI.WebControls.HyperLink hypAdmin;

private void Page_Load(object sender, System.EventArgs e)

{

if (!Context.User.Identity.IsAuthenticated)

{

this.hypProducts.Visible = false;

this.hypOrders.Visible = false;

this.hypSales.Visible = false;

this.hypReviews.Visible = false;

this.hypEmail.Visible = false;

this.hypUsers.Visible = false;

this.hypAdmin.Visible = false;

this.lblDiv1.Visible = false;

this.lblDiv2.Visible = false;

this.lblDiv3.Visible = false;

this.lblDiv4.Visible = false;

this.lblDiv5.Visible = false;

this.lblDiv6.Visible = false;

}

SqlConnection con;

string sql;

SqlCommand cmd;

SqlDataReader AddAdminreader;

con = new SqlConnection("data source=127.0.0.1; initial
catalog=ConsoleJunkies; user id=****; password=****");

sql = "SELECT AddAdmin FROM [Admin] WHERE AdminID='" +
Context.User.Identity.Name.ToString() + "'";

cmd = new SqlCommand(sql, con);

con.Open();

AddAdminreader = cmd.ExecuteReader(CommandBehavior.CloseConnection);

if (AddAdminreader.Read())

{

if (AddAdminreader["AddAdmin"].ToString()=="1")

{

//Allowed full admin

this.hypProducts.Visible = true;

this.hypOrders.Visible = true;

this.hypSales.Visible = true;

this.hypReviews.Visible = true;

this.hypEmail.Visible = false;

this.hypUsers.Visible = false;

this.hypAdmin.Visible = false;

this.lblDiv1.Visible = true;

this.lblDiv2.Visible = true;

this.lblDiv3.Visible = true;

this.lblDiv4.Visible = false;

this.lblDiv5.Visible = false;

this.lblDiv6.Visible = false;

}


}

AddAdminreader.Close();

}
 
Forget the reader, for what you are doing, you should be using the
..ExecuteScalar() method on the command object. ExecuteScalar returns the
first column of the first row of the query.

HTH,
--Michael
 
Back
Top