asp.net C# page_load event reads database table and redirects
Hello,
I am trying to code the page_load event read a database table, if a column "Active" has a "1", then redirect to another page, otherwise, load the page normally,
My test page is as follows:
----------------
protected void Page_Load(object sender, EventArgs e)
{
getData();
}
private void getData()
{
// Create a connection
SqlConnection MsgEmergency = new SqlConnection(connectionString);
// Open the connection
MsgEmergency.Open();
// Create a String to hold the query.
string query = "SELECT Active FROM tbl_News WHERE Active=1";
// Create a SqlCommand object and pass the constructor the connection string and the query string.
SqlCommand queryCommand = new SqlCommand(query, MsgEmergency);
//SqlDataReader dr = queryCommand.ExecuteReader();
int results = (Int32)queryCommand.ExecuteScalar();
...guaranteed to return exactly one row? If not, then ExecuteScalar is not going to work, and you would need to revert to the ExecuteReader method.
Codeaddict7423
ASKER
kaufmed,
Thank you for the quick reply.
I tried a new tactic. My code follows:
----------
private void getWarning()
{
SqlConnection MsgEmergency = new SqlConnection(connectionString);
SqlDataSource sds = new SqlDataSource();
SqlDataReader EmergencyReader;
sds.SelectCommand = "SELECT Active FROM tblPressRelease_PIO_Emergency WHERE Active=1";
int results = Convert.ToInt32(EmergencyReader.GetByte(“Active”));
if (results != 1)
Response.Redirect("default_emergency.aspx"); //<-- If emergency notice is active
else
Response.Redirect("default.aspx"); //<-- If no emergency notice is active
}
----------
My problem is that the "@Active" is underlined and VS2008 is indicating ") is espected"
Again, my goal is simple: I'm trying to have the "default.aspx" page check to see if there are any rows in my table where "Active = 1" if so, redirect , if not, then load the page as normal..
Again, thanks for the quick reply.
kaufmed
As I asked earlier, does the query return exactly one row?
Your help has saved me hundreds of hours of internet surfing.
fblack61
Codeaddict7423
ASKER
Yes, when I used the ExecuteScalar option, I received only one row.
My goal to have the default.aspx page check a database table on page_load for any "active = 1" entries. If so, then redirect, if not, load the default.aspx normally.
Any help would be greatly appreciated.
kaufmed
for any "active = 1"
What should happen if more than one row is returned?
Codeaddict7423
ASKER
kaufmed,
My code is now ammended to include a SqlDataReader as follows:
----------
protected void Page_Load(object sender, EventArgs e)
{
// getData();
getWarning();
}
private void getWarning()
{
SqlConnection MsgEmergency = new SqlConnection(connectionString);
SqlDataSource sds = new SqlDataSource();
SqlDataReader EmergencyReader;
sds.SelectCommand = "SELECT Active FROM tbl_News WHERE Active=1";
int results = Convert.ToInt32(EmergencyReader.GetByte(“Active”));
if (results != 1)
Response.Redirect("default_emergency.aspx"); //<-- If emergency notice is active
else
Response.Redirect("default.aspx"); //<-- If no emergency notice is active
}
--------------
The error message I see is "specified cast is not valid"
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
...guaranteed to return exactly one row? If not, then ExecuteScalar is not going to work, and you would need to revert to the ExecuteReader method.