473,403 Members | 2,323 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,403 software developers and data experts.

Stored procedure->ASP.NET application

Hi,

I would like to ask you do you know how to return a resultset and int value
from Stored Procedure.
If we have a table
Teachers
=========
ID INT PK
NAME VARCHAR(25)
ADDR VARCHAR(75)

I would like to write a SP which must return the @COUNT of all teachers in
the table Teachers and
also the resultset from SELECT NAME FROM TEACHERS WHERE NAME="ADAM". Do you
know how to write this SP?
It must b e something like this:
CREATE STORED PROCEDURE
@NAME VARCHAR(25),
@COUNT INT OUTPUT
AS
SELECT @COUNT=COUNT(*) FROM TEACHERS
SELECT ADDR FROM TEACHERS WHERE NAME='ADAM'
return @COUNT

In the ASP.NET/C# application I do this:
dad.SelectCommand.Parameters.Add(new SqlParameter("@NAME",
SqlDbType.VarChar,25));
dad.SelectCommand.Parameters["@USRNAM"].Value =
Session["usrName"].ToString();
dad.SelectCommand.Parameters.Add(new SqlParameter("@Count", SqlDbType.Int));
dad.SelectCommand.Parameters["@Count"].Direction =
ParameterDirection.Output;
int count = (int)dad.SelectCommand.Parameters["@Count"].Value;
But there is error: Null Reference in the last line.
How can I catch the resultset from the sql query and also the OUTPUT VALUE?

Thank you!
Viktor
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.776 / Virus Database: 523 - Release Date: 12.10.2004 a.
Nov 16 '05 #1
3 2121
hi

hope this will work

CREATE PROCEDURE spGetTeachers
@NAME VARCHAR(25),
@COUNT INT OUTPUT
AS
SET NOCOUNT ON
SELECT @COUNT=COUNT(*) FROM TEACHERS
SELECT ADDR FROM TEACHERS WHERE NAME=@NAME
GO

cnn.Open();
SqlDataAdapter cmd=new SqlDataAdapter("spGetTeachers",cnn);
cmd.SelectCommand.CommandType =CommandType.StoredProcedure;
SqlParameter param=new SqlParameter ("@NAME",SqlDbType.VarChar,50);
param.Value ="Adams";

SqlParameter param2=new SqlParameter ("@COUNT",SqlDbType.Int,8);
param2.Direction =ParameterDirection.Output ;
param2.Value =-1;
cmd.SelectCommand.Parameters.Add(param) ;
cmd.SelectCommand.Parameters.Add(param2) ;

DataSet ds=new DataSet ();
cmd.Fill(ds);

param2.Value will give the count
regards
Ansil

"Viktor Popov" wrote:
Hi,

I would like to ask you do you know how to return a resultset and int value
from Stored Procedure.
If we have a table
Teachers
=========
ID INT PK
NAME VARCHAR(25)
ADDR VARCHAR(75)

I would like to write a SP which must return the @COUNT of all teachers in
the table Teachers and
also the resultset from SELECT NAME FROM TEACHERS WHERE NAME="ADAM". Do you
know how to write this SP?
It must b e something like this:
CREATE STORED PROCEDURE
@NAME VARCHAR(25),
@COUNT INT OUTPUT
AS
SELECT @COUNT=COUNT(*) FROM TEACHERS
SELECT ADDR FROM TEACHERS WHERE NAME='ADAM'
return @COUNT

In the ASP.NET/C# application I do this:
dad.SelectCommand.Parameters.Add(new SqlParameter("@NAME",
SqlDbType.VarChar,25));
dad.SelectCommand.Parameters["@USRNAM"].Value =
Session["usrName"].ToString();
dad.SelectCommand.Parameters.Add(new SqlParameter("@Count", SqlDbType.Int));
dad.SelectCommand.Parameters["@Count"].Direction =
ParameterDirection.Output;
int count = (int)dad.SelectCommand.Parameters["@Count"].Value;
But there is error: Null Reference in the last line.
How can I catch the resultset from the sql query and also the OUTPUT VALUE?

