how to link my button

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 1641 Views - Last Post: 28 August 2009 - 08:35 PM Rate Topic: -----

#1 looper  Icon User is offline

  • New D.I.C Head

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

how to link my button

Posted 26 August 2009 - 02:16 AM

I am trying to link my button to another aspx page, i have two text box and one button

this is my code
SqlConnection con = new SqlConnection(connectString);
			con.Open();
			string thesql = "select * from Staff where staffNumber=' " + txtStaffsNo + " 'and cardNumber=' " + txtCardNo + " '";
			SqlCommand cmd = new SqlCommand(thesql, con);
			SqlDataReader reader = cmd.ExecuteReader();
			while (reader.Read())
			{
 string order = reader[2].ToString();
				if ("txtStaffsNo" == "staffNumber")
				{
					Response.Redirect("WelcomeT.aspx");
				}
					else if ("txtStaffsNo" == "")
					{
						Response.Write("Please fill in a Staff Number");
					}
				else
				{
			 
  Response.Write("Please enter a valid Staff Number");
				}
			}
			reader.Close();
			con.Close();

***added code tags*** - jjsaw5***

But nothing appear after clicking the button

Is This A Good Question/Topic? 0
  • +

Replies To: how to link my button

#2 jjsaw5  Icon User is offline

  • I must break you
  • member icon

Reputation: 90
  • View blog
  • Posts: 3,060
  • Joined: 04-January 08

Re: how to link my button

Posted 26 August 2009 - 05:34 AM

:code:
Was This Post Helpful? 0
  • +
  • -

#3 NitinPro  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 16
  • Joined: 25-August 09

Re: how to link my button

Posted 26 August 2009 - 06:18 AM

Hey Dude.Forlinking to another page in asp u use response.redirect("nameof page.aspx")


View Postlooper, on 26 Aug, 2009 - 01:16 AM, said:

I am trying to link my button to another aspx page, i have two text box and one button

this is my code
SqlConnection con = new SqlConnection(connectString);
			con.Open();
			string thesql = "select * from Staff where staffNumber=' " + txtStaffsNo + " 'and cardNumber=' " + txtCardNo + " '";
			SqlCommand cmd = new SqlCommand(thesql, con);
			SqlDataReader reader = cmd.ExecuteReader();
			while (reader.Read())
			{
 string order = reader[2].ToString();
				if ("txtStaffsNo" == "staffNumber")
				{
					Response.Redirect("WelcomeT.aspx");
				}
					else if ("txtStaffsNo" == "")
					{
						Response.Write("Please fill in a Staff Number");
					}
				else
				{
			 
  Response.Write("Please enter a valid Staff Number");
				}
			}
			reader.Close();
			con.Close();

***added code tags*** - jjsaw5***

But nothing appear after clicking the button

Was This Post Helpful? 0
  • +
  • -

#4 NitinPro  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 16
  • Joined: 25-August 09

Re: how to link my button

Posted 26 August 2009 - 06:30 AM

Hey dude..I don't understand what ur main objective from this code..U simply have a sql query for retrieving corresponding data ..and simply check data reader and stored value in another string data type..

