473,395 Members | 1,537 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

ExecuteScalar?

A class file has the following code:

Imports System
Imports System.Data
Imports System.Data.SqlClient

Namespace LoginUsers
Public Class UserValidation
Public Function Validate(ByVal UserName As String, ByVal
Password As String) As Integer
Dim iID As Integer
Dim sqlCmd As SqlCommand
Dim sqlConn As SqlConnection

sqlConn = New SqlConnection(".......")
sqlCmd = New SqlCommand("LoginUser", sqlConn)
sqlCmd.CommandType = CommandType.StoredProcedure

With sqlCmd
.Parameters.Add("@UserID", SqlDbType.Int, 4).Direction
= ParameterDirection.ReturnValue
.Parameters.Add("@UserName", SqlDbType.VarChar,
50).Value = UserName
.Parameters.Add("@Password", SqlDbType.VarChar,
50).Value = Password
End With

sqlConn.Open()
iID = CType(sqlCmd.ExecuteScalar, Integer)
sqlConn.Close()

Return iID
End Function
End Class
End Namespace

Using vbc, I compiled the above into a DLL named LoginUsers.dll. This
is the simple stored procedure:

ALTER PROCEDURE dbo.NETLoginUser
@UserName varchar(50),
@Password varchar(50)
AS
DECLARE
@ID int

IF EXISTS(SELECT UserID FROM Users WHERE UserName = @UserName AND
Password = @Password)
BEGIN
SET @ID = (SELECT UserID FROM Users WHERE UserName = @UserName
AND Password = @Password)
END
ELSE
BEGIN
SET @ID = 0
END
RETURN @ID

Finally this is the ASPX page:

<%@ Import Namespace="LoginUsers" %>

<script runat="server">
Sub LoginUser(ByVal obj As Object, ByVal ea As EventArgs)
Dim boUserValidation As UserValidation
Dim iUID As Integer

boUserValidation = New UserValidation

iUID = boUserValidation.Validate(txtUserName.Text,
txtPassword.Text)
Response.Write(iUID)
</script>
<form runat="server">
<asp:TextBox ID="txtUserName" runat="server"/>
<asp:TextBox ID="txtPassword" TextMode="password" runat="server"/>
<asp:Button ID="btnSubmit" OnClick="LoginUser" runat="server"/>
</form>

Asuume that one of the records in the DB table named Users has the
UserName & Password value as "ron" & "nor" respectively (both without
the quotes). Assume that the UserID of this user is 10.

When I run the above ASPX page & enter the UserName & Password as "ron"
& "nor", then the Response.Write(iUID) says 0 where as it should be 10.
Why?

If I replace ExecuteScalar in the class file (the first code snippet)
with this:

sqlCmd.ExecuteNonQuery()
iID = CInt(sqlCmd.Parameters(0).Value)

then Response.Write(iUID) correctly shows the ID value as 10 but using
ExecuteScalar shows the ID value as 0! Why so?

Sep 24 '06 #1
1 2461
Hi,
The value is not returned in the ExecueteScalar function because you are not
selecting the @ID in the stored procedure, so you have to change the last
statement in the stored procedure to select the @ID instead of returing it,
as follows
ALTER PROCEDURE dbo.NETLoginUser
@UserName varchar(50),
@Password varchar(50)
AS
DECLARE
@ID int

IF EXISTS(SELECT UserID FROM Users WHERE UserName = @UserName AND
Password = @Password)
BEGIN
SET @ID = (SELECT UserID FROM Users WHERE UserName = @UserName
AND Password = @Password)
END
ELSE
BEGIN
SET @ID = 0
END
Select @ID
<rn**@rediffmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
>A class file has the following code:

Imports System
Imports System.Data
Imports System.Data.SqlClient

Namespace LoginUsers
Public Class UserValidation
Public Function Validate(ByVal UserName As String, ByVal
Password As String) As Integer
Dim iID As Integer
Dim sqlCmd As SqlCommand
Dim sqlConn As SqlConnection

sqlConn = New SqlConnection(".......")
sqlCmd = New SqlCommand("LoginUser", sqlConn)
sqlCmd.CommandType = CommandType.StoredProcedure

With sqlCmd
.Parameters.Add("@UserID", SqlDbType.Int, 4).Direction
= ParameterDirection.ReturnValue
.Parameters.Add("@UserName", SqlDbType.VarChar,
50).Value = UserName
.Parameters.Add("@Password", SqlDbType.VarChar,
50).Value = Password
End With