Thank you!
Viktor
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.776 / Virus Database: 523 - Release Date: 12.10.2004 a.

Nov 16 '05 #2
Hi Viktor,

Please also note that using the Fill keyword will return the number of
affected rows : graphically create a DataAdapter, and a Dataset
modeled after your teachers table, and then :

dsTeachers myTeachers = new dsTeachers();
int NumberOfAffectedRecords = daTeachers.Fill(myTeachers);

HTH,

Michel
"Ansil MCAD" <An*******@discussions.microsoft.com> wrote in message news:<EB**********************************@microso ft.com>...
hi

hope this will work

CREATE PROCEDURE spGetTeachers
@NAME VARCHAR(25),
@COUNT INT OUTPUT
AS
SET NOCOUNT ON
SELECT @COUNT=COUNT(*) FROM TEACHERS
SELECT ADDR FROM TEACHERS WHERE NAME=@NAME
GO

cnn.Open();
SqlDataAdapter cmd=new SqlDataAdapter("spGetTeachers",cnn);
cmd.SelectCommand.CommandType =CommandType.StoredProcedure;
SqlParameter param=new SqlParameter ("@NAME",SqlDbType.VarChar,50);
param.Value ="Adams";

SqlParameter param2=new SqlParameter ("@COUNT",SqlDbType.Int,8);
param2.Direction =ParameterDirection.Output ;
param2.Value =-1;
cmd.SelectCommand.Parameters.Add(param) ;
cmd.SelectCommand.Parameters.Add(param2) ;

DataSet ds=new DataSet ();
cmd.Fill(ds);

param2.Value will give the count
regards
Ansil

"Viktor Popov" wrote:
Hi,

I would like to ask you do you know how to return a resultset and int value
from Stored Procedure.
If we have a table
Teachers
=========
ID INT PK
NAME VARCHAR(25)
ADDR VARCHAR(75)

I would like to write a SP which must return the @COUNT of all teachers in
the table Teachers and
also the resultset from SELECT NAME FROM TEACHERS WHERE NAME="ADAM". Do you
know how to write this SP?
It must b e something like this:
CREATE STORED PROCEDURE
@NAME VARCHAR(25),
@COUNT INT OUTPUT
AS
SELECT @COUNT=COUNT(*) FROM TEACHERS
SELECT ADDR FROM TEACHERS WHERE NAME='ADAM'
return @COUNT

In the ASP.NET/C# application I do this:
dad.SelectCommand.Parameters.Add(new SqlParameter("@NAME",
SqlDbType.VarChar,25));
dad.SelectCommand.Parameters["@USRNAM"].Value =
Session["usrName"].ToString();
dad.SelectCommand.Parameters.Add(new SqlParameter("@Count", SqlDbType.Int));
dad.SelectCommand.Parameters["@Count"].Direction =
ParameterDirection.Output;
int count = (int)dad.SelectCommand.Parameters["@Count"].Value;
But there is error: Null Reference in the last line.
How can I catch the resultset from the sql query and also the OUTPUT VALUE?

Thank you!
Viktor
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.776 / Virus Database: 523 - Release Date: 12.10.2004 a.

Nov 16 '05 #3
Thank you for the replies!
It works now:)

Regards,

Viktor



"Ansil MCAD" <An*******@discussions.microsoft.com> wrote in message
news:EB**********************************@microsof t.com...
hi

hope this will work

CREATE PROCEDURE spGetTeachers
@NAME VARCHAR(25),
@COUNT INT OUTPUT
AS
SET NOCOUNT ON
SELECT @COUNT=COUNT(*) FROM TEACHERS
SELECT ADDR FROM TEACHERS WHERE NAME=@NAME
GO

cnn.Open();
SqlDataAdapter cmd=new SqlDataAdapter("spGetTeachers",cnn);
cmd.SelectCommand.CommandType =CommandType.StoredProcedure;
SqlParameter param=new SqlParameter ("@NAME",SqlDbType.VarChar,50);
param.Value ="Adams";

SqlParameter param2=new SqlParameter ("@COUNT",SqlDbType.Int,8);
param2.Direction =ParameterDirection.Output ;
param2.Value =-1;
cmd.SelectCommand.Parameters.Add(param) ;
cmd.SelectCommand.Parameters.Add(param2) ;

DataSet ds=new DataSet ();
cmd.Fill(ds);

param2.Value will give the count
regards
Ansil

"Viktor Popov" wrote:
Hi,

I would like to ask you do you know how to return a resultset and int value from Stored Procedure.
If we have a table
Teachers
=========
ID INT PK
NAME VARCHAR(25)
ADDR VARCHAR(75)

I would like to write a SP which must return the @COUNT of all teachers in the table Teachers and
also the resultset from SELECT NAME FROM TEACHERS WHERE NAME="ADAM". Do you know how to write this SP?
It must b e something like this:
CREATE STORED PROCEDURE
@NAME VARCHAR(25),
@COUNT INT OUTPUT
AS
SELECT @COUNT=COUNT(*) FROM TEACHERS
SELECT ADDR FROM TEACHERS WHERE NAME='ADAM'
return @COUNT

In the ASP.NET/C# application I do this:
dad.SelectCommand.Parameters.Add(new SqlParameter("@NAME",
SqlDbType.VarChar,25));
dad.SelectCommand.Parameters["@USRNAM"].Value =
Session["usrName"].ToString();
dad.SelectCommand.Parameters.Add(new SqlParameter("@Count", SqlDbType.Int)); dad.SelectCommand.Parameters["@Count"].Direction =
ParameterDirection.Output;
int count = (int)dad.SelectCommand.Parameters["@Count"].Value;
But there is error: Null Reference in the last line.
How can I catch the resultset from the sql query and also the OUTPUT VALUE?
Thank you!
Viktor
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.776 / Virus Database: 523 - Release Date: 12.10.2004 a.

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.776 / Virus Database: 523 - Release Date: 12.10.2004 a.
Nov 16 '05 #4

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

Similar topics

6
by: Shaun Stuart | last post by:
I've got a webpage that calls some stored procedures with input variables. The procedures return recordsets and also some output variables. We're trying to get the values of the output variables....
0
by: Nashat Wanly | last post by:
HOW TO: Call a Parameterized Stored Procedure by Using ADO.NET and Visual C# .NET View products that this article applies to. This article was previously published under Q310070 For a Microsoft...
4
by: Robin Tucker | last post by:
Hi, I'm trying to determine with my program whether or not a given database supports a given feature set. To do this I'm querying for certain stored procedures in the sysobjects table and if...
2
by: Dino L. | last post by:
How can I run stored procedure (MSSQL) ?
0
by: Ranginald | last post by:
Is there way to execute a certain stored procedure based on the logged in user? Could you store the storedprocedure name using Profiles, and then pull the data when the user is logged in? Thanks!
1
by: a.mustaq | last post by:
Hi All, I have some doubts regarding to caching. 1.Where cached objects are stored. 2.What is public caching and private caching. 3.Is caching user specific or application specific Please...
12
by: Light | last post by:
Hi all, I posted this question in the sqlserver.newusers group but I am not getting any response there so I am going to try it on the fine folks here:). I inherited some legacy ASP codes in my...
1
by: sheenaa | last post by:
Hello Members, I m creating my application forms in ASP.Net 2005 C# using the backend SQL Server 2005. What i have used on forms :: ? On my first form i have used some...
12
by: brwalias | last post by:
Hi, using .net 2 sql server 2005 Here is my situation: I'm passing a variable in the url from a selection on Page A and need to display the results on the Results page be based on that...
4
by: Chris | last post by:
Hi, i have a formview in Insert mode. when a new record is inserted, the text of a textbox becomes: "ok". Instead of using the ItemInserted or ItemInserting, i use my own procedure like this: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.