Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2012-12-27
7
Medium Priority
?
515 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
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 

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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

783 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