Madsing
asked on
Retrive result from stored procedure in web application
Hi,
I have created a Stored Procedure which select some rows and inserts them into a temporary table before the table (tmpCust) gets dropped again.
It is also used by a web service for some others usage.
I would like to receive the content from the SP in a asp.net application but is a little uncertain how?
Lets say that I wanted the content to be displayed in a gridview.
I have made a test page where I have placed a calendar control and a gridview but with no code.
My SP:
I have created a Stored Procedure which select some rows and inserts them into a temporary table before the table (tmpCust) gets dropped again.
It is also used by a web service for some others usage.
I would like to receive the content from the SP in a asp.net application but is a little uncertain how?
Lets say that I wanted the content to be displayed in a gridview.
I have made a test page where I have placed a calendar control and a gridview but with no code.
My SP:
GO
/****** Object: StoredProcedure [dbo].[CustomerWebServiceGetByDate] Script Date: 04/19/2011 20:59:15 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[CustomerWebServiceGetByDate]
@InputDate as datetime = '1753-01-01 00:00:000'
as
--declare @InputDate as datetime
--set @InputDate = '2009-12-31 12:30:000' --'1753-01-01 00:00:000'
--set @InputDate = dateadd(day,-5,getdate())
declare @day as varchar(2)
declare @month as varchar(2)
set @day = datepart(dd,@InputDate)
set @month = datepart(mm,@InputDate)
if len(@day) = 1
begin
set @day = '0' + cast(@day as char(1))
end
if len(@month) = 1
begin
set @month = '0' + cast(@month as char(1))
end
set @InputDate = cast(datepart(yyyy,@InputDate) as varchar(4)) + '-' +
cast(@month as varchar(2)) + '-' +
cast(@day as varchar(2)) + ' 00:00:000'
create table dbo.tmpCust
(CustomerNo varchar(50),
ProductionDate datetime,
DiscontinuedDate datetime,
StopProductionDate datetime)
if @InputDate <> '1753-01-01 00:00:000'
begin
insert into dbo.tmpCust
SELECT distinct
CustomerNo,
ProductionDate,
DiscontinuedDate,
StopProductionDate
FROM Customer
Where
(--ProductionDate <= @InputDate
(ProductionDate <= @InputDate and ProductionDate <> '1753-01-01 00:00:000')
and
StopProductionDate = '1753-01-01 00:00:000'
and
(DiscontinuedDate >= @InputDate or DiscontinuedDate = '1753-01-01 00:00:000')
)
or
(--ProductionDate <= @InputDate
(ProductionDate <= @InputDate and ProductionDate <> '1753-01-01 00:00:000')
and
StopProductionDate >= @InputDate
and
(DiscontinuedDate >= @InputDate or DiscontinuedDate = '1753-01-01 00:00:000')
)
order by CustomerNo
end
else
begin
insert into dbo.tmpCust
SELECT distinct
CustomerNo,
ProductionDate,
DiscontinuedDate,
StopProductionDate
FROM Customer
Where
StopProductionDate <= getdate()
order by CustomerNo
end
select distinct CustomerNo
from dbo.tmpCust
order by CustomerNo
drop table tmpCust
ASKER
The new SP:
USE [SQL2008_640987_merevind]
GO
/****** Object: StoredProcedure [dbo].[CustomerWebServiceGetByDate2] Script Date: 04/19/2011 22:11:10 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[CustomerWebServiceGetByDate2]
@InputDate as datetime = '1753-01-01 00:00:000'
as
--declare @InputDate as datetime
--set @InputDate = '2009-12-31 12:30:000' --'1753-01-01 00:00:000'
--set @InputDate = dateadd(day,-5,getdate())
declare @day as varchar(2)
declare @month as varchar(2)
set @day = datepart(dd,@InputDate)
set @month = datepart(mm,@InputDate)
if len(@day) = 1
begin
set @day = '0' + cast(@day as char(1))
end
if len(@month) = 1
begin
set @month = '0' + cast(@month as char(1))
end
set @InputDate = cast(datepart(yyyy,@InputDate) as varchar(4)) + '-' +
cast(@month as varchar(2)) + '-' +
cast(@day as varchar(2)) + ' 00:00:000'
SELECT distinct
CustomerNo,
ProductionDate,
DiscontinuedDate,
StopProductionDate
FROM Customer
WHERE
@InputDate <= [StopProductionDate]
SqlConnection conn = new SqlConnection("Data Source=myDBServer;Database=myDB;User ID=myUser; Password=myPassword");
SqlCommand command = new SqlCommand("CustomerWebServiceGetByDate2", conn);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("@InputDate", SqlDbType.DateTime).Value = Now;
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataSet ds = new DataSet();
adapter.Fill(ds);
myGridView.DataSource = ds;
myGridView.DataBind;
If you are using Visual Basic, you can convert this code here: http://www.developerfusion.com/tools/convert/csharp-to-vb/
Login failed for user 'myusername' - Check your connection string. Make sure that the username and password are correct. See here for guidance http://www.connectionstrings.com/sql-server-2005
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Wrong query syntax in SP
ASKER
When I then bind it with a gridview and test it I get a Servererror
Login failed for user 'myusername'. Before I ran it in a browser I tested the query in the "Configure Data source" wizard with no problem. I was asked to type in my password twice though, but it didn't fail?!
Please help?