Question : Problem with datasource for gridview

I am executing the code below, where gvStudents is a gridview, but am getting the error:
Data source is an invalid type.  It must be either an IListSource, IEnumerable, or IDataSource.
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.InvalidOperationException: Data source is an invalid type.  It must be either an IListSource, IEnumerable, or IDataSource.

Source Error:

Line 159:        cmd.Parameters.AddRange(arParms)
Line 160:        'cmd.ExecuteNonQuery()
Line 161:        gvStudents.DataSource = cmd.ExecuteNonQuery()
Line 162:        gvStudents.DataBind()

What is wrong?

Thanks,

----------------

ConnString = ConfigurationManager.ConnectionStrings("dmConnectionString").ConnectionString
        connection = New SqlConnection(ConnString)

        connection.Open()
        Dim cmd As New SqlCommand("pageStudents", connection)
        cmd.CommandType = CommandType.StoredProcedure
        Dim arParms() As SqlParameter = New SqlParameter(2) {}
        arParms(0) = New SqlParameter("@Page", SqlDbType.NVarChar, 4)
        arParms(0).Value = strPage
        arParms(1) = New SqlParameter("@PageSize", SqlDbType.NVarChar, 4)
        arParms(1).Value = strPageSize
        arParms(2) = New SqlParameter("@UID", SqlDbType.NVarChar, 4)
        arParms(2).Value = strUID
        cmd.Parameters.AddRange(arParms)
        ‘ cmd.ExecuteNonQuery() ‘ THIS LINE WILL EXECUTE WITHOUT FAILURE
        gvStudents.DataSource = cmd.ExecuteNonQuery() ‘ FAILING LINE
        gvStudents.DataBind()

Answer : Problem with datasource for gridview

You are using ExecuteNonQuery which only return an integer values showing the number of records affected. You have to use ExecuteReader so that you can get adatareader with which you can bind the gridview or use adapter and fill a dataset using adapter.Fill() method and give it as datasource to gridview. That is y u r getting the error bcoz integer is neither of them ( IListSource, IEnumerable, or IDataSource. ).

Hope it helps you solve your problem.
Random Solutions  
 
programming4us programming4us