Solved

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

Posted on 2012-12-27
7
511 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38725347
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
ID: 38725381
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 75

Expert Comment

by:käµfm³d 👽
ID: 38725396
As I asked earlier, does the query return exactly one row?
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

Author Comment

by:Codeaddict7423
ID: 38725557
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 75

Expert Comment

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

Author Comment

by:Codeaddict7423
ID: 38726731
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
ID: 38734687
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

751 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