But if u will check ur textbox data u use textboxname.text to with read.getstring[0].if it will be correct then u simply link by response.redirect(anothrpage.aspx");

hope u will try to match the values from sql data..

View Postlooper, on 26 Aug, 2009 - 01:16 AM, said:

I am trying to link my button to another aspx page, i have two text box and one button

this is my code
SqlConnection con = new SqlConnection(connectString);
			con.Open();
			string thesql = "select * from Staff where staffNumber=' " + txtStaffsNo + " 'and cardNumber=' " + txtCardNo + " '";
			SqlCommand cmd = new SqlCommand(thesql, con);
			SqlDataReader reader = cmd.ExecuteReader();
			while (reader.Read())
			{
 string order = reader[2].ToString();
				if ("txtStaffsNo" == "staffNumber")
				{
					Response.Redirect("WelcomeT.aspx");
				}
					else if ("txtStaffsNo" == "")
					{
						Response.Write("Please fill in a Staff Number");
					}
				else
				{
			 
  Response.Write("Please enter a valid Staff Number");
				}
			}
			reader.Close();
			con.Close();

***added code tags*** - jjsaw5***

But nothing appear after clicking the button

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: how to link my button

Posted 26 August 2009 - 07:42 AM

to redirect to a page in the same directory..

Response.Redirect("~/PageName.aspx");



however, that code will not be hit. Even if your query returns data, this if statement will always return false...

if ("txtStaffsNo" == "staffNumber")
{
     // the code will never hit here
}
else
{
    // the code will always hit here
}



why will it always go to the "else"? because the literal string "txtStaffsNo" will never equal the literal string "staffNumber".
Was This Post Helpful? 0
  • +
  • -

#6 W3bDev  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 42
  • View blog
  • Posts: 379
  • Joined: 15-March 09

Re: how to link my button

Posted 26 August 2009 - 07:55 AM

eclipsed is right, you are comparing two String literals, meaning the string here will never change... You need to compare a variable with a literal, like this:

if(MyTextBox.text == "ValueIWantitToBe"){
// Perform action
}
else{
// Perform something else
}

// or

if(myVariable(string) == "ValueIWantItToBe){
// Perform action
}
else{
// Perform something else
}



This post has been edited by W3bDev: 26 August 2009 - 07:56 AM

Was This Post Helpful? 0
  • +
  • -

#7 looper  Icon User is offline

  • New D.I.C Head

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

Re: how to link my button

Posted 26 August 2009 - 08:32 AM

My purpose of the code is to access to another page if the database is right . Even if i tried this code it dosen't work
SqlConnection con = new SqlConnection(connectString);
			con.Open();
			string thesql = "select * from Staff where staffNumber=' " + txtStaffsNo + " 'and cardNumber=' " + txtCardNo + " '";
			SqlCommand cmd = new SqlCommand(thesql, con);
			SqlDataReader reader = cmd.ExecuteReader();
			while (reader.Read())
			{
string order = reader[2].ToString();
Response.Redirect("~/WelcomeT.aspx");
reader.Close();
			con.Close();


Is like a login kind of stuff
Was This Post Helpful? 0
  • +
  • -

#8 W3bDev  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 42
  • View blog
  • Posts: 379
  • Joined: 15-March 09

Re: how to link my button

Posted 26 August 2009 - 09:13 AM

First, create a label to contain your errors if they are outputted, I labeled it myLabel in the code, also do a try catch block to catch any errors. I also noticed you were closing the reader and the connection on the first initiator of the while loop.

			SqlConnection con = new SqlConnection(connectString);

			try{

			con.Open();
			string thesql = "select * from Staff where staffNumber='" + txtStaffsNo + "'and cardNumber=' " + txtCardNo + " '";
			SqlCommand cmd = new SqlCommand(thesql, con);
			SqlDataReader reader = cmd.ExecuteReader();
			while (reader.Read())
			{
				string order = reader[2].ToString();
				Response.Redirect("~/WelcomeT.aspx");
			}

			}
			catch(Exception err){

				myLabel.Text = "Error: " + err;
			}

			finally{

				reader.Close();
				con.Close();
			}

This post has been edited by W3bDev: 26 August 2009 - 09:14 AM

Was This Post Helpful? 0
  • +
  • -

#9 looper  Icon User is offline

  • New D.I.C Head

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

Re: how to link my button

Posted 26 August 2009 - 08:06 PM

i tried using this code, but which page do i put the label at? my current one or the page i'm going to? There seems to be error in the try catch code

			SqlConnection con = new SqlConnection(connectString);

			try{

			con.Open();
			string thesql = "select * from Staff where staffNumber='" + txtStaffsNo + "'and cardNumber=' " + txtCardNo + " '";
			SqlCommand cmd = new SqlCommand(thesql, con);
			SqlDataReader reader = cmd.ExecuteReader();
			while (reader.Read())
			{
				string order = reader[2].ToString();
				Response.Redirect("~/WelcomeT.aspx");
			}

			}
			catch(Exception err){

				myLabel.Text = "Error: " + err;
			}

			finally{

				reader.Close();
				con.Close();
			}

Was This Post Helpful? 0
  • +
  • -

#10 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

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

Re: how to link my button

Posted 26 August 2009 - 08:12 PM

you put the label on the current page.
Was This Post Helpful? 0
  • +
  • -

#11 looper  Icon User is offline

  • New D.I.C Head

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

Re: how to link my button

Posted 26 August 2009 - 09:39 PM

ok i did it but there are still alot of errors in the try-catch finally method.
this is my code my asp code by chance anything wrong with it?
<asp:Button ID="btnProceed" runat="server" Text="Proceed" 
			onclick="btnProceed_Click" PostBackUrl="~/WelcomeT.aspx" />
		
		
		<asp:Label ID="myLabel" runat="server" Text="myLabel"></asp:Label>

Was This Post Helpful? 0
  • +
  • -

#12 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1638
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: how to link my button

Posted 26 August 2009 - 09:49 PM

You say "a lot of errors", so what are the errors? And which code is causing them?
Was This Post Helpful? 0
  • +
  • -

#13 looper  Icon User is offline

  • New D.I.C Head

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

Re: how to link my button

Posted 26 August 2009 - 10:03 PM

error like invalid token 'catch in class, type or namespace defination, or end-of-file expected
Was This Post Helpful? 0
  • +
  • -

#14 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1638
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: how to link my button

Posted 26 August 2009 - 10:07 PM

Generally that error is generated because you;re miossing a closing bracket (If using C#) or an End Sub or End Function call. Show us your code, it's impossible to say what's causing it if we cant see it :)
Was This Post Helpful? 0
  • +
  • -

#15 looper  Icon User is offline

  • New D.I.C Head

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

Re: how to link my button

Posted 26 August 2009 - 10:11 PM

Basically i paste the codes from dev whom suggest that i should use it

 catch(Exception err){

				myLabel.Text = "Error: " + err;
			}

			finally{
 reader.Close();
				con.Close();
			}


This post has been edited by looper: 26 August 2009 - 10:12 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2