Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
|
ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application .
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ADO.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 19th, 2008, 01:57 PM
Authorized User
 
Join Date: Sep 2007
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Post what is wrong in this code

Hi to all friends out there. I am unable to find mistake in this code. In this code i want to verify whether user name and password exit in database or not. if yes than it shows success message box.


Dim ds As DataSet
Dim da As SqlDataAdapter
Dim con As SqlConnection
Dim str As String
con = New SqlConnection("initial catalog=abc;data source=SQLSVR;user id=sa;password=xyz")
ds = New DataSet

str = " select username,password from login_form where (username=' " & TextBox1.Text & " ' and password = '" & TextBox2.Text & " ') "
da = New SqlDataAdapter(str, con)
da.Fill(ds, "login_form")
If ds.Tables("login_form").Rows.Count = 1 Then
MsgBox("successfull login")
Else
MsgBox("please check your username/password ")


when i am checking by breakpoint it shows that ds.tables.rows.... count is returning value 0. But in database those username and password exist.

please tell me where i am wrong.

Thank you in advance.
 
Old December 19th, 2008, 10:10 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

You have SPACES between your apostrophes and the values they surround. I will show you by changing your spaces to @ characters:
Code:
str = " select username,password from login_form where (username='@" & TextBox1.Text & "@' and password = '" & TextBox2.Text & "@') "
So if the user enters tha username "joe" you will be searching for a username of '[space]joe[space]' and not surpringly you don't find any matches.

I should note that this is inherently a bad way to do this. You are
(a) setting yourself up for SQL injection attacks and
(b) not able to hanle a user name such as O'Brien
 
Old December 19th, 2008, 10:37 PM
Authorized User
 
Join Date: Sep 2007
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Post thank you

thank you allot my friend.

from sql injection i think that can be avoided by using store procedure right but how i handle other thing mean o'johns like name like as you said.
 
Old December 21st, 2008, 12:22 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Actually, it's the same answer to handle O'Brien and avoid SQL Injection.

(1) Use a stored procedure and parameters. Fixes both problems.

(2) Use your existing code, but create the string for the SQL thus:
Code:
str = "select username,password from login_form " _
     & " where username='" & Replace(TextBox1.Text,"'","''") & "' " _
     & " and password = '" & Replace(TextBox2.Text,"'","''") & "'"
You just convert every apostrophe you find into a pair of apostrophes. With a simple query like that, you have fixed both problems.
 
Old December 21st, 2008, 04:08 AM
Authorized User
 
Join Date: Sep 2007
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Post thanks for your reply

Thanks allot for your reply and making thing much more clear..





Similar Threads
Thread Thread Starter Forum Replies Last Post
What's wrong with this code jack123 SQL Server 2000 4 July 11th, 2007 12:09 PM
What's wrong with this code? appleseed C++ Programming 2 November 25th, 2006 08:17 AM
What is wrong in this code? gajee ASP.NET 1.0 and 1.1 Basics 2 July 14th, 2006 06:41 AM
What is wrong with code? rtr1900 Classic ASP Databases 1 April 3rd, 2006 03:20 AM
Can anyone tell me what's wrong with this code? hobgoblin BOOK: Beginning ASP 3.0 0 March 3rd, 2005 01:47 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.