Wednesday, March 28, 2012

problems with sqlreader

Hi everyone,

I am writing a simple login application in asp 2.0. I have spent quite a little bit of time of this error so any help would be greatly appreciated.

Here is the code i am using.

Dim cnAs SqlConnection

Dim cmdAs SqlCommand

Dim sqlAsString ="select password from attendant where " & _

" ((username = @.username) and (password = @.password)"

cn =

New SqlConnection("Data Source=z-davis\sqlexpress;Initial Catalog=Trainingdb;Integrated Security=True")' cn = New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("TrainingdbConnectionString").ToString)

'I have tried both connection strings and get the same error.

cmd =New SqlCommand(sql, cn)

cmd.Parameters.Add(

"@.username", SqlDbType.VarChar, 50)

cmd.Parameters(

"@.username").Value = txtuser.Text

cmd.Parameters.Add(

"@.password", SqlDbType.VarChar, 50)

cmd.Parameters(

"@.password").Value = txtpass.Text

cn.Open()

Dim myreaderAs SqlDataReader

myreader = cmd.ExecuteReader(CommandBehavior.CloseConnection)

'The line above is where i get the error.

If myreader.Read()Then

FormsAuthentication.RedirectFromLoginPage(txtuser.Text,

False)Else

Response.Write(

"Try again")

After stepping through the code the error stops on this line:

myreader = cmd.executereader(commandbehavior.closeconnection)

The error that is generated is: Incorrect syntax near ')'

Any help would be greatly appreciated or any other code snippets

Thanks,

notrosh

In this line you have 3 open parentheses and only 2 close parentheses.

Dim sqlAsString ="select password from attendant where " & _

" ((username = @.username) and (password = @.password)"


Remove the first open parenthesis and you will have better luck.

|||

Thanks,

Sometimes you need an outside eye to see an error that simple.

zach davis

|||Or you need to have made that same sort of error a few dozen timesWink [;)]

I've learrned to count my parentheses using my fingers. Working left to right in my SQL statement, I put up a finger each time I open one, and put down a finger each time I close one. When I get to the last character, if I have any fingers sticking up, I know I have a problem. Pretty silly approach, but it works for me.

No comments:

Post a Comment