Showing posts with label procedures. Show all posts
Showing posts with label procedures. Show all posts

Friday, March 30, 2012

Return Distinct Values from Stored Procedures

I need to somehow filter the results of my stored procedure to return the distinct "OrderNum" values. I'm using SQL database and I'm look for a way to alter the results from the stored procedure. My stored procedure rptOpenOrderLines currently returns all invoices (items under a OrderNum). I want to somehow filter those results to return one and only one of those "OrderNum" variables from the invoices. The tricky part is that I need to somehow find a way to do this without going into my database and directly altering the SQL stored procedure. I would be happy for any recommendations/ideas. Thanks!

Is OrderNum contained in the result set returned by the sp rptOpenOrderLines? If yes, you may need something likeDataView.RowFilter:

http://msdn2.microsoft.com/en-us/library/system.data.dataview.rowfilter.aspx

If no, there seems to be no way to do this without checking back in database.

|||Great! Thanks lori_Jay! Although I don't yet have the exact solution, this looks like a good lead toward the final product. That article is pretty useful and I appreciate the help. I've spent days on developing these datalist/gridview controls and its about time that it should be done. I'll post back with the solution when I'm complete with this part.|||

Please See:

http://forums.asp.net/thread/1371660.aspx

Monday, March 26, 2012

Retrning XML raw from Stored procedure - (by using dual table)

Hi ,

I have a problem returning the @.CustomerNo from dual for one of my stored procedures. (by the way I do a some manipulations and assign a value to @.CustomerNo everytime this stored procedure runs)

SELECT @.CustomerNo AS CustomerNumber from dual FOR XML RAW

What I figured out is that the reason no values is returned in my XML file is because the dual table does not have any rows. But ,the dual table always have a dummy row by default. Doesnt it.

Alsoif possible please let me know any alternative way to returnthe following XML :

<row>

<CustomerNumber >

value of @.CustomerNo


<CustomerNumber >

</row>

Thanks in advance.

The dual table is an Oracle (shudders) feature.

If you are using SQL Server 2005, you can do the following:

Code Snippet

SELECT @.CustomerNo AS CustomerNumber

FOR XML PATH('row')

--

Peter DeBetta

MVP - SQL Server

http://sqlblog.com

|||Super.. Thanks for the soln.sql

Retrning XML raw from Stored procedure - (by using dual table)

Hi ,

I have a problem returning the @.CustomerNo from dual for one of my stored procedures. (by the way I do a some manipulations and assign a value to @.CustomerNo everytime this stored procedure runs)

SELECT @.CustomerNo AS CustomerNumber from dual FOR XML RAW

What I figured out is that the reason no values is returned in my XML file is because the dual table does not have any rows. But ,the dual table always have a dummy row by default. Doesnt it.

Alsoif possible please let me know any alternative way to returnthe following XML :

<row>

<CustomerNumber >

value of @.CustomerNo


<CustomerNumber >

</row>

Thanks in advance.

The dual table is an Oracle (shudders) feature.

If you are using SQL Server 2005, you can do the following:

Code Snippet

SELECT @.CustomerNo AS CustomerNumber

FOR XML PATH('row')

--

Peter DeBetta

MVP - SQL Server

http://sqlblog.com

|||Super.. Thanks for the soln.

Wednesday, March 21, 2012

Retrieving Scalar or Calculated Values from Stored Procedures with C#

I am trying to build an Sql page hit provider. I am having trouble getting a count back from the database. If I use ExecuteScalar it doesn't see any value in the returned R1C1. If I use ExecuteNonQuery with a @.ReturnValue, the return value parameter value is always zero. Ideally I would like to use a dynamic stored proceudre if there are any suggestions for using them with C#. My table has rvPathName, userName and a date. I have the AddWebPageHit method working so I know data connection and sql support code in provider is working. I think the problem is either in how I am writing the stored procedures or how I am trying to retrieve the data in C#. Any help with this will be greatly appreciated.

We're not going to be able to help without seeing the code, both the C# code and the stored procedure code.|||

Here you go. I worked on this for more than 8 hours so know that you help is greatly appreciated!!!

