Link to home
Start Free TrialLog in
Avatar of Codeaddict7423
Codeaddict7423Flag for United States of America

asked on

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();

            if (results != 1)


                    Response.Redirect("default_emergency.aspx");
                else
                    Response.Redirect("default.aspx");


            MsgEmergency.Close();
            }
           
-----------------------------------

ANY help would  be greatly appreciated.
Avatar of kaufmed
kaufmed
Flag of United States of America image

Is the query:

SELECT Active FROM tbl_News WHERE Active=1

...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.
Avatar of 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.
As I asked earlier, does the query return exactly one row?
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.
for any "active = 1"
What should happen if more than one row is returned?
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"

ANY HELP would be greatly appreciated.
ASKER CERTIFIED SOLUTION
Avatar of gaurang04
gaurang04
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial