Solved

asp.net C#  page_load event reads database table and redirects

Posted on 2012-12-27
7
504 Views
Last Modified: 2013-01-02
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.
0
Comment
Question by:Codeaddict7423
  • 3
  • 3
7 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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.
0
 

Author Comment

by:Codeaddict7423
Comment Utility
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.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
As I asked earlier, does the query return exactly one row?
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:Codeaddict7423
Comment Utility
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.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
for any "active = 1"
What should happen if more than one row is returned?
0
 

Author Comment

by:Codeaddict7423
Comment Utility
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.
0
 
LVL 2

Accepted Solution

by:
gaurang04 earned 500 total points
Comment Utility
protected void Page_Load(object sender, EventArgs e)
        {
               

           // getData();
            getWarning();
           
        }



private void getWarning()
{
        using(SqlConnection con = new SqlConnection(connectionString))
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("SELECT Active FROM tbl_News  WHERE Active=1",con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);

            if (dt.Rows.Count > 0)
            {
                Response.Redirect("default_emergency.aspx"); //<-- If emergency notice is active
            }
            else
            {
                Response.Redirect("default.aspx"); //<-- If no emergency notice is active
            }

        }
}
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now