CREATE PROCEDURE dbo.WebPageHits_CountByWebPageVPathName @.webPageVPathName nvarchar(256)

AS

DECLARE @.count int

SELECT @.count = COUNT(WebPageHitId)

FROM dbo.WebPageHits

WHERE WebPageVPathName = @.webPageVPathName

RETURN (@.count)

GO

publicoverrideint GetWebPageHitCount(string webPageVPathName)

{

SecUtility.CheckParameter(ref webPageVPathName,true,false,true, 256,"webPageVPathName");

SqlConnectionHolder connectionHolder =null;

SqlConnection connection =null;

int webPageHitCount = 0;

try

{

try

{

connectionHolder =SqlConnectionHelper.GetConnection(_sqlConnectionString,true);

connection = connectionHolder.Connection;

CheckSchemaVersion(connectionHolder.Connection);

SqlCommand cmd =newSqlCommand("dbo.WebPageHits_CountByWebPageVPathName", connection);

cmd.CommandType =CommandType.StoredProcedure;

cmd.CommandTimeout = CommandTimeout;

SqlParameter p =newSqlParameter("@.ReturnValue",SqlDbType.Int);

p.Direction =ParameterDirection.ReturnValue;

cmd.Parameters.Add(p);

cmd.Parameters.Add(CreateInputParam("@.webPageVPathName",SqlDbType.VarChar, webPageVPathName));

cmd.ExecuteNonQuery();

webPageHitCount = GetReturnValue(cmd);

}

finally

{

if (connectionHolder !=null)

{

connectionHolder.Close();

connectionHolder =null;

}

}

}

catch

{

throw;

}

return webPageHitCount;

}

|||

It's a little tough, as you are using custom methods such as GetReturnValue and CreateInputParam.

My gut instinct is that you are having a problem because youare both using the wrong data type for the @.webPageVPathName as well asnot specifying the length. The lack of length specification is mostlikely causing the trouble; if you don't specify it then a length of 1is used. Use the nvarchar datatype and a length of 256 in your C#code, and I think you will have better luck.

I will tell you that it's a better practice to use Output parameters to return values from a stored procedure rather than a ReturnValue. Return values are typically used to communicate success or failure; using them in the manner you are attempting just because the value you want to communicate back to the calling code happens to be an integer can be seen as "cheating". Use an Output parameter instead.

|||

You are right about my cheating. Sometimes you have to hear things from somebody else to actually realize it even though it's right in front of your face.

I got this to work by following the aspnet profile provider code. The provider uses this stored procedure.

aspnet_Profile_GetCountOf...

If just does SELECT COUNT(*) FROM ... with no RETURN statement. Then the C# uses ExecuteScalar then dim o as object = cmd then if o <> null return cmd. My function returns an int so somehow the value gets from the cmd object to the function's return value. I don't know but it's easier.

|||

mtsonic:

If just does SELECT COUNT(*) FROM ... with no RETURN statement. Then the C# uses ExecuteScalar then dim o as object = cmd then if o <> null return cmd. My function returns an int so somehow the value gets from the cmd object to the function's return value. I don't know but it's easier.

ExecuteScalar is an OK way to accomplish what you need. Glad you got it working :-)

sql

Monday, March 12, 2012

Retrieving Default Values for Parameters in a Stored Procedure.

I'm generating a list of parameters needed by stored procedures, andI'd like to know which ones have default values assigned to them.
To retrieve the parameter information I use:

sp_sproc_columns @.Procedure_Name='InsertUser''

However, the column that is supposed to give the default value,'COLUMN_DEF' always returns as NULL, even when that column has adefault value assigned to it.
i.e.

CREATE PROCEDURE InsertUser
@.UserID INT = 10,
....

And then if I do a sp_sproc_columns @.Procedure_Name='InsertUser'', the COLUMN_DEF value for the @.UserID column is still NULL.
Does anyone know what I'm doing wrong and how I can retrieve the default value?
Thanks

It looks like you will have to parse the stored procedure code to get the default parameter info:

http://www.aspfaq.com/show.asp?id=2463
Hope this helps.
Chris R. Timmons
--------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/

Wednesday, March 7, 2012