sqlConn.Open()
iID = CType(sqlCmd.ExecuteScalar, Integer)
sqlConn.Close()

Return iID
End Function
End Class
End Namespace

Using vbc, I compiled the above into a DLL named LoginUsers.dll. This
is the simple stored procedure:

ALTER PROCEDURE dbo.NETLoginUser
@UserName varchar(50),
@Password varchar(50)
AS
DECLARE
@ID int

IF EXISTS(SELECT UserID FROM Users WHERE UserName = @UserName AND
Password = @Password)
BEGIN
SET @ID = (SELECT UserID FROM Users WHERE UserName = @UserName
AND Password = @Password)
END
ELSE
BEGIN
SET @ID = 0
END
RETURN @ID

Finally this is the ASPX page:

<%@ Import Namespace="LoginUsers" %>

<script runat="server">
Sub LoginUser(ByVal obj As Object, ByVal ea As EventArgs)
Dim boUserValidation As UserValidation
Dim iUID As Integer

boUserValidation = New UserValidation

iUID = boUserValidation.Validate(txtUserName.Text,
txtPassword.Text)
Response.Write(iUID)
</script>
<form runat="server">
<asp:TextBox ID="txtUserName" runat="server"/>
<asp:TextBox ID="txtPassword" TextMode="password" runat="server"/>
<asp:Button ID="btnSubmit" OnClick="LoginUser" runat="server"/>
</form>

Asuume that one of the records in the DB table named Users has the
UserName & Password value as "ron" & "nor" respectively (both without
the quotes). Assume that the UserID of this user is 10.

When I run the above ASPX page & enter the UserName & Password as "ron"
& "nor", then the Response.Write(iUID) says 0 where as it should be 10.
Why?

If I replace ExecuteScalar in the class file (the first code snippet)
with this:

sqlCmd.ExecuteNonQuery()
iID = CInt(sqlCmd.Parameters(0).Value)

then Response.Write(iUID) correctly shows the ID value as 10 but using
ExecuteScalar shows the ID value as 0! Why so?

Sep 24 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: cmo63126 | last post by:
Why am I not able to retrieve the value of ID (which does exist) using ExecuteScalar()? Do i simply have to use ExecuteReader() instead? My problem snippet: string mySQL = "SELECT ID FROM...
2
by: WeiminZhang | last post by:
When I use the OleDb to connect a Oracle db, and use the ExecutScalar() method to get the count of a table, the return value can't be cast to a data type, say int, while this works fine for a SQL...
3
by: AL | last post by:
Hello. I've been programming in C# for about 6 months, and I have a question; I was experimenting with some code this evening, for example this; this.sqlConnection1.Open(); int qty = (int)...
2
by: Dinesh | last post by:
Hi, I have one stored procedure in SQL server in which i have written one insert statement. Now in my cs file i pass the parameters require to execute that stored procedure and finaly by mistaken...
2
by: trialproduct2004 | last post by:
hi all i am having application which is connecting to databset and executing one query as below:- string str; str="select count(*) from table1; and then i am using sqlcommand to get result...
6
by: Max | last post by:
Anyone know why I'm always getting 0 returned? My stored procedure returns -1. Dim iErrorCode As Int32 iErrorCode = Convert.ToInt32(SqlHelper.ExecuteScalar(AppVars.strConn, _ "gpUpdateMember",...
7
by: Neven Klofutar | last post by:
Hi, I have a problem with SqlHelper.ExecuteScalar ... When I try to execute SqlHelper.ExecuteScalar I get this message: "System.InvalidCastException: Object must implement IConvertible.". ...
5
by: bienwell | last post by:
Hi all, I have a problem with using myCommand.ExecuteScalar(). My question is : If the Web setup is incorrect, does it make command ExecuteScalar() work improperly ?? In my program, I was...
3
by: charliewest | last post by:
Hi - I need to detect when the ExecuteScalar() method of the cmd object returns NULL. I have tried the below code, however, it always returns false (this is to say, that ExecuteScalar never...
2
by: Manikandan | last post by:
Hi, I have a table with following data Tablename:details No(varchar) Name(varchar) Updated(Datetime) 1 mm 10/10/2006 2 nn 02/12/2005 3 kk NULL I'm using executescalar to get the...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.