?
Solved

Check/Redirect Based If Values Exist

Posted on 2011-10-19
3
Medium Priority
?
203 Views
Last Modified: 2012-05-12
Hello Experts,

I need help determiniing if a value exists before users can submit data on a page called register.aspx. If the value ghaoc_id exist on page register.aspx then they can submit. If ghaoc_id does not exist on page register.aspx then redirect to schedule.aspx. Please see all steps below but Step3 is actually what I need help with.

Step1: User can register courses by clicking on a register button on the schedule.aspx page. Once user clicks register button on the schedule.aspx page then they are redirected to login or create account if account does not already exist.

Step2: Once a user is authenticated then they are automatically sent to the register.aspx page passing two parameters to the schedule.aspx page for submittion. This all works fine.

Step3: User navigate to register.aspx after he/she logs in without choosing a Course on page schedule.aspx. I need to prevent the user from submitting data on register.aspx if the value for ghaoc_id DOES NOT EXIST. It would even be better to prevent the user from even seeing the content on register.aspx if the value ghaoc_id does not exist.

Step2:
REGISTER.ASPX CODEBEHIND:

using System;
using System.Configuration;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Net.Mail;
using System.Net.NetworkInformation;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

public partial class programinfo_ghap_secure_register : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        RetrieveUsersFirstLastName();

        // Create code to check to see if a value exist for ghaoc_id. If no value exist then redirect to page for user to select a course to enroll in and also disable submit button.
    }

    protected void RetrieveUsersFirstLastName()
    {       
        using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["HealthCourses"].ConnectionString))
        {
            string FirstLastName = Convert.ToString(Session["UserNameSessionID"]);

            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "HealthCourses_RetrieveUsersFLName";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = conn;

            cmd.Parameters.AddWithValue("@users_username", SqlDbType.VarChar).Value = FirstLastName;

            try
            {
                conn.Open();

                SqlDataReader rdr = cmd.ExecuteReader();

                if (rdr.Read())
                {
                    lblFLName.Text = rdr["users_flname"].ToString();
                    hf_users_id.Value = rdr["users_id"].ToString();
                }
            }

            catch (Exception ex)
            {
                ex.Message.ToString();
            }

            finally
            {
                conn.Close();
            }
        }
    }

    protected void btn_RegisterUser_Click(object sender, EventArgs e)
    {
        hf_ghaoc_id.Value = Request.QueryString["ghaoc_id"];
        hf_ghaocp_id.Value = Request.QueryString["ghaocp_id"];

        using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["HealthCourses"].ConnectionString))
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "HealthCourses_InsertUsersEnrolledCourses";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = conn;

            cmd.Parameters.AddWithValue("@users_id", SqlDbType.Int).Value = hf_users_id.Value;
            cmd.Parameters.AddWithValue("@ghaoc_id", SqlDbType.Int).Value = hf_ghaoc_id.Value;
            cmd.Parameters.AddWithValue("@ghaocp_id", SqlDbType.Int).Value = hf_ghaocp_id.Value;

            try
            {
                conn.Open();

                cmd.ExecuteNonQuery();

                Response.Redirect("register_success.aspx");
            }

            catch (Exception ex)
            {
                ex.Message.ToString();
            }

            finally
            {
                conn.Close();
            }
        }
    }
}

Open in new window

0
Comment
Question by:asp_net2
3 Comments
 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 2000 total points
ID: 36992566
change the page_load like this


protected void Page_Load(object sender, EventArgs e)
    {
       
      if ((Request.QueryString["ghaoc_id"] == null))
                  {
                  

                                                                                      Response.Redirect("schedule.aspx");
 
                                                                                      }
else
{


 RetrieveUsersFirstLastName();
}

    }
0
 
LVL 17

Expert Comment

by:Kiran Sonawane
ID: 36992599
OR

if(string.IsNullOrEmpty(Request.QueryString["ghaoc_id"]))
   Response.Redirect("schedule.aspx");
else
   RetrieveUsersFirstLastName();
 
0
 
LVL 4

Author Closing Comment

by:asp_net2
ID: 36992823
Thank you very much!!! That worked as I needed.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
There may be issues when you are trying to access Outlook or send & receive emails or due to Outlook crash which leads to corrupt or damaged PST file. To eliminate the corruption from your PST file, you need to repair the corrupt Outlook PST file. U…

588 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