Showing posts with label sqlconnectiondim. Show all posts
Showing posts with label sqlconnectiondim. Show all posts

Sunday, February 19, 2012

Filtering SQL statement by server variable

Hi

Hope somebody can help me with a problem that i am having. I have the following piece of code

<%

Dim sqlConnAs SqlConnectionDim sqlCmdAs SqlCommandDim sqlReaderAs SqlDataReader

sqlConn =

New SqlConnection("server=myserver;database=aspnet;uid=username;pwd=password")

sqlCmd =

New SqlCommand("SELECT SamAccount, FirstName, Surname, FirstName + N' ' + Surname+' Logged In ' AS UserLoggedIn FROM tblUsers WHERE (SamAccount = " + Request.ServerVariables("logon_user") +")", sqlConn)

sqlConn.Open()

sqlReader = sqlCmd.ExecuteReader()

%>

<%

Dim strPCNAmeAsString

sqlReader.Read()

strPCNAme = sqlReader(

"UserLoggedIn")

Response.Write(strPCNAme)

%>

The bit i have i have underlined and made bold throws up the error. Basically what i am trying to do is to filter the the SQL statement by the server variable logon user. Everthing works fine until the point that i put the where clase in.

Can anybody help please?

Thanks

Marcus

put your loginname in single quotes for query maybe it will help :

New SqlCommand("SELECT SamAccount, FirstName, Surname, FirstName + N' ' + Surname+' Logged In ' AS UserLoggedIn FROM tblUsers WHERE (SamAccount ='" + Request.ServerVariables("logon_user") +"')", sqlConn)

|||Thanks for that managed to get it working now