Showing posts with label sql2005. Show all posts
Showing posts with label sql2005. Show all posts

Friday, March 30, 2012

Problem with sql2005 query/storedproc

I am working on the login portion of my app and am using my own setup for the moment so that I can learn more about how things work. I have 1 user setup in the db and am using a stored procedure to do the checking for me, here is the stored procedure code:

ALTER PROCEDUREdbo.MemberLogin(@.MemberNamenchar(20),

@.MemberPasswordnchar(15),

@.BoolLoginbit OUTPUT

)

AS

selectMemberPasswordfrommemberswheremembername = @.MemberNameandmemberpassword = @.MemberPassword

if@.@.Rowcount = 0

begin

selectBoolLogin = 0

return

end

selectBoolLogin=1

/* SET NOCOUNT ON */

RETURN

When I run my app, I continue to get login failed but no error messages. Can anybody help? Here is my vb code:

Dim MemberNameAsString

Dim MemberPasswordAsString

Dim BoolLoginAsBoolean

Dim DBConnectionAsNew Data.SqlClient.SqlConnection(MyCONNECTIONSTRING)

Dim SelectMembersAsNew Data.SqlClient.SqlCommand("MemberLogin", DBConnection)

SelectMembers.CommandType = Data.CommandType.StoredProcedure

MemberName = txtLogin.Text

MemberPassword = txtPassword.Text

Dim SelectMembersParameterAs Data.SqlClient.SqlParameter = SelectMembers.CreateParameter

'Name

SelectMembersParameter.ParameterName ="@.MemberName"

SelectMembersParameter.Value = MemberName

SelectMembers.Parameters.Add(SelectMembersParameter)

'Password

Dim SelectPasswordParameterAs Data.SqlClient.SqlParameter = SelectMembers.CreateParameter

SelectPasswordParameter.ParameterName ="@.MemberPassword"

SelectPasswordParameter.Value = MemberPassword

SelectMembers.Parameters.Add(SelectPasswordParameter)

Dim SelectReturnParameterAs Data.SqlClient.SqlParameter = SelectMembers.CreateParameter

SelectReturnParameter.ParameterName ="@.BoolLogin"

SelectReturnParameter.Value = BoolLogin

SelectReturnParameter.Direction = Data.ParameterDirection.Output

SelectMembers.Parameters.Add(SelectReturnParameter)

If BoolLogin =FalseThen

MsgBox("Login Failed")

ElseIf BoolLogin =TrueThen

MsgBox("Login Successful")

EndIf

EndSub

Thank you!!!

Perhaps its because of the nchar's you are using. CHAR is used for a fixed width string so if you send in a string which is less than the specified length it will be padded with extra spaces at the end. I would modify your code as follows:

ALTER PROCEDURE dbo.MemberLogin(@.MemberNamenvarchar(20),@.MemberPasswordnvarchar(15),@.BoolLoginbit OUTPUT)ASBEGINSET NOCOUNT ON-- @.BoolLogin =0 ==> Does not exist, @.BoolLogin=1 ==> ExistsSET @.BoolLogin =0IFEXISTS(select MemberPasswordfrom memberswhere membername = @.MemberNameand memberpassword = @.MemberPassword)SET @.BoolLogin = 1SET NOCOUNT OFFEND
|||

Hello all, I am still having problems and am very frustrated. I have looked and ready over a dozen articles on ado/asp/sql and cannot seem to figure this out. I have a sql db I added using the add components part of asp. It is local, IIS is active. As far as i can tell servername is 'local'. This is the code I am running:

