Problems With Sqlreader

May 5, 2006

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 cn As SqlConnection

Dim cmd As SqlCommand

Dim sql As String = "select password from attendant where " & _

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

cn = New SqlConnection("Data Source=z-davissqlexpress;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 myreader As 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

View 3 Replies


ADVERTISEMENT

Going From Sqlconnection And Sqlreader To Using A Tableadapter And ????????

Apr 13, 2007

Below is my code right now Im using a direct sqlconnection for each request. I would like to use Tableadapters and/or my BLL code like the rest of my sites pages. I cant find how to do this progamatically and using the xmltextwriter. (Also i need to be able to name the xml output nodes like below)Thanks for the help.NeilPrivate Sub GetAllEvents(ByVal SqlString)    Response.Clear()    Response.ContentType = "text/xml"    Dim objX As New XmlTextWriter(Response.OutputStream, Encoding.UTF8)    objX.WriteStartDocument()    objX.WriteStartElement("Events")    Dim objConnection As New SqlConnection("Data Source=.SQLEXPRESS;AttachDbFilename=|DataDirectory|*********.mdf;Integrated Security=True;User Instance=True")    objConnection.Open()    Dim sql As String = SqlString    Dim objCommand As New SqlCommand(sql, objConnection)    Dim objReader As SqlDataReader = objCommand.ExecuteReader()    While objReader.Read()      objX.WriteStartElement("Event")      objX.WriteAttributeString("EventId", objReader.GetInt32(0))      objX.WriteAttributeString("EventName", objReader.GetString(1))      objX.WriteAttributeString("EventDescription", objReader.GetString(2))      objX.WriteAttributeString("EventDate", objReader.GetDateTime(3))      objX.WriteAttributeString("CurrentDate", Date.Now.ToString)      If Not objReader.IsDBNull(12) Then        objX.WriteAttributeString("EventImage", objReader.GetString(12))      End If      objX.WriteEndElement()    End While    objReader.Close()    objConnection.Close()    objX.WriteEndElement()    objX.WriteEndDocument()    objX.Flush()    objX.Close()    Response.End()  End Sub

View 2 Replies View Related

Populating An Array From A Sqlreader

Jun 8, 2006

I am trying to populate an array from a sqlreader.  I am getting the error "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index".  Can anyone help?  I have my code below.
        Try            sql = "...."            cmd = New SqlCommand(sql, conn)            SqlReader = cmd.ExecuteReader       'Get the data from SQL Server            Dim Counter As Integer = 0                        Do While SqlReader.Read()                    Counter += 1                    PageArray(Counter) = SqlReader("WebPageID")           Loop                    Catch ex As Exception            lblMessage.Text = ex.Message        End Try

View 5 Replies View Related

Sqlreader.close() And Sqlconn.close()

Feb 13, 2008

Hi,
My question is if I close the sqlreader i am using. will that close the connection it uses or the connection will still remain open?
Syed

View 4 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved