Login Form with MS SQL Backend

How to create a Login form with sql backend

Page 1 of 1

3 Replies - 5999 Views - Last Post: 25 November 2008 - 06:56 AM Rate Topic: -----

#1 hruggiano  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 4
  • Joined: 24-November 08

Login Form with MS SQL Backend

Posted 24 November 2008 - 03:42 PM

Hello everyone, i'd like to know how to create a login form in c++ .net , checking data against two sql fields (user and pass).
Thanks!
Is This A Good Question/Topic? 0
  • +

Replies To: Login Form with MS SQL Backend

#2 PsychoCoder  Icon User is offline

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

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

Re: Login Form with MS SQL Backend

Posted 24 November 2008 - 04:25 PM

[rules][/rules]
Was This Post Helpful? 0
  • +
  • -

#3 hruggiano  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 4
  • Joined: 24-November 08

Re: Login Form with MS SQL Backend

Posted 24 November 2008 - 05:11 PM

View PostPsychoCoder, on 24 Nov, 2008 - 03:25 PM, said:

[rules][/rules]


Ok, sorry about that, Well, this is what i've done, textbox 1 is for user and 2 for password, I've created a stored procedure in sql that returns 1 if user and password match

create procedure validate
@user as nvarchar(50),
@pass as nvarchar(max)
AS
Begin
select count(*) as Validated from autenticacion where
usuario = @user and pass = @pass
end



This is the code I entered on the submit button...

 {
			string accion = "exec validate '" + TextBox1.Text + "', '" + TextBox2.Text + "'";
			SqlConnection cnn = new SqlConnection(@"Data Source=10.1.6.2\INVESTIT;Initial Catalog=privada;Persist Security Info=True;User ID=sa;Password=PASSWORD");
			SqlCommand InsertCommand = new SqlCommand();
			InsertCommand.Connection = cnn;
			InsertCommand.CommandText = accion;
			try
			{
				cnn.Open();
				int validateduser;
				validateduser = Convert.ToInt32(InsertCommand.ExecuteScalar());

				if (validateduser == 1)
					{FormsAuthentication.SetAuthCookie(TextBox1.Text,true);
					
				}
				else
				{FormsAuthentication.RedirectToLoginPage();}
			}
			catch (Exception ex)
			{
				Response.Write(ex.ToString());
			}
			finally
			{
				cnn.Close();
			}
			
		}



But the thing is that i don't know if this is ok, and how to check for this cookie when other pages load...
Was This Post Helpful? 1

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

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

Re: Login Form with MS SQL Backend

Posted 25 November 2008 - 06:56 AM

1. You do not have to create a SQL string for the stored procedure like you have to in a Query window.

code for calling stored procedure with parameters
int validateUser = 0;

using(SqlConnection cnn = new SqlConnection(@"Data Source=10.1.6.2\INVESTIT;Initial Catalog=privada;Persist Security Info=True;User ID=sa;Password=PASSWORD"))
{
     SqlCommand cmd = new SqlCommand();
     cmd.Connection = cnn;
     cmd.CommandText = "validate"; //name of stored procedure
     cmd.CommandType = CommandType.StoredProcedure;

     SqlParameterCollection sqlParams = cmd.Parameters;
     sqlParams.AddWithValue("@user", textBox1.Text); //parameter names
     sqlParams.AddWithValue("@pass", textBox2.Text); //parameter names

     cnn.Open();
     validateUser = (int)cmd.ExecuteScalar(); //returns the first column of the first row
}

//now use your validation code to set the cookie.


This post has been edited by eclipsed4utoo: 25 November 2008 - 06:57 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1