Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.net 4.5 model binding broken when returning IEnumerable using vb?

I have a gridview as follows:

<asp:GridView ID="gv" runat="server" SelectMethod="gv_GetData" AllowPaging="true" AllowSorting="true" AutoGenerateColumns="true">
</asp:GridView>

I have the following code in c# that works:

public IList<string> gv_GetData(int maximumRows, int startRowIndex, out int totalRowCount, string sortByExpression)
{
    List<string> l = GetTestData();
    totalRowCount = l.Count;
    return l;
}

private List<string> GetTestData()
{
    List<string> l = new List<string>();
    l.Add("a");
    l.Add("b");
    l.Add("c");
    return l;
}

Now, in VB I have:

Public Function gv_GetData(maximumRows As Integer, startRowIndex As Integer, ByRef totalRowCount As Integer, sortByExpression As String) As IList(Of String)
    Dim l As List(Of String) = GetTestData()
    totalRowCount = l.Count
    Return l
End Function

Private Function GetTestData() As List(Of String)
    Dim l As New List(Of String)()
    l.Add("a")
    l.Add("b")
    l.Add("c")
    Return l
End Function

The VB version always generates the following error:

When the DataBoundControl has paging enabled, either the SelectMethod should return an IQueryable or should have all these mandatory parameters : int startRowIndex, int maximumRows, out int totalRowCount

Could this be a framework bug? Or am I missing something overly obvious?


Answered by nemesv below. New method is:

Public Function gv_GetData(maximumRows As Integer, startRowIndex As Integer, <System.Runtime.InteropServices.Out()> ByRef totalRowCount As Integer, sortByExpression As String) As IList(Of String)
    Dim l As List(Of String) = GetTestData()
    totalRowCount = l.Count
    Return l
End Function
like image 330
John Avatar asked Aug 21 '12 16:08

John


1 Answers

You need to mark the totalRowCount with the OutAttribute

<Out()> ByRef totalRowCount As Integer

This is semantically equivalent with the c# out keyword.

like image 52
nemesv Avatar answered Sep 27 '22 20:09

nemesv