Login page
-
i have a login page that accepts username and password and checks it with the value in the database,if the values match the next form is loaded otherwise error message appears. The stored procedure I used is CREATE PROCEDURE sp_User @NAME char(50),@PASSWORD varchar(50) AS SELECT COUNT(*)AS Num_of_User FROM LOGINTAB WHERE (((LOGINTAB.NAME)=@NAME) AND ((LOGINTAB.PASSWORD)=@PASSWORD)); GO The database connection does not return any error but even if i give the correct username and password i get error message""Invalid Login, please try again!";and"Invalid Login!". I got the code from the url www.daniweb.com/tutorials/tutorial24148.htm it is called "Simple ASP.Net Login Page using C#" The code is as follows private void Button1_Click(object sender, System.EventArgs e) { if (Page.IsValid) { if (DBConnection(txtUserName.Text.Trim(), txtPassword.Text.Trim())) { WebForm2 wf2=new WebForm2(); WebForm2.Visible=true; } else { lblMessage.Text = "Invalid Login, please try again!"; } } } private bool DBConnection(string txtUser, string txtPass) { SqlConnection myConn = new SqlConnection("server=;database=login;user id=;password="); SqlCommand myCmd = new SqlCommand("sp_User", myConn); myCmd.CommandType = CommandType.StoredProcedure; SqlParameter objParam1; SqlParameter objParam2; SqlParameter returnParam; objParam1 = myCmd.Parameters.Add ("@NAME", SqlDbType.Char); objParam2 = myCmd.Parameters.Add ("@PASSWORD", SqlDbType.VarChar); returnParam = myCmd.Parameters.Add ("@Num_of_User", SqlDbType.Int); objParam1.Direction = ParameterDirection.Input; objParam2.Direction = ParameterDirection.Input; returnParam.Direction = ParameterDirection.ReturnValue; objParam1.Value = txtUser; objParam2.Value = txtPass; /* Label1.Text=objParam1.Value.ToString(); Label2.Text=objParam2.Value.ToString();These 2 lines that are commented outputs the label1 and label2 with the name and password i gave in run time this is just to check if this part is working fine*/ try { if (myConn.State.Equals(ConnectionState.Closed)) { myConn.Open(); myCmd.ExecuteNonQuery(); } /*the problem is that the returnParam returns 0 and not 1 and i found this out by using a textbox that stores the returnParam TextBox1.Text=returnParam.Value.ToString(); */ if ((int)returnParam.Value<1) { lblM
-
i have a login page that accepts username and password and checks it with the value in the database,if the values match the next form is loaded otherwise error message appears. The stored procedure I used is CREATE PROCEDURE sp_User @NAME char(50),@PASSWORD varchar(50) AS SELECT COUNT(*)AS Num_of_User FROM LOGINTAB WHERE (((LOGINTAB.NAME)=@NAME) AND ((LOGINTAB.PASSWORD)=@PASSWORD)); GO The database connection does not return any error but even if i give the correct username and password i get error message""Invalid Login, please try again!";and"Invalid Login!". I got the code from the url www.daniweb.com/tutorials/tutorial24148.htm it is called "Simple ASP.Net Login Page using C#" The code is as follows private void Button1_Click(object sender, System.EventArgs e) { if (Page.IsValid) { if (DBConnection(txtUserName.Text.Trim(), txtPassword.Text.Trim())) { WebForm2 wf2=new WebForm2(); WebForm2.Visible=true; } else { lblMessage.Text = "Invalid Login, please try again!"; } } } private bool DBConnection(string txtUser, string txtPass) { SqlConnection myConn = new SqlConnection("server=;database=login;user id=;password="); SqlCommand myCmd = new SqlCommand("sp_User", myConn); myCmd.CommandType = CommandType.StoredProcedure; SqlParameter objParam1; SqlParameter objParam2; SqlParameter returnParam; objParam1 = myCmd.Parameters.Add ("@NAME", SqlDbType.Char); objParam2 = myCmd.Parameters.Add ("@PASSWORD", SqlDbType.VarChar); returnParam = myCmd.Parameters.Add ("@Num_of_User", SqlDbType.Int); objParam1.Direction = ParameterDirection.Input; objParam2.Direction = ParameterDirection.Input; returnParam.Direction = ParameterDirection.ReturnValue; objParam1.Value = txtUser; objParam2.Value = txtPass; /* Label1.Text=objParam1.Value.ToString(); Label2.Text=objParam2.Value.ToString();These 2 lines that are commented outputs the label1 and label2 with the name and password i gave in run time this is just to check if this part is working fine*/ try { if (myConn.State.Equals(ConnectionState.Closed)) { myConn.Open(); myCmd.ExecuteNonQuery(); } /*the problem is that the returnParam returns 0 and not 1 and i found this out by using a textbox that stores the returnParam TextBox1.Text=returnParam.Value.ToString(); */ if ((int)returnParam.Value<1) { lblM
-
i have a login page that accepts username and password and checks it with the value in the database,if the values match the next form is loaded otherwise error message appears. The stored procedure I used is CREATE PROCEDURE sp_User @NAME char(50),@PASSWORD varchar(50) AS SELECT COUNT(*)AS Num_of_User FROM LOGINTAB WHERE (((LOGINTAB.NAME)=@NAME) AND ((LOGINTAB.PASSWORD)=@PASSWORD)); GO The database connection does not return any error but even if i give the correct username and password i get error message""Invalid Login, please try again!";and"Invalid Login!". I got the code from the url www.daniweb.com/tutorials/tutorial24148.htm it is called "Simple ASP.Net Login Page using C#" The code is as follows private void Button1_Click(object sender, System.EventArgs e) { if (Page.IsValid) { if (DBConnection(txtUserName.Text.Trim(), txtPassword.Text.Trim())) { WebForm2 wf2=new WebForm2(); WebForm2.Visible=true; } else { lblMessage.Text = "Invalid Login, please try again!"; } } } private bool DBConnection(string txtUser, string txtPass) { SqlConnection myConn = new SqlConnection("server=;database=login;user id=;password="); SqlCommand myCmd = new SqlCommand("sp_User", myConn); myCmd.CommandType = CommandType.StoredProcedure; SqlParameter objParam1; SqlParameter objParam2; SqlParameter returnParam; objParam1 = myCmd.Parameters.Add ("@NAME", SqlDbType.Char); objParam2 = myCmd.Parameters.Add ("@PASSWORD", SqlDbType.VarChar); returnParam = myCmd.Parameters.Add ("@Num_of_User", SqlDbType.Int); objParam1.Direction = ParameterDirection.Input; objParam2.Direction = ParameterDirection.Input; returnParam.Direction = ParameterDirection.ReturnValue; objParam1.Value = txtUser; objParam2.Value = txtPass; /* Label1.Text=objParam1.Value.ToString(); Label2.Text=objParam2.Value.ToString();These 2 lines that are commented outputs the label1 and label2 with the name and password i gave in run time this is just to check if this part is working fine*/ try { if (myConn.State.Equals(ConnectionState.Closed)) { myConn.Open(); myCmd.ExecuteNonQuery(); } /*the problem is that the returnParam returns 0 and not 1 and i found this out by using a textbox that stores the returnParam TextBox1.Text=returnParam.Value.ToString(); */ if ((int)returnParam.Value<1) { lblM
Your stored procedure does not return any values back through the parameters. It returns a result set of data that contains a single row with a single column. You should use
ExecuteScalar()
and NOTExecuteNonQuery()
.ExecuteNonQuery()
is for when you don't expect any result sets. e.g.int result = myCmd.ExecuteNonQuery();
result will contain the number of rows in LOGIN tab that match the supplied username and password. Does this help?
My: Blog | Photos WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More
-
Your stored procedure does not return any values back through the parameters. It returns a result set of data that contains a single row with a single column. You should use
ExecuteScalar()
and NOTExecuteNonQuery()
.ExecuteNonQuery()
is for when you don't expect any result sets. e.g.int result = myCmd.ExecuteNonQuery();
result will contain the number of rows in LOGIN tab that match the supplied username and password. Does this help?
My: Blog | Photos WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More
HI, Even after i changed it to executescalar i am not getting the result as 1. so if u have some other idea please help. P.S. thanks for ur help.
-
HI, Even after i changed it to executescalar i am not getting the result as 1. so if u have some other idea please help. P.S. thanks for ur help.
hi i got the desired output i had to change the sp. crate procedure sp_user @NAME char(50), @PASSWORD varchar(50), @NO_OF_users INT OUTPUT AS SELECT @NO_OF_users = COUNT(*) FROM LOGINTAB WHERE (((LOGINTAB.NAME)=@NAME) AND ((LOGINTAB.PASSWORD)=@PASSWORD)); PRINT @NO_OF_users thanks again bye sarah