Click here to Skip to main content
15,897,226 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am using linq and asp.net ...the code is below

C#
public static List<category> getPopularCategories() 
{
    clsCityPageDataContext cox = new clsCityPageDataContext();
    var cats = from cat in cox.Categories 
               select cat;
    return cats.ToList<category>().GetRange(0,10);
}


it get result from

SQL
USE [cpDB]
GO

DECLARE	@return_value int

EXEC	@return_value = [dbo].[Directory_GetPopularCategories]

SELECT	'Return Value' = @return_value

GO

........
i am getting
System.InvalidCastException: Unable to cast object of type 'System.Int64' to type 'System.String'.error
can any one help me?
Posted
Updated 10-May-11 3:13am
v3

change this:

SQL
DECLARE	@return_value int


to:

SQL
DECLARE	@return_value nvarchar(MAX)
 
Share this answer
 
Comments
Member 7911856 10-May-11 9:16am    
i am doing this but the sp do not updated succesfully....how can i update my sp?
jim lahey 10-May-11 9:23am    
When you edit the SP in SMSS, you'll see it's wrapped in an ALTER statement.. click on the button what says Execute
Member 7911856 10-May-11 9:29am    
when i right click on my sp and click modify..i get
ALTER PROCEDURE [dbo].[Directory_GetPopularCategories]

AS
BEGIN

Select Top 5 * from Categories Order by Hits
END
.......when i click execute sp ..i get
USE [cpDB]
GO

DECLARE @return_value int

EXEC @return_value = [dbo].[Directory_GetPopularCategories]

SELECT 'Return Value' = @return_value

GO
here i change ur statment like

DECLARE @return_value nvarchar(MAX)
and execute it,but no result....
SQL
check it twice. some where it requires string value and you are passing int value (based on your error description). so it will give you the error.

Hint : error is in below portion of code.
DECLARE @return_value int
EXEC @return_value = [dbo].[Directory_GetPopularCategories]
SELECT 'Return Value' = @return_value
 
Share this answer
 
BTW, why are you using linq to get the list? Why not just do this:

C#
List<category> cats = cox.Categories.GetRange(0, Math.Min(10, cox.Categories));


It will throw an exception if you specify more items than what exists in the list, that's why I used Math.Min.
 
Share this answer
 
v2
Comments
Member 7911856 10-May-11 9:22am    
i use this
public static List<category> getPopularCategories() {
clsCityPageDataContext cox = new clsCityPageDataContext();
return List<category> cats = cox.Categories.GetRange(0, Math.Min(10, cox.Categories));
}
this gives me error
CS0305: Using the generic type 'System.Collections.Generic.List<t>' requires '1' type arguments
#realJSOP 10-May-11 11:18am    
Are you completely incapable of figuring out what's wrong by using, oh, I don't know... the DEBUGGER!?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900