Hello people, I need your advice, what to do. I use LINQ stored procedure to select all data from sql table to DataGridView:
EtiketyLINQDataContext EtiketyData = new EtiketyLINQDataContext();
IEnumerable VypisDat = EtiketyData.Vypis("Cislo", "Typ", "Hrubka", "MaterialTyp");
LINQdataGridView.DataSource = VypisDat;
The problem occurs with this line:
IEnumerable VypisDat = EtiketyData.Vypis("Cislo", "Typ", "Hrubka", "MaterialTyp");
...saying 'Cannot implicitly convert type 'int' to 'System.Collections.IEnumerable'.
The stored procedure "Vypis" looks like this:
ALTER PROCEDURE dbo.Vypis
(
@Column1 varchar(50),
@Column2 varchar(50),
@Column3 varchar(50),
@Tablename varchar(50)
)
AS
/*Vypis vsetkych stlpcov tabulky*/
EXEC ('SELECT ' + @Column1 + ', '+@Column2+', '+@Column3+' FROM '+@Tablename)
RETURN
I realize that the problem can be also in bad type in design view of the table, where the code is like this:
[Function(Name = "dbo.Vypis")]
public int
Vypis([Parameter(Name = "Column1", DbType = "VarChar(50)")] string
column1, [Parameter(Name = "Column2", DbType = "VarChar(50)")] string
column2, [Parameter(Name = "Column3", DbType = "VarChar(50)")] string
column3, [Parameter(Name = "Tablename", DbType = "VarChar(50)")] string
tablename)
{
IExecuteResult result =
this.ExecuteMethodCall(this,
((MethodInfo)(MethodInfo.GetCurrentMethod())), column1, column2,
column3, tablename);
return ((int)(result.ReturnValue));
}
But here, when I change type on System.Collections.IEnumerable, I get message:
'System.Collections.IEnumerable' is not a valid return type for a mapped stored procedure method'.
I'm now completely lost, what shall I do... I hope, the explanation is not complicated.
matej
Loading
AlexPosted Oct 6, 2008, 9:24 AM
Here's what I've done for a work around. Go back to your Linq to Sql designer, and remove the stored proc from the list of methods. Save the file and close it. Then go to SQL and changed your stored proc for now to just select a simple select like this :
SELECT 1 as Customer, 2 as Whatever etc etc....
Meaning, you're going to fake it for now so that VS can determine the datatypes of what's being returned. Therefore, if let's say you need a bool you would do something like this : Select Cast(1 as bit) as MyBoolValue or whatever....
Once that's done, go back to Visual studio, drag the stored procedure back on to your designer, and that should create the object you need. Then GO BACK to your stored procedure, and change it back to the way it should be. You're basically tricking the designer and letting it know the return type it needs to generate.
Be careful though, if the DBML ever gets regenerated for whatever reason, you'll have to go through all this again.