visual basic code:
Imports System.DataImports System.Data.SqlClientPartialClass _DefaultInherits System.Web.UI.PagePrivateConst MyCONNECTIONSTRINGAsString = "Server=(local);Database=Vex;Trusted_Connection=True"ProtectedSub btnLogin_Click(ByVal senderAsObject,ByVal eAs System.EventArgs)Handles btnLogin.ClickDim MemberNameAsStringDim MemberPasswordAsStringDim BoolLoginAsBooleanDim testAsStringDim DBConnectionAsNew Data.SqlClient.SqlConnection(MyCONNECTIONSTRING)Dim SelectMembersAsNew Data.SqlClient.SqlCommand("MemberLogin", DBConnection) SelectMembers.CommandType = Data.CommandType.StoredProcedure 'open the connection to the db DBConnection.Open() MemberName = txtLogin.Text MemberPassword = txtPassword.Text 'NameDim SelectMembersParameterAs Data.SqlClient.SqlParameter = SelectMembers.CreateParameter SelectMembersParameter.ParameterName = "@.MemberName" SelectMembersParameter.Value = MemberName SelectMembers.Parameters.Add(SelectMembersParameter) 'PasswordDim SelectPasswordParameterAs Data.SqlClient.SqlParameter = SelectMembers.CreateParameter SelectPasswordParameter.ParameterName = "@.MemberPassword" SelectPasswordParameter.Value = MemberPassword SelectMembers.Parameters.Add(SelectPasswordParameter) 'Pass or Fail VariableDim SelectReturnParameterAs Data.SqlClient.SqlParameter = SelectMembers.CreateParameter SelectReturnParameter.ParameterName = "@.BoolLogin" SelectReturnParameter.Value = BoolLogin SelectReturnParameter.Direction = Data.ParameterDirection.Output SelectMembers.Parameters.Add(SelectReturnParameter) test = SelectMembers.ExecuteScalar()EndSubEndClass

I get this error when i try to login on the .open line:

An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

If i try to execute the .scalar w/o the line it tells me i need an open connection, but when I try to open the connection it throws this error. What am I doing wrong? Is there some setup piece(s) I am missing? I have gone through a basic install of vs2005 with no settings changes to sql05. Any help is greatly appreciated as I am at my wits end with this and I know it is going to be something simple......

as an added note, here is the connection string in the web.config file:

visual basic code:
<connectionStrings> <add name="csVex" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Vex.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient" /> </connectionStrings>
I went into sql2005 surface configuration and made sure all protocols are enabled. I am an admin locally on my machine. I do not know what else to check or do at this point.

Thanks for your help....

|||

Just in case anybody else runs into this. I created a new project and added a datasource to that project pointing to my sql 2005 database. I then copied the connectionstring from that connection and pasted it in my CONST connectionstring. Error went away.

Good Luck!!

Wednesday, March 21, 2012

Problem with simple subquery in SQL2005 AND SQL2000.

When I use the simple query with a subquery shown below, this is the error message I get in SQL 2000 AND SQL 2005

"Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression."

And here is the query I use:

SELECT docSections.SectionID,

(SELECT docSectionText.colText FROM docSectionText

WHERE (docSections.SectionID = docSectionText.SectionID)

AND (docSectionText.colOrdinal = 1)) AS SecTitle

FROM docSections

Can anyone please let me know what I do wrong here.

Thanks

Gerhard

I can tell you why you get the error. But, without understanding your schema and requirements, I can not give you a solution for what you are trying to do.

The problem is that when you have a subquery in your SELECT it can only return 1 row per row. So, your subquery must be returning multiple rows.

To check try the following queries and see what it returns.

-- This should show you the sectionid that have multiple rows with colOrdinal = 1
SELECT SECTIONID, count(coltext) as rowcount
FROM docSections
WHERE docSectionText.colOrdinal = 1
GROUP BY SectionID
HAVING count(coltext) > 1

-- This should checks if perhaps the identified sections have multiple rows
--but all with same coltext. If first returns rows, but this doesn't,
--then you can add distinct to solve your problems
SELECT SECTIONID, count(distinct coltext) as rowcount
FROM docSections
WHERE docSectionText.colOrdinal = 1
GROUP BY SectionID
HAVING count(distinct coltext) > 1

HTH

|||

Thank You very much.

You were correct. I had 2 doubles in my table.

Gerhard