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
Solved

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

Posted on 2012-12-27
7
509 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 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

837 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