Retrieve Stored Procedure Code

Hi,
I am trying to retrieve the code for some stored procedures. I was able to
select the routine definition from the information_schema.routines for the
procedures that had less than 4000 characters, but I can't get the larger
procedures. Does anyone know how to pull the entire stored procedure if it
has more than 4000 characters?
ThanksChris F wrote:
> Hi,
> I am trying to retrieve the code for some stored procedures. I was
> able to select the routine definition from the
> information_schema.routines for the procedures that had less than
> 4000 characters, but I can't get the larger procedures. Does anyone
> know how to pull the entire stored procedure if it has more than 4000
> characters?
> Thanks
That system table syscomments contains multiple rows that you can pull
back. Or you can simply use sp_helptext (or examine its code using
sp_helptext to see how it pulls the data back).
David Gugick
Quest Software
www.imceda.com
www.quest.com|||You can use sp_helptext or you can pull it from syscomments system table.
Example:
use northwind
go
declare @.s1 varchar(8000)
declare @.s2 varchar(8000)
set @.s1 = 'create procedure dbo.p1 as select '''
set @.s2 = replicate('a', 7999) + ''''
exec(@.s1 + @.s2)
go
select
*
from
syscomments
where
[id] = object_id('[dbo].[p1]')
order by
number, colid
go
exec sp_helptext '[dbo].[p1]'
go
drop procedure dbo.p1
go
AMB
"Chris F" wrote:

> Hi,
> I am trying to retrieve the code for some stored procedures. I was able t
o
> select the routine definition from the information_schema.routines for the
> procedures that had less than 4000 characters, but I can't get the larger
> procedures. Does anyone know how to pull the entire stored procedure if i
t
> has more than 4000 characters?
> Thanks|||Thanks. I appreciate the help.
"David Gugick" wrote:

> Chris F wrote:
> That system table syscomments contains multiple rows that you can pull
> back. Or you can simply use sp_helptext (or examine its code using
> sp_helptext to see how it pulls the data back).
> --
> David Gugick
> Quest Software
> www.imceda.com
> www.quest.com
>

retrieve result set generated by a stored procedure

I want to call other stored procedures in my stored procedure.
However, when I tried to call a stored procedure which return a result set rather than a single record, I don't know how to catch it in my stored procedures?

Are there some methods to catch up a result set returned by a stored procedure in another stored procedure?

Thanks!

Thomasinsert the resultset of the stored procedure into a table

create table
#tbl(spid int
, ecid int
, status varchar(15)
, loginame varchar(20)
, hostname varchar(15)
, blk int
, dbname varchar(15)
, cmd varchar(25))

insert into #tbl exec sp_who

select * From #tbl

Saturday, February 25, 2012

Retrieve list of largest tables in a database

Hello,
is there an easy way to retrieve a list of the largest tables in a
given database? I know how to use stored procedures to retrieve the
size of a single table, but I was wondering if there was a query that
could be used to obtain a list of the 20 (for instance) top largest
tables. I know this can be coded on the client side in the application
by retrieving the size of all the tables in a database and then
selecting only the largest n tables, but I was looking for something
more efficient and just a query if possible.
Thanks in advance.
If you don't mind missing out on 100% accuracy,
select top 20 object_name(id),rowcnt
from sysindexes
where indid in (0,1)
order by rowcnt desc
<ozonefilter@.gmail.com> wrote in message
news:1166050186.506361.21040@.80g2000cwy.googlegrou ps.com...
> Hello,
> is there an easy way to retrieve a list of the largest tables in a
> given database? I know how to use stored procedures to retrieve the
> size of a single table, but I was wondering if there was a query that
> could be used to obtain a list of the 20 (for instance) top largest
> tables. I know this can be coded on the client side in the application
> by retrieving the size of all the tables in a database and then
> selecting only the largest n tables, but I was looking for something
> more efficient and just a query if possible.
> Thanks in advance.
>
|||Aaron Bertrand [SQL Server MVP] wrote:
> If you don't mind missing out on 100% accuracy,
> select top 20 object_name(id),rowcnt
> from sysindexes
> where indid in (0,1)
> order by rowcnt desc
Hi Aaron,
thank you for your answer. If I'm not mistaken, the query above would
only provide me with the list of the tables having the greatest
cardinality. While I do not need 100% accuracy, very large tables (in
bytes) may have a relatively small amount of rows, while smaller tables
may have many tiny records. In fact I've tested it against a real
database and only 40% of the top 20 tables seems to be reported. Any
idea how to get a better approximation?
Thanks.
|||> thank you for your answer. If I'm not mistaken, the query above would
> only provide me with the list of the tables having the greatest
> cardinality.
Maybe I'm not sure what you mean by "cardinality" because the query I
provided has nothing to do with cardinality.
I may also be confused by what you mean by "largest". The query I provided
gives you the top 20 tables in terms of # of rows. If you want those that
are occupying the largest amount of space, maybe try this:
create table #foo
(
[name] sysname,
rows INT,
reserved nvarchar(32),
data nvarchar(32),
index_size nvarchar(32),
unused nvarchar(32)
);
set nocount on;
exec sp_msForEachTable 'INSERT #foo EXEC sp_spaceused ''?'';';
select top 20 *
from #foo
order by
convert(int, replace(reserved, N' KB', '')) desc;
drop table #foo;
|||Take a look at
http://vyaskn.tripod.com/sp_show_biggest_tables.htm
<ozonefilter@.gmail.com> wrote in message
news:1166050186.506361.21040@.80g2000cwy.googlegrou ps.com...
> Hello,
> is there an easy way to retrieve a list of the largest tables in a
> given database? I know how to use stored procedures to retrieve the
> size of a single table, but I was wondering if there was a query that
> could be used to obtain a list of the 20 (for instance) top largest
> tables. I know this can be coded on the client side in the application
> by retrieving the size of all the tables in a database and then
> selecting only the largest n tables, but I was looking for something
> more efficient and just a query if possible.
> Thanks in advance.
>
|||ozonefilter@.gmail.com wrote:
> [...] a query that
> could be used to obtain a list of the 20 (for instance) top largest
> tables. [...]
SELECT TOP 20
[TableSchema] = info.table_schema,
[TableName] = info.table_name,
[Used] = si.used
FROM
sysindexes si,
INFORMATION_SCHEMA.TABLES info
WHERE
object_name(si.id) = info.table_name
AND
info.table_type = 'BASE TABLE'
ORDER BY
si.used DESC;
Cheers,
Antonio

Retrieve list of largest tables in a database

Hello,
is there an easy way to retrieve a list of the largest tables in a
given database? I know how to use stored procedures to retrieve the
size of a single table, but I was wondering if there was a query that
could be used to obtain a list of the 20 (for instance) top largest
tables. I know this can be coded on the client side in the application
by retrieving the size of all the tables in a database and then
selecting only the largest n tables, but I was looking for something
more efficient and just a query if possible.
Thanks in advance.If you don't mind missing out on 100% accuracy,
select top 20 object_name(id),rowcnt
from sysindexes
where indid in (0,1)
order by rowcnt desc
<ozonefilter@.gmail.com> wrote in message
news:1166050186.506361.21040@.80g2000cwy.googlegroups.com...
> Hello,
> is there an easy way to retrieve a list of the largest tables in a
> given database? I know how to use stored procedures to retrieve the
> size of a single table, but I was wondering if there was a query that
> could be used to obtain a list of the 20 (for instance) top largest
> tables. I know this can be coded on the client side in the application
> by retrieving the size of all the tables in a database and then
> selecting only the largest n tables, but I was looking for something
> more efficient and just a query if possible.
> Thanks in advance.
>|||Aaron Bertrand [SQL Server MVP] wrote:
> If you don't mind missing out on 100% accuracy,
> select top 20 object_name(id),rowcnt
> from sysindexes
> where indid in (0,1)
> order by rowcnt desc
Hi Aaron,
thank you for your answer. If I'm not mistaken, the query above would
only provide me with the list of the tables having the greatest
cardinality. While I do not need 100% accuracy, very large tables (in
bytes) may have a relatively small amount of rows, while smaller tables
may have many tiny records. In fact I've tested it against a real
database and only 40% of the top 20 tables seems to be reported. Any
idea how to get a better approximation?
Thanks.|||> thank you for your answer. If I'm not mistaken, the query above would
> only provide me with the list of the tables having the greatest
> cardinality.
Maybe I'm not sure what you mean by "cardinality" because the query I
provided has nothing to do with cardinality.
I may also be confused by what you mean by "largest". The query I provided
gives you the top 20 tables in terms of # of rows. If you want those that
are occupying the largest amount of space, maybe try this:
create table #foo
(
[name] sysname,
rows INT,
reserved nvarchar(32),
data nvarchar(32),
index_size nvarchar(32),
unused nvarchar(32)
);
set nocount on;
exec sp_msForEachTable 'INSERT #foo EXEC sp_spaceused ''?'';';
select top 20 *
from #foo
order by
convert(int, replace(reserved, N' KB', '')) desc;
drop table #foo;|||Take a look at
http://vyaskn.tripod.com/sp_show_biggest_tables.htm
<ozonefilter@.gmail.com> wrote in message
news:1166050186.506361.21040@.80g2000cwy.googlegroups.com...
> Hello,
> is there an easy way to retrieve a list of the largest tables in a
> given database? I know how to use stored procedures to retrieve the
> size of a single table, but I was wondering if there was a query that
> could be used to obtain a list of the 20 (for instance) top largest
> tables. I know this can be coded on the client side in the application
> by retrieving the size of all the tables in a database and then
> selecting only the largest n tables, but I was looking for something
> more efficient and just a query if possible.
> Thanks in advance.
>|||You can use the dpages, used or reserved columns instead of rowcnt, in the s
ysindexes table.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
<ozonefilter@.gmail.com> wrote in message
news:1166055677.196013.32640@.l12g2000cwl.googlegroups.com...
> Aaron Bertrand [SQL Server MVP] wrote:
> Hi Aaron,
> thank you for your answer. If I'm not mistaken, the query above would
> only provide me with the list of the tables having the greatest
> cardinality. While I do not need 100% accuracy, very large tables (in
> bytes) may have a relatively small amount of rows, while smaller tables
> may have many tiny records. In fact I've tested it against a real
> database and only 40% of the top 20 tables seems to be reported. Any
> idea how to get a better approximation?
> Thanks.
>|||ozonefilter@.gmail.com wrote:
> [...] a query that
> could be used to obtain a list of the 20 (for instance) top largest
> tables. [...]
SELECT TOP 20
[TableSchema] = info.table_schema,
[TableName] = info.table_name,
[Used] = si.used
FROM
sysindexes si,
INFORMATION_SCHEMA.TABLES info
WHERE
object_name(si.id) = info.table_name
AND
info.table_type = 'BASE TABLE'
ORDER BY
si.used DESC;
Cheers,
Antonio

Retrieve list of largest tables in a database

Hello,
is there an easy way to retrieve a list of the largest tables in a
given database? I know how to use stored procedures to retrieve the
size of a single table, but I was wondering if there was a query that
could be used to obtain a list of the 20 (for instance) top largest
tables. I know this can be coded on the client side in the application
by retrieving the size of all the tables in a database and then
selecting only the largest n tables, but I was looking for something
more efficient and just a query if possible.
Thanks in advance.If you don't mind missing out on 100% accuracy,
select top 20 object_name(id),rowcnt
from sysindexes
where indid in (0,1)
order by rowcnt desc
<ozonefilter@.gmail.com> wrote in message
news:1166050186.506361.21040@.80g2000cwy.googlegroups.com...
> Hello,
> is there an easy way to retrieve a list of the largest tables in a
> given database? I know how to use stored procedures to retrieve the
> size of a single table, but I was wondering if there was a query that
> could be used to obtain a list of the 20 (for instance) top largest
> tables. I know this can be coded on the client side in the application
> by retrieving the size of all the tables in a database and then
> selecting only the largest n tables, but I was looking for something
> more efficient and just a query if possible.
> Thanks in advance.
>|||Aaron Bertrand [SQL Server MVP] wrote:
> If you don't mind missing out on 100% accuracy,
> select top 20 object_name(id),rowcnt
> from sysindexes
> where indid in (0,1)
> order by rowcnt desc
Hi Aaron,
thank you for your answer. If I'm not mistaken, the query above would
only provide me with the list of the tables having the greatest
cardinality. While I do not need 100% accuracy, very large tables (in
bytes) may have a relatively small amount of rows, while smaller tables
may have many tiny records. In fact I've tested it against a real
database and only 40% of the top 20 tables seems to be reported. Any
idea how to get a better approximation?
Thanks.|||> thank you for your answer. If I'm not mistaken, the query above would
> only provide me with the list of the tables having the greatest
> cardinality.
Maybe I'm not sure what you mean by "cardinality" because the query I
provided has nothing to do with cardinality.
I may also be confused by what you mean by "largest". The query I provided
gives you the top 20 tables in terms of # of rows. If you want those that
are occupying the largest amount of space, maybe try this:
create table #foo
(
[name] sysname,
rows INT,
reserved nvarchar(32),
data nvarchar(32),
index_size nvarchar(32),
unused nvarchar(32)
);
set nocount on;
exec sp_msForEachTable 'INSERT #foo EXEC sp_spaceused ''?'';';
select top 20 *
from #foo
order by
convert(int, replace(reserved, N' KB', '')) desc;
drop table #foo;|||Take a look at
http://vyaskn.tripod.com/sp_show_biggest_tables.htm
<ozonefilter@.gmail.com> wrote in message
news:1166050186.506361.21040@.80g2000cwy.googlegroups.com...
> Hello,
> is there an easy way to retrieve a list of the largest tables in a
> given database? I know how to use stored procedures to retrieve the
> size of a single table, but I was wondering if there was a query that
> could be used to obtain a list of the 20 (for instance) top largest
> tables. I know this can be coded on the client side in the application
> by retrieving the size of all the tables in a database and then
> selecting only the largest n tables, but I was looking for something
> more efficient and just a query if possible.
> Thanks in advance.
>|||You can use the dpages, used or reserved columns instead of rowcnt, in the sysindexes table.
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
<ozonefilter@.gmail.com> wrote in message
news:1166055677.196013.32640@.l12g2000cwl.googlegroups.com...
> Aaron Bertrand [SQL Server MVP] wrote:
>> If you don't mind missing out on 100% accuracy,
>> select top 20 object_name(id),rowcnt
>> from sysindexes
>> where indid in (0,1)
>> order by rowcnt desc
> Hi Aaron,
> thank you for your answer. If I'm not mistaken, the query above would
> only provide me with the list of the tables having the greatest
> cardinality. While I do not need 100% accuracy, very large tables (in
> bytes) may have a relatively small amount of rows, while smaller tables
> may have many tiny records. In fact I've tested it against a real
> database and only 40% of the top 20 tables seems to be reported. Any
> idea how to get a better approximation?
> Thanks.
>|||ozonefilter@.gmail.com wrote:
> [...] a query that
> could be used to obtain a list of the 20 (for instance) top largest
> tables. [...]
SELECT TOP 20
[TableSchema] = info.table_schema,
[TableName] = info.table_name,
[Used] = si.used
FROM
sysindexes si,
INFORMATION_SCHEMA.TABLES info
WHERE
object_name(si.id) = info.table_name
AND
info.table_type = 'BASE TABLE'
ORDER BY
si.used DESC;
Cheers,
Antonio

Tuesday, February 21, 2012

retrieve data from a stored procedure

Hello Group
I am new to stored procedures and I have been fighting this for a while and I hope someone can help. In my sp it checks username and password and returns an integer value. I would like to retrieve some data from the same database about the user (the users first and last name and the password of the user). I can't retrieve the data. Here is my code.

CREATE PROCEDURE stpMyAuthentication
(
@.fldUsername varchar( 50 ),
@.fldPassword Char( 25 )--,
--@.fldFirstName char( 30 ) OUTPUT,
--@.fldLastName char( 30 ) OUTPUT
)
As
DECLARE @.actualPassword Char( 25 )
SELECT
@.actualPassword = fldPassword
FROM
tbMembUsers
Where
fldUsername = @.fldUsername
IF @.actualPassword IS NOT NULL
IF @.fldPassword = @.actualPassword
RETURN 1
ELSE
RETURN -2
ELSE
RETURN -1

SELECT
fldFirstName,
fldLastName,
fldPassword
FROM
tbMembUsers
Where
fldUsername = @.fldUsername
GO

############### login page ################

Sub Login_Click(ByVal s As Object, ByVal e As EventArgs)
If IsValid Then
If MyAuthentication(Trim(txtuserID.Text), Trim(txtpaswrd.Text)) > 0 Then
FormsAuthentication.RedirectFromLoginPage(Trim(txtuserID.Text), False)
End If
End If
End Sub

Function MyAuthentication(ByVal strUsername As String, ByVal strPassword As String) As Integer
Dim myConn As SqlConnection
Dim myCmd As SqlCommand
Dim myReturn As SqlParameter
Dim intResult As Integer
Dim sqlConn As String
Dim strFirstName, strLastName As String

sqlConn = ConfigurationSettings.AppSettings("sqlConnStr")
myConn = New SqlConnection(sqlConn)

myCmd = New SqlCommand("stpMyAuthentication", myConn)
myCmd.CommandType = CommandType.StoredProcedure

myReturn = myCmd.Parameters.Add("RETURN_VALUE", SqlDbType.Int)
myReturn.Direction = ParameterDirection.ReturnValue

myCmd.Parameters.Add(Trim("@.fldUsername"), Trim(strUsername))
myCmd.Parameters.Add(Trim("@.fldPassword"), Trim(strPassword))
myCmd.Parameters.Add("@.fldFirstName", strFirstName)
myCmd.Parameters.Add("@.fldLastName", strLastName)
myCmd.Parameters.Add("@.fldPassword", strPassword)

myConn.Open()
myCmd.ExecuteNonQuery()
intResult = myCmd.Parameters("RETURN_VALUE").Value

myConn.Close()

'If strPassword = 55555555 Then
' Session("intDefaultPass") = 1
'End If

Session("strFullName") = strFirstName & " " & strLastName
Session("strPassword") = strPassword

If intResult < 0 Then
If intResult = -1 Then
lblMessage.Text = "Username Not Registered!<br><br>"
Else
lblMessage.Text = "Invalid Password!<br><br>"
End If
End If

Return intResult
End Function

At this time I am not getting any errors. How can I retrieve the data that I am after?
Michaeldude
your code is all screwed up. re-work on your logic. you dont need to select the actualpassword in a sep variable and compare it against whats in the variable. you can directly compare it against the value in the table.

i'd would rewrite your query as :


CREATE PROCEDURE stpMyAuthentication

(

@.fldUsername varchar( 50 ),
@.fldPassword varChar( 25 ),
@.fldFirstName varchar(30) OUTPUT,
@.fldLastName varchar( 30 ) OUTPUT
@.intRes int OUTPUT
)
As
SET NOCOUNT OFF
BEGIN

--@.intRes 0=no user. 1=valid. 2=invalid passwd

IF EXISTS ( Select * from tbMembUsers where fldUsername = @.fldUsername and fldPassword=@.fldPassword )
BEGIN
SELECT @.fldFirstName=fldFirstName, @.fldLastName =fldLastName
FROMtbMembUsers
WHEREfldUsername = @.fldUsername AND fldPassword=@.fldPassword
SET@.intRes=1
END

ELSE
BEGIN
-- INVALID PASSWORD
IF EXISTS ( Select * from tbMembUsers where fldUsername = @.fldUsername )
SET @.intRes=2

--No CREDENTIALS FOUND SO RETURN 0
IF EXISTS ( select * From tbMembUsers where fldUsername = @.fldUsername and fldPassword=@.fldPassword )
SET @.intRes=0
END

SET NOCOUNT OFF
END

and from the asp.net page you can use the sample code here to modify it acc to your stored proc.


Dim myCommand As SqlCommand
Dim myParam As SqlParameter
dim resvalue as integer

myCommand = New SqlCommand()
myCommand.Connection = objcon
myCommand.CommandText = "testmovein_cusinfo"
myCommand.CommandType = CommandType.StoredProcedure

myCommand.Parameters.Add(New SqlParameter("@.username",SqlDbType.varchar,30))
myCommand.Parameters("@.username").Value = Trim(username)

'output parameter
myParam = mycommand.CreateParameter()
myParam.ParameterName = "@.result"
myParam.Direction = ParameterDirection.Output
myParam.SqlDbType = SqlDbType.int
mycommand.Parameters.Add(myParam)

Try
If objCon.State = 0 Then objCon.Open()
mycommand.ExecuteNonQuery()
resvalue=convert.toint16((mycommand.Parameters("@.result").Value))

Catch exc As Exception
Response.Write(exc)
Finally
If objCon.State = ConnectionState.Open Then
objCon.Close()
End If
'objCon.Dispose()
End Try

hth|||why do you SET NOCOUNT OFF twice?

and i think you'll confuse him with
'output parameter
myParam = mycommand.CreateParameter()
myParam.ParameterName = "@.result"
myParam.Direction = ParameterDirection.Output
myParam.SqlDbType = SqlDbType.int
mycommand.Parameters.Add(myParam)
----

mycommand.Paramters.Add("@.Result", SqlDbType.int)
mycommand.Parameters("@.Result").Direction = ParameterDirection.Output

does the same thing. :)|||yeah sorry abt the nocount thing.

theres always more than one way you can do a thing in. i just cut n pasted some template code.|||Thanks guys. I finally got it. Even though this code works just fine, I am wondering if I have some unnecessary code. Can you please check it for me thanks.


Function MyAuthentication(ByVal strUsername As String, ByVal strPassword As String) As Integer
' Variable Declaration
Dim myConn As SqlConnection
Dim myCmd As SqlCommand
Dim myParam As SqlParameter
Dim intResult As Integer
Dim sqlConn As String
Dim strFirstName, strLastName As String
Dim resvalue As Integer

sqlConn = ConfigurationSettings.AppSettings("sqlDbConn")
myConn = New SqlConnection(sqlConn)

myCmd = New SqlCommand("stpMyAuthentication", myConn)
myCmd.CommandType = CommandType.StoredProcedure

'output parameter
myParam = myCmd.CreateParameter()

myParam.Direction = ParameterDirection.Output
myParam.SqlDbType = SqlDbType.Int
myCmd.Parameters.Add(myParam)

'########################################################
myCmd = New SqlCommand
myCmd.Connection = myConn
myCmd.CommandText = "stpMyAuthentication"
myCmd.CommandType = CommandType.StoredProcedure

myCmd.Parameters.Add("@.intRes", SqlDbType.Int)
myCmd.Parameters("@.intRes").Direction = ParameterDirection.Output
myCmd.Parameters.Add(Trim("@.fldUsername"), Trim(strUsername))
myCmd.Parameters.Add(Trim("@.fldPassword"), Trim(strPassword))

myCmd.Parameters.Add("@.fldFirstName", SqlDbType.VarChar, 30)
myCmd.Parameters("@.fldFirstName").Direction = ParameterDirection.Output
myCmd.Parameters.Add("@.fldLastName", SqlDbType.VarChar, 30)
myCmd.Parameters("@.fldLastName").Direction = ParameterDirection.Output

'########################################################
Try
Response.Write("1<br>")
If myConn.State = 0 Then myConn.Open()
myCmd.ExecuteNonQuery()
resvalue = Convert.ToInt16((myCmd.Parameters("@.intRes").Value))
strFirstName = myCmd.Parameters("@.fldFirstName").Value
strLastName = myCmd.Parameters("@.fldLastName").Value
Session("strFullName") = strFirstName & " " & strLastName

Catch exc As Exception
Response.Write(exc)
Finally
If myConn.State = ConnectionState.Open Then
myConn.Close()
End If
'objCon.Dispose()
End Try

Select Case resvalue
Case 0
lblMessage.Text = "Username Not Registered!<br><br>"
Case 1
If strPassword = "55555555" Then
Session("intDefaultPass") = 1
End If
Case 2
lblMessage.Text = "Invalid Password!<br><br>"
End Select

Return resvalue
End Function