C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Forums » .NET » ASP.NET »

Functions in SQL 2000


Posted Date: 07 Nov 2009      Posted By: ritu      Member Level: Silver     Points: 1   Responses: 2



I have created a Foll. Function in SQL.It is working correctly.


CREATE FUNCTION whichContinent
(@Country nvarchar(15))
RETURNS varchar(30)
AS
BEGIN
declare @Return varchar(30)
select @return = case @Country
when 'Argentina' then 'South America'
when 'Belgium' then 'Europe'
when 'Brazil' then 'South America'
when 'Canada' then 'North America'
when 'Denmark' then 'Europe'
when 'Finland' then 'Europe'
when 'France' then 'Europe'
else 'Unknown'
end

return @return
end

print dbo.WhichContinent('France')

create table test
(Country varchar(15),
Continent as (dbo.WhichContinent(Country)))
insert into test (country) values ('USA')
insert into test (country) values ('France')

select * from test


Now I want to use this function from ASP.Net

public partial class FrmFunctioncs : System.Web.UI.Page
{
SqlCommand cmd ;
SqlDataReader dr;

protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
string query = "SELECT * FROM dbo.whichContinent('France')";
cmd=new SqlCommand (query ,conn ) ;
conn.Open();
dr = cmd.ExecuteReader();
string continentName = dr["Continent"].ToString ();
conn.Close ();
}


when command dr = cmd.ExecuteReader(); is executed,error is dere
Invalid object name 'dbo.whichContinent'.
But the function is existing in my DB. then why the error is coming ????







Responses

Author: ABitSmart    07 Nov 2009Member Level: DiamondRating: 2 out of 52 out of 5     Points: 2

Try it this way,

protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
string query = "SELECT dbo.whichContinent('France')";
cmd=new SqlCommand (query ,conn ) ;
conn.Open();
dr = cmd.ExecuteReader();
string continentName = dr["Continent"].ToString ();
conn.Close ();
}


Kind regards,
ABitSmart
DNS Web-master, DNS MVM
My blog
Thoughts.exe



Author: Bunty    08 Nov 2009Member Level: DiamondRating: 2 out of 52 out of 5     Points: 2

Hi,

You have call the function incorrectly,so it is giving function.

Modify the below statement
string query = "SELECT * FROM dbo.whichContinent('France')";

To
string query = "SELECT dbo.whichContinent('France')";

Now execute the your modify code.


Thanks & Regards
S.S.Bajoria



Post Reply
You must Sign In to post a response.
Next : Stored Procedures + ASP.NET
Previous : Crystal Report
Return to Discussion Forum
Post New Message
Category: ASP.NET

Related Messages



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use