none
DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'ID'.

    Question

  • I have an unbound gridview that is populated from a Stored Procedure. I also have an UPDATE command that provides DB updates on EDIT. I am trying to implement PAGING on the Gridview and get this error. The paging is also provided from a SP. Which is what I believe is throwing this error when it attempts to return to the Page Load witha  ROW COUNT..

    ALTER PROCEDURE [dbo].[PageSmarter]  
    
    @Table   NVARCHAR(128) = 'dbo.tblLangenUS',
    @FirstRow INT,
    @PageSize INT,
    @TotalRows INT OUTPUT
     
     AS
     BEGIN
     
     SET NOCOUNT ON; -- always, in every stored procedure  
     DECLARE     @first_id INT,
    				 @startRow INT, 
    				 @sql NVARCHAR(MAX); 
    				 
     SET @sql = N'WITH x AS
     (
    	SELECT
    		ID,
    		rn = ROW_NUMBER() OVER (ORDER BY ID)
    		FROM
    			' + @Table + '  
    )  
    
    SELECT rn, ID
    	INTO #x FROM x
    	WHERE rn BETWEEN ' + CONVERT(VARCHAR(12), @FirstRow)
    	+ 'AND (' + CONVERT(VARCHAR(12), @FirstRow)
    	+ ' +  ' + CONVERT(VARCHAR(12), @PageSize) + ' - 1);
    	
    SELECT first_id = MIN(ID) FROM #x;
    
    SELECT
    	ID, StringID_from_Master, GUID, short_Text, lang_String, date_Changed,
    	prev_LangString, needsTranslation, displayRecord, brief_Descrip    
    	FROM ' + @Table + ' AS src    
    	WHERE EXISTS    
    	(      
    		SELECT 1 FROM #x      
    		WHERE ID = src.ID    
    	);';  
    	
    EXEC sp_executeSQL @sql;
    
    SELECT @totalRows = SUM(row_count)    
    	FROM sys.dm_db_partition_stats    
    	WHERE [object_id] = OBJECT_ID(@Table);
    	
    END
    

    Here is the error stack....

    DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'ID'.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.Web.HttpException: DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'ID'.

    Source Error:

    Line 331:
    Line 332:    GridView2.DataSource = ds
    Line 333:    GridView2.DataBind()
    

     

    And this is the code behind....

     

        Dim myConnection As SqlConnection = New SqlConnection(connectionString)
        Dim myCommand = New SqlCommand("PageSmarter", myConnection)
        myCommand.CommandType = CommandType.StoredProcedure
    
        myCommand.Parameters.AddWithValue("@FirstRow", currentPageNumber)
        myCommand.Parameters.AddWithValue("@PageSize", PAGE_SIZE)
        myCommand.Parameters.AddWithValue("@Table", "tblLangenUS")
        myCommand.Parameters.Add("@TotalRows", SqlDbType.Int, 4)
    
        Dim totalRows As Double = CType(myCommand.Parameters("@TotalRows").Value, Integer)
        myCommand.Parameters("@TotalRows").Direction = ParameterDirection.Output
    
    
        Dim ad As SqlDataAdapter = New SqlDataAdapter(myCommand)
    
        Dim ds As DataTable = New DataTable()
        ad.Fill(ds)
    
        GridView2.DataSource = ds
        GridView2.DataBind()
    

     

     

     

     

    Thursday, August 04, 2011 2:42 PM

Answers

  • Hi

    Your are binding to the wrong data table.
    Change your code to:

    Dim ds As DataSet = New DataSet()
    ad.Fill(ds)
    GridView2.DataSource = ds.Tables(1)
    

    Marcel


    Thursday, August 04, 2011 7:43 PM

All replies

  • Hi

    Your are binding to the wrong data table.
    Change your code to:

    Dim ds As DataSet = New DataSet()
    ad.Fill(ds)
    GridView2.DataSource = ds.Tables(1)
    

    Marcel


    Thursday, August 04, 2011 7:43 PM
  • Hello,

    Please check if your GridView's declaration has specified property "DataKeyNames", for example:

    <asp:GridView ID="GridView1" DataKeyNames="ID" runat="server" ...
    

    Check here for more information. http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.datakeynames.aspx

    In addition, your question wasn't related to ADO.NET and DataSet. My option is to open a new thread into ASP.NET forum for further support. http://forums.asp.net

    Best Regards,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, August 08, 2011 5:07 AM
  • Hi Larcolais,

    In addition, your question wasn't related to ADO.NET and DataSet.

    The original poster is binding IMHO to the total rows return value (TotalRows) instead of binding to the rows. This is not apparent because he does it over a SqlDataAdapter instead of using the classic SqlDataReader. Now, the total rows return value is an integer. And this object obviously does not have a property named "ID". That's why (at least I suppose so) he gets the 'System.Data.DataRowView' does not contain a property with the name 'ID' error. If the original poster instead binds to the table rows, the property "ID" will be there and all will work just fine (it worked for me).

    Marcel



    Monday, August 08, 2011 7:04 AM