Exception\Error in c# asp.net

Exception\Error in c# asp.net

Page 1 of 1

3 Replies - 1280 Views - Last Post: 11 November 2010 - 08:09 AM Rate Topic: -----

#1 skrose  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 02-April 09

Exception\Error in c# asp.net

Posted 11 November 2010 - 06:10 AM

I need assistance figuring out a very small assignment. Taking my first c# class and I asked my co-worker for a quick task to turn in for extra credit. Here is the description of an area I can't figure out:

"If one of these parameters is missing, throw an error."

The easiest thing for me to check is the missing "type" in the url.

I have spent a lot of time reading and googling for assistance, but I am not able to figure this out.

Any help is appeciated! Thanks!!


Here's the code that does not catch any errors, but continues to display what's in the default.aspx. The ID and TYPE are empty in the URL.

using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;


public partial class _Default : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        string idString;
	string typeString;
        
        idString = Request.QueryString["ID"];
        typeString = Request.QueryString["TYPE"];

                 
        try 
        {
            if (typeString == null);
        }

        catch (Exception ex)
        {
            Response.Write("<b> Error messsage: </b><br />" + ex.Message);
        }
  
        FlashVars1.Text = string.Format("<param name=\"FlashVars\" value=\"id={0}&type={1}\" />", idString, typeString);

         
    }

}



Is This A Good Question/Topic? 0
  • +

Replies To: Exception\Error in c# asp.net

#2 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Exception\Error in c# asp.net

Posted 11 November 2010 - 06:27 AM

Your code is wrong, try this:


public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string idString;
	string typeString;
        
        idString = Request.QueryString["ID"];
        typeString = Request.QueryString["TYPE"];          
                 
        if (typeString == null);
        {
            Response.Write("<b> Error messsage: </b><br />" + ex.Message);            
        }
        else
        {
           FlashVars1.Text = string.Format("<param name=\"FlashVars\" value=\"id={0}&type={1}\" />", idString, typeString);
        }
    }
}


In your code, the catch block is never actually invoked.

This post has been edited by Sergio Tapia: 11 November 2010 - 06:28 AM

Was This Post Helpful? 1
  • +
  • -

#3 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Exception\Error in c# asp.net

Posted 11 November 2010 - 07:55 AM

or you could do this..

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["ID"] == null ||
            Request.QueryString["TYPE"] == null)
        {
             Response.Write("<b> Error messsage: </b><br />Query String values are not valid");
             return;
        }

        string idString = Request.QueryString["ID"];
	string typeString= Request.QueryString["TYPE"];

        FlashVars1.Text = string.Format("<param name=\"FlashVars\" value=\"id={0}&type={1}\" />", idString, typeString);
        }
    }
}


Was This Post Helpful? 1
  • +
  • -

#4 skrose  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 02-April 09

Re: Exception\Error in c# asp.net

Posted 11 November 2010 - 08:09 AM

View Posteclipsed4utoo, on 11 November 2010 - 06:55 AM, said:

or you could do this..

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["ID"] == null ||
            Request.QueryString["TYPE"] == null)
        {
             Response.Write("<b> Error messsage: </b><br />Query String values are not valid");
             return;
        }

        string idString = Request.QueryString["ID"];
	string typeString= Request.QueryString["TYPE"];

        FlashVars1.Text = string.Format("<param name=\"FlashVars\" value=\"id={0}&type={1}\" />", idString, typeString);
        }
    }
}




Thanks!!!

I changed your suggestion just a bit and now my little project seems to be happier :bigsmile: I will play with it more after work and class tonight. I have several more errors I'm supposed to report. This is such a big help. I was stuck and now I can move forward.

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["ID"] == null || Request.QueryString["TYPE"] == null)
        {
             Response.Write("<b> Error messsage: </b><br />Query String values are not valid");
             return;
        }
        else
        {
        string idString = Request.QueryString["ID"];
	    string typeString= Request.QueryString["TYPE"];

        FlashVars1.Text = string.Format("<param name=\"FlashVars\" value=\"id={0}&type={1}\" />", idString, typeString);
        }
    }


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1