5 Replies - 782 Views - Last Post: 17 September 2009 - 12:41 PM Rate Topic: -----

#1 looper  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 26-August 09

try catch

Posted 16 September 2009 - 10:41 PM

hi i have a login in page and i want my label to show some words when i type the wrong values.

this is my code
 try
			{
				string connectString = ConfigurationManager.AppSettings["DatabaseConnectionString"];
				SqlConnection con = new SqlConnection(connectString);
				con.Open();
				string thesql = "select * from student where studMatricNumber='" + txtMatricNo.Text + "' and  studCardNumber = '" + txtSCardNo.Text + "';";
				SqlCommand cmd = new SqlCommand(thesql, con);
				SqlDataReader reader = cmd.ExecuteReader();
				
				while (reader.Read())
				{

					//name2 = reader["uid"].ToString();
					//Session["name2"] = name2;
					name = reader["studName"].ToString();
					Session["name"] = name;
					caregroupID = reader["caregroupID"].ToString();
					Session["caregroupID"] = caregroupID;
					uid = reader["uid"].ToString();
					Session["uid"] = uid;
					
					Response.Redirect("~/Profile2.aspx");
				}
				//Response.Redirect("~/Profile2.aspx");
				reader.Close();
				con.Close();
			}
		
			catch (Exception err)
			{
				Label1.Text = "can't work";
			  
				//myLabel2.Text = "Error: " + err;
			}



as you can see i want my label1.text to show the message "can't work" when i type the wrong values. Can someone help me?

Is This A Good Question/Topic? 0
  • +

Replies To: try catch

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

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

Re: try catch

Posted 17 September 2009 - 05:23 AM

can you better explain what "type the wrong values" means?

how many records are you expecting to return from the query? 1?
Was This Post Helpful? 0
  • +
  • -

#3 Aeternalis  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 291
  • Joined: 13-July 09

Re: try catch

Posted 17 September 2009 - 05:29 AM

ASP.NET is not my strongest point..

that said, I don't see where you are validating the login in this code. It simply is creating a database connection and executing a reader, this is completely different from logging into your site..perhaps I misunderstood the question?

can you clarify what you are trying to do?

If you are setting up a login for your website, are you using memberships and a login control (ASP.NET 2.0 I think) or are you validating the login information by handling the events of the login control? Those are the two methods I have seen for setting up logins to a website using a login control.

if your not using a login control, what ARE you doing.. and trying to accomplish?


more information will surely help!

Aet
Was This Post Helpful? 0
  • +
  • -

#4 looper  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 26-August 09

Re: try catch

Posted 17 September 2009 - 08:16 AM

i am not using any login control, my code is to make sure my studMatricNumber and studCardNumber is the same as the one in my database. so if it's correct it will proceed to the next page. If the values are wrong if will not redirect to the next page. but instead of not doing anything is it possible to make a label tt show's "please re-enter your infromation".?
Was This Post Helpful? 0
  • +
  • -

#5 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

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

Re: try catch

Posted 17 September 2009 - 10:14 AM

after you close the reader and connection, set the label's text. The Response.Redirect will redirect at that line. the rest of the code will not be run.
Was This Post Helpful? 0
  • +
  • -

#6 Aeternalis  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 291
  • Joined: 13-July 09

Re: try catch

Posted 17 September 2009 - 12:41 PM

Your problem is that your catch block only executes if an error is thrown. You are not handling the case where the reader does just fine but the data returned doesn't match the input data. In fact.. your not comparing the returned data to the input data at all as far as I can tell.

Where are you performing the check:
if( Session["caregroupId"] == inputCareGroupID.Text)
{  // caregroupID is validated redirect to the new page..

}
else
{ 
Label1.Text = "The information you supplied does not match the database!";
}





I hope this helps!
Aet
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1