SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 Development Tools
 Other Development Tools
 database connection
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

unleashed-my-freedom
Starting Member

Singapore
5 Posts

Posted - 10/15/2011 :  20:55:58  Show Profile  Reply with Quote
string username = TextBox1.Text;
string password = TextBox2.Text;
OleDbConnection connection = null;
OleDbCommand command = null;
OleDbDataReader dataReader = null;
try
{
string connectionString = ConfigurationManager.ConnectionStrings["ChatDBConnectionString"].ConnectionString;
connection = new OleDbConnection(connectionString);
connection.Open();
//prepare sql statements
string sql = "SELECT * from Staff where username='" + username + "'And Password='" + password + "'";
command = new OleDbCommand(sql, connection);
dataReader = command.ExecuteReader();

while (dataReader.Read())
{

username = dataReader.GetString(3);
Session.Add("username", username);

}
dataReader.Close();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
//cleanup object
finally
{
if (connection != null)
connection.Close();
}

How do I change OLEdb to SQL connection?

judy

GilaMonster
Flowing Fount of Yak Knowledge

South Africa
4507 Posts

Posted - 10/15/2011 :  21:36:51  Show Profile  Visit GilaMonster's Homepage  Reply with Quote
Assuming this is .Ney, look at the SQLDataClient class.

--
Gail Shaw
SQL Server MVP
Go to Top of Page

unleashed-my-freedom
Starting Member

Singapore
5 Posts

Posted - 10/16/2011 :  00:27:16  Show Profile  Reply with Quote
what do you mean?

judy
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47040 Posts

Posted - 10/16/2011 :  00:31:00  Show Profile  Reply with Quote
you mean this?

http://searchsqlserver.techtarget.com/feature/Creating-basic-ADONET-data-objects-with-SqlClient

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

GilaMonster
Flowing Fount of Yak Knowledge

South Africa
4507 Posts

Posted - 10/16/2011 :  01:25:41  Show Profile  Visit GilaMonster's Homepage  Reply with Quote
quote:
Originally posted by unleashed-my-freedom

what do you mean?


I mean 'open up the documentation, browse to the section on the SQLDataClient class and read up on that class'.

--
Gail Shaw
SQL Server MVP
Go to Top of Page

unleashed-my-freedom
Starting Member

Singapore
5 Posts

Posted - 10/16/2011 :  02:05:21  Show Profile  Reply with Quote
string username = TextBox1.Text;
string password = TextBox2.Text;
SqlConnection connection = null;
SqlCommand command = null;
SqlDataReader dataReader = null;
try
{
string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
connection = new SqlConnection(connectionString);
connection.Open();
//prepare sql statements
string sql = "SELECT * from Staff where username='" + username + "'And Password='" + password + "'";
command = new SqlCommand(sql, connection);
dataReader = command.ExecuteReader();

while (dataReader.Read())
{

username = dataReader.GetString(3);
Session.Add("username", username);

}
dataReader.Close();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
//cleanup object
finally
{
if (connection != null)
connection.Close();
}
Response.Redirect("./default.aspx");
}


I had tried changing from OLEdb to SQL. May I know if it is correct?

judy
Go to Top of Page

GilaMonster
Flowing Fount of Yak Knowledge

South Africa
4507 Posts

Posted - 10/16/2011 :  02:23:37  Show Profile  Visit GilaMonster's Homepage  Reply with Quote
Did you try it? Did it throw errors? Did you consult the documentation?

--
Gail Shaw
SQL Server MVP
Go to Top of Page

unleashed-my-freedom
Starting Member

Singapore
5 Posts

Posted - 10/16/2011 :  02:39:27  Show Profile  Reply with Quote
Actually I had created a registration page for users to sign up an account and a database to store all the information such as username and password. I had also created a login page for users to login after creating an account. How do I link the database between these two pages?

judy
Go to Top of Page

GilaMonster
Flowing Fount of Yak Knowledge

South Africa
4507 Posts

Posted - 10/16/2011 :  02:49:14  Show Profile  Visit GilaMonster's Homepage  Reply with Quote
I just noticed something:

quote:
string sql = "SELECT * from Staff where username='" + username + "'And Password='" + password + "'";

This is an open invitation to SQL injection. You're asking to be hacked and hacked badly. That should be a stored procedure call and should be correctly parameterised. If it can't be a procedure, it should still be parameterised.

--
Gail Shaw
SQL Server MVP
Go to Top of Page

unleashed-my-freedom
Starting Member

Singapore
5 Posts

Posted - 10/16/2011 :  03:05:47  Show Profile  Reply with Quote
What should I do to prevent it? I'm trying to logged in as a registered user but even the password is wrong, users are still able to logged in. How do I solve this error?

judy
Go to Top of Page

afrika
Flowing Fount of Yak Knowledge

Nigeria
2702 Posts

Posted - 10/23/2011 :  04:51:56  Show Profile  Reply with Quote
well like gilamonster said, your code is very wrong ( bad practice ) to put the sql string like that.

I would advice you to either use LINQ, entity framework or a stored proceedure. See my post here

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=166467
Go to Top of Page

jassi.singh
Posting Yak Master

122 Posts

Posted - 10/23/2011 :  05:19:54  Show Profile  Visit jassi.singh's Homepage  Reply with Quote
definetly you have codded incorrectly, please post your code written on login click button.

Please mark answer as accepted if it helped you.

Thanks,
Jassi Singh
Go to Top of Page

afrika
Flowing Fount of Yak Knowledge

Nigeria
2702 Posts

Posted - 10/23/2011 :  13:26:21  Show Profile  Reply with Quote
quote:
Originally posted by jassi.singh

definetly you have codded incorrectly, please post your code written on login click button.

Please mark answer as accepted if it helped you.

Thanks,
Jassi Singh



How do you mark as answer ? lol
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000