Solved

ASP.Net GridView ObjectDataSource Issue

Posted on 2010-11-24
6
1,117 Views
Last Modified: 2012-05-10
Dear Friends,

I am working on a GridView with ObjectDataSource and getting the following error.

"ObjectDataSource 'MyDataSource' could not find a non-generic method 'SelectMethod' that has parameters: MaxRows, StartRow. "

Please help to sort this issue. The complete code is given below:

.aspx -- Only the gridview and objectdatasource is given but there are other controls.

 <div id="divGrid" runat="server">
        <asp:GridView ID="gvGrid" runat="server" AllowPaging="true" AllowSorting="false" PageSize="4"
            DataKeyNames="SupplierID" AutoGenerateColumns="false" CellPadding="2" DataSourceID="MyDataSource">
        </asp:GridView>
       
  <asp:ObjectDataSource ID="MyDataSource" runat="server" TypeName="OMS.DAL.SupplierBL"
        SelectCountMethod="SelectCountMethod" SelectMethod="SelectMethod" EnablePaging="true" StartRowIndexParameterName="StartRow" MaximumRowsParameterName="MaxRows">
        </asp:ObjectDataSource>
     </div>



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

OMS.DAL.SupplierBL



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OMS.Entity;
using OMS.DAL;

namespace OMS.DAL
{
    public class SupplierBL
    {
        public SupplierBL()
        {
        }

        private int totalRows;

        public SupplierCollection SelectMethod(int MaxRows, int StartRow, string supplierName, string supplierEmail, int countryID, int cityID)
        {
            try
            {
                Supplier sup = new Supplier();
                SupplierCollection suc = new SupplierCollection();

                sup.SupplierName = supplierName;
                sup.SupplierEmail = supplierEmail;
                sup.SupplierCountry = countryID;
                sup.SupplierCity = cityID;

                SupplierDataSource sd = new SupplierDataSource();

                int totRows = 0;
                suc = sd.SupplierSearch(sup, StartRow, MaxRows, ref totRows);

                totalRows = totRows;

                return suc;


            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {

            }

        }

        public int SelectCountMethod()
        {
            return totalRows;
        }
    }
}


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



using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using OMS.Entity;

namespace OMS.DAL
{
    public class SupplierDataSource
    {
        private SqlConnection con;
        private SqlCommand cmd;
        private DataSet ds;
        private SqlDataAdapter da;
        private SqlDataReader dr;

        private string connectionString;
        public SupplierDataSource()
        {
            con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConStr"].ToString());
            connectionString = ConfigurationManager.ConnectionStrings["ConStr"].ToString();
        }

        public Int32 SupplierAdd(Supplier sup)
        {
            Int32 rowsAffected = 0;
            try
            {
                using (SqlConnection con1 = new SqlConnection(connectionString))
                {
                    using (SqlCommand cmd1 = new SqlCommand("SupplierInsert", con1))
                    {
                        cmd1.CommandType = CommandType.StoredProcedure;

                        cmd1.Parameters.AddWithValue("@SupplierID", sup.SupplierID);
                        cmd1.Parameters.AddWithValue("@SupplierName", sup.SupplierName);
                        cmd1.Parameters.AddWithValue("@SupplierAddress1", sup.SupplierAddress1);
                        cmd1.Parameters.AddWithValue("@SupplierAddress2", sup.SupplierAddress2);
                        cmd1.Parameters.AddWithValue("@SupplierCity", sup.SupplierCity);
                        cmd1.Parameters.AddWithValue("@SupplierCountry", sup.SupplierCountry);
                        cmd1.Parameters.AddWithValue("@SupplierPinCode", sup.SupplierPinCode);
                        cmd1.Parameters.AddWithValue("@SupplierEmail", sup.SupplierEmail);
                        cmd1.Parameters.AddWithValue("@SupplierTelephone", sup.SupplierTelephone);

                        con1.Open();

                        rowsAffected = cmd1.ExecuteNonQuery();
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message + " -- " + ex.InnerException);
            }
            finally
            {
            }

            return rowsAffected;
        }

        public int SupplierUpdate(Supplier sup)
        {
            Int32 rowsAffected = 0;
            try
            {
                using (SqlConnection con1 = new SqlConnection(connectionString))
                {
                    using (SqlCommand cmd1 = new SqlCommand("SupplierUpdate", con1))
                    {
                        cmd1.CommandType = CommandType.StoredProcedure;

                        cmd1.Parameters.AddWithValue("@SupplierID", sup.SupplierID);
                        cmd1.Parameters.AddWithValue("@SupplierName", sup.SupplierName);
                        cmd1.Parameters.AddWithValue("@SupplierAddress1", sup.SupplierAddress1);
                        cmd1.Parameters.AddWithValue("@SupplierAddress2", sup.SupplierAddress2);
                        cmd1.Parameters.AddWithValue("@SupplierCity", sup.SupplierCity);
                        cmd1.Parameters.AddWithValue("@SupplierCountry", sup.SupplierCountry);
                        cmd1.Parameters.AddWithValue("@SupplierPinCode", sup.SupplierPinCode);
                        cmd1.Parameters.AddWithValue("@SupplierEmail", sup.SupplierEmail);
                        cmd1.Parameters.AddWithValue("@SupplierTelephone", sup.SupplierTelephone);

                        con1.Open();

                        rowsAffected = cmd1.ExecuteNonQuery();
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message + " -- " + ex.InnerException);
            }
            finally
            {
            }

            return rowsAffected;
        }


        public int SupplierDelete(int supID)
        {
            Int32 rowsAffected = 0;
            try
            {
                using (SqlConnection con1 = new SqlConnection(connectionString))
                {
                    using (SqlCommand cmd1 = new SqlCommand("SupplierDelete", con1))
                    {
                        cmd1.CommandType = CommandType.StoredProcedure;

                        con1.Open();

                        rowsAffected = cmd1.ExecuteNonQuery();
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message + " -- " + ex.InnerException);
            }
            finally
            {
            }

            return rowsAffected;
        }


        public Supplier SupplierFetchByID(int supID)
        {
            Supplier sup = new Supplier();
            try
            {
                using (SqlConnection con1 = new SqlConnection(connectionString))
                {
                    using (SqlCommand cmd1 = new SqlCommand("SupplierFetchByID", con1))
                    {
                        cmd1.CommandType = CommandType.StoredProcedure;

                        cmd1.Parameters.AddWithValue("SupplierID", supID);

                        con1.Open();

                        //Int32 rowsAffected = cmd1.ExecuteNonQuery();

                        SqlDataReader dr = cmd1.ExecuteReader();

                        if (dr.HasRows)
                        {
                            while (dr.Read())
                            {
                                sup.SupplierID = dr.GetInt32(0);
                                sup.SupplierName = dr.GetString(1);
                                sup.SupplierAddress1 = dr.GetString(2);
                                sup.SupplierAddress2 = dr.GetString(3);
                                sup.SupplierCity = dr.GetInt32(4);
                                sup.SupplierCountry = dr.GetInt32(5);
                                sup.SupplierPinCode = dr.GetString(6);
                                sup.SupplierEmail = dr.GetString(7);
                                sup.SupplierTelephone = dr.GetString(8);
                            }
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message + " -- " + ex.InnerException);
            }
            finally
            {
            }
            return sup;
        }


        public SupplierCollection SupplierSearch(Supplier sup, int startRowIndex, int maximumRows, ref int totalRows)
        {
           

            SupplierCollection suc = new SupplierCollection();
            Supplier su = new Supplier();

            try
            {
                using (SqlConnection con1 = new SqlConnection("connectionString"))
                {
                    using (SqlCommand cmd1 = new SqlCommand("SupplierSearch", con1))
                    {
                        cmd1.CommandType = CommandType.StoredProcedure;

                        //cmd1.Parameters.AddWithValue("@SupplierID", sup.SupplierID);

                        cmd1.Parameters.AddWithValue("@SupplierName", sup.SupplierName);

                       // cmd1.Parameters.AddWithValue("@SupplierAddress1", sup.SupplierAddress1);

                        //cmd1.Parameters.AddWithValue("@SupplierAddress2", sup.SupplierAddress2);

                        cmd1.Parameters.AddWithValue("@SupplierCity", sup.SupplierCity);

                        cmd1.Parameters.AddWithValue("@SupplierCountry", sup.SupplierCountry);

                       // cmd1.Parameters.AddWithValue("@SupplierPinCode", sup.SupplierPinCode);

                        cmd1.Parameters.AddWithValue("@SupplierEmail", sup.SupplierEmail);

                        cmd1.Parameters.AddWithValue("@StartRowIndex", startRowIndex);

                        cmd1.Parameters.AddWithValue("@MaximumRows", maximumRows);

                        cmd1.Parameters.AddWithValue("@TotalRow", 0);

                        SqlDataReader dr;

                        con1.Open();
                        dr = cmd1.ExecuteReader();

                        if (dr.HasRows)
                        {
                            while (dr.Read())
                            {
                                su = new Supplier();

                                su.SupplierID = dr.GetInt32(0);
                                su.SupplierName = dr.GetString(1);
                                su.SupplierAddress1 = dr.GetString(2);
                                su.SupplierAddress2 = dr.GetString(3);
                                su.SupplierCity = dr.GetInt32(4);
                                su.SupplierCountry = dr.GetInt32(5);
                                su.SupplierPinCode = dr.GetString(6);
                                su.SupplierEmail = dr.GetString(7);
                                su.SupplierTelephone = dr.GetString(8);

                                suc.Add(su);
                            }

                        }

                        totalRows = int.Parse(cmd1.Parameters["@TotalRows"].ToString());

                        con1.Close();

                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message + " -- " + ex.InnerException);
            }
            finally
            {
            }

            return suc;

        }

    }
}


_________________________________________

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;

namespace OMS.Entity
{
    public class Supplier
    {
        private int supplierID;
        private string supplierName;
        private string supplierAddress1;
        private string supplierAddress2;
        private int supplierCity;
        private int supplierCountry;
        private string supplierPinCode;
        private string supplierEmail;
        private string supplierTelephone;

        public int SupplierID
        {
            get { return supplierID; }
            set { supplierID = value; }
        }

        public string SupplierName
        {
            get { return supplierName; }
            set { supplierName = value; }
        }

        public string SupplierAddress1
        {
            get { return supplierAddress1; }
            set { supplierAddress1 = value; }
        }

        public string SupplierAddress2
        {
            get { return supplierAddress2; }
            set { supplierAddress2 = value; }
        }

        public int SupplierCity
        {
            get { return supplierCity; }
            set { supplierCity = value; }
        }

        public int SupplierCountry
        {
            get { return supplierCountry; }
            set { supplierCountry = value; }
        }

        public string SupplierPinCode
        {
            get { return supplierPinCode; }
            set { supplierPinCode = value; }
        }

        public string SupplierEmail
        {
            get { return supplierEmail; }
            set { supplierEmail = value; }
        }

        public string SupplierTelephone
        {
            get { return supplierTelephone; }
            set { supplierTelephone = value; }
        }
   
    }

    public class SupplierCollection : Collection<Supplier> { }
}

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

Stored Procedure:

USE [OMS]
GO
/****** Object:  StoredProcedure [dbo].[SupplierSearch]    Script Date: 11/25/2010 11:36:41 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[SupplierSearch]
(
        @SupplierName           VARCHAR(50) = NULL, -- default to NULL
        @SupplierCity           INT = NULL,
        @SupplierCountry        INT = NULL,
        @SupplierEmail          VARCHAR(50) = NULL,
        @StartRowIndex                  INT,
        @MaximumRows                  INT,
        @TotalRow                        INT OUTPUT
)
AS
BEGIN
      
DECLARE @first_id int, @startRow INT
DECLARE @StringCity VARCHAR(20)
DECLARE @StringCountry VARCHAR(20)

IF @StartRowIndex > 0
SET @StartRowIndex =  (@StartRowIndex - 1)  * @MaximumRows

IF @StartRowIndex = 0
SET @StartRowIndex = 1

--PRINT @StartRowIndex
SET ROWCOUNT @StartRowIndex



IF RTRIM(@SupplierName)='' OR @SupplierName IS NULL
      SET @SupplierName = '%'
ELSE
      SET @SupplierName = @SupplierName + '%'
IF RTRIM(@SupplierEmail)='' OR @SupplierEmail IS NULL
      SET @SupplierEmail = '%'
ELSE
      SET @SupplierEmail = @SupplierEmail + '%'
IF RTRIM(@SupplierCity)='' OR @SupplierCity = 0
      SET @StringCity = '%'
ELSE
      SET @StringCity = CONVERT(VARCHAR(20),@SupplierCity)
IF RTRIM(@SupplierCountry)='' OR @SupplierCountry = 0
      SET @StringCountry = '%'
ELSE
      SET @StringCountry = CONVERT(VARCHAR(20),@SupplierCountry)
      



               
        -- Append % for LIKE operator search (if not NULL)
--        IF @SupplierName IS NULL
--                SET @SupplierName = @SupplierName + '%'
--
--        IF @SupplierEmail IS NULL
--                SET @SupplierEmail = @SupplierEmail + '%'
               
  PRINT Convert(varchar(30),@StartRowIndex) + ' - @StartRowIndex'
  PRINT Convert(varchar(30),@MaximumRows) + ' - @MaximumRows'
 
  PRINT Convert(varchar(30),@SupplierName) + ' - @SupplierName'
  PRINT Convert(varchar(30),@StringCity) + ' - @SupplierCity'
  PRINT Convert(varchar(30),@StringCountry) + ' - @StringCountry'
  PRINT Convert(varchar(30),@SupplierEmail) + ' - @SupplierEmail'
 
 
       
        SET ROWCOUNT @StartRowIndex
       
        SELECT @first_id = SupplierID FROM Supplier Where
                        (@SupplierName IS NULL OR [SupplierName] LIKE @SupplierName) AND -- If passed only applies to filter
                --(SupplierCity = IsNull(@SupplierCity,SupplierCity)) AND
                --(SupplierCountry = IsNull(@SupplierCountry,SupplierCountry)) AND
               
                 (SupplierCity like @StringCity) AND
                (SupplierCountry like @StringCountry) AND
               
                (@SupplierEmail IS NULL OR SupplierEmail LIKE @SupplierEmail)  
        ORDER BY SupplierID
               
        PRINT Convert(varchar(30),@first_id) + ' - First ID'
               
            SET ROWCOUNT @MaximumRows
            
        SELECT * FROM Supplier WHERE
                (@SupplierName IS NULL OR [SupplierName] LIKE @SupplierName) AND -- If passed only applies to filter
--                (SupplierCity = IsNull(@SupplierCity,SupplierCity)) AND
--                (SupplierCountry = IsNull(@SupplierCountry,SupplierCountry)) AND

                         (SupplierCity like @StringCity) AND
                (SupplierCountry like @StringCountry) AND
                (@SupplierEmail IS NULL OR SupplierEmail LIKE @SupplierEmail) AND
                (SupplierID > @first_id) ORDER BY SupplierID
       
        SET ROWCOUNT 0
       
       
        SELECT @TotalRow = COUNT(*) FROM Supplier
          WHERE
                (@SupplierName IS NULL OR [SupplierName] LIKE @SupplierName) AND -- If passed only applies to filter
--                (SupplierCity = IsNull(@SupplierCity,SupplierCity)) AND
--                (SupplierCountry = IsNull(@SupplierCountry,SupplierCountry)) AND

                        (SupplierCity like @StringCity) AND
                (SupplierCountry like @StringCountry) AND
                (@SupplierEmail IS NULL OR SupplierEmail LIKE @SupplierEmail)
         
         PRINT Convert(varchar(30),@TotalRow) + ' - Total Rows'    
       
END


0
Comment
Question by:MadhuMenong
  • 3
  • 2
6 Comments
 
LVL 5

Accepted Solution

by:
buraksarica earned 500 total points
ID: 34210877
Did you define the selectMethod parameters in the object datasource definition? I can't see them.

<asp:ObjectDataSource ID="MyDataSource" runat="server" TypeName="OMS.DAL.SupplierBL"
        SelectCountMethod="SelectCountMethod" SelectMethod="SelectMethod" EnablePaging="true" StartRowIndexParameterName="StartRow" MaximumRowsParameterName="MaxRows">
        </asp:ObjectDataSource>

Open in new window

0
 
LVL 10

Expert Comment

by:P1ST0LPETE
ID: 34210966
I don't use the ObjectDataSource myself, but based off of the error message:

"ObjectDataSource 'MyDataSource' could not find a non-generic method 'SelectMethod' that has parameters: MaxRows, StartRow. "

It would appear that the compiler is looking for a method like this:

public void SelectMethod(int MaxRows, int StartRow)
{
      //Code.....
}

Since those are the only two parameters you define in your asp markup
0
 
LVL 5

Assisted Solution

by:buraksarica
buraksarica earned 500 total points
ID: 34211025
You need to define the parameters other than StartRow and MaxRows, for example :

<asp:ObjectDataSource ID="MyDataSource" runat="server" TypeName="OMS.DAL.SupplierBL"
        SelectCountMethod="SelectCountMethod" SelectMethod="SelectMethod" EnablePaging="true" StartRowIndexParameterName="StartRow" MaximumRowsParameterName="MaxRows">
<asp:QueryStringParameter Name="supplierName" QueryStringField="supplierName" Type="String" />
<asp:QueryStringParameter Name="supplierEmail" QueryStringField="supplierEmail" Type="String" />
<asp:QueryStringParameter Name="cityID" QueryStringField="cityID" Type="Int32" />
        </asp:ObjectDataSource>

Open in new window


Of course querystringparameter is just an example. You can use other types of parameters too.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:MadhuMenong
ID: 34211046
Hi buraksarica,

U R right i forgot to define the <selectParameters> and have done so as below:

 <asp:GridView ID="gvGrid" runat="server" AllowPaging="true" AllowSorting="false"
            PageSize="4" DataKeyNames="SupplierID" AutoGenerateColumns="false" CellPadding="2"
            DataSourceID="MyDataSource">
        </asp:GridView>
        <asp:ObjectDataSource ID="MyDataSource" runat="server" TypeName="OMS.DAL.SupplierBL"
            SelectCountMethod="SelectCountMethod" SelectMethod="SelectMethod" EnablePaging="true"
            StartRowIndexParameterName="StartRow" MaximumRowsParameterName="MaxRows">
            <SelectParameters>
                <asp:Parameter Name="StartRow" Type="Int32" />
                <asp:Parameter Name="MaxRows" Type="Int32" />
                <asp:ControlParameter ControlID="tbSupplierNames" Name="supplierName" PropertyName="Text" />
                 <asp:ControlParameter ControlID="tbSupplierEmailS" Name="supplierEmail" PropertyName="Text" />
                  <asp:ControlParameter ControlID="ddlCountryS" Name="countryID" PropertyName="SelectedValue" />
                  <asp:ControlParameter ControlID="ddlCityS" Name="cityID" PropertyName="SelectedValue" />
               
            </SelectParameters>
        </asp:ObjectDataSource>


But now i am faced with  a new problem.

____________________________________________________

SupplierDataSource  -- > below method

" totalRows = int.Parse(cmd1.Parameters["@TotalRows"].ToString());" at this line getting error "Input string not in correct format." . Can you advise please.

     public SupplierCollection SupplierSearch(Supplier sup, int startRowIndex, int maximumRows, ref int totalRows)
        {
           

            SupplierCollection suc = new SupplierCollection();
            Supplier su = new Supplier();

            try
            {
                using (SqlConnection con1 = new SqlConnection(connectionString))
                {
                    using (SqlCommand cmd1 = new SqlCommand("SupplierSearch", con1))
                    {
                        cmd1.CommandType = CommandType.StoredProcedure;

                        //cmd1.Parameters.AddWithValue("@SupplierID", sup.SupplierID);

                        cmd1.Parameters.AddWithValue("@SupplierName", sup.SupplierName);

                       // cmd1.Parameters.AddWithValue("@SupplierAddress1", sup.SupplierAddress1);

                        //cmd1.Parameters.AddWithValue("@SupplierAddress2", sup.SupplierAddress2);

                        cmd1.Parameters.AddWithValue("@SupplierCity", sup.SupplierCity);

                        cmd1.Parameters.AddWithValue("@SupplierCountry", sup.SupplierCountry);

                       // cmd1.Parameters.AddWithValue("@SupplierPinCode", sup.SupplierPinCode);

                        cmd1.Parameters.AddWithValue("@SupplierEmail", sup.SupplierEmail);

                        cmd1.Parameters.AddWithValue("@StartRowIndex", startRowIndex);

                        cmd1.Parameters.AddWithValue("@MaximumRows", maximumRows);

                        cmd1.Parameters.Add("@TotalRows", SqlDbType.Int);
                        cmd1.Parameters["@TotalRows"].Direction = ParameterDirection.Output;
                        cmd1.Parameters["@TotalRows"].Value = 0;

                        //cmd1.Parameters.AddWithValue("@TotalRows", 0);

                        SqlDataReader dr;

                        con1.Open();
                        dr = cmd1.ExecuteReader();

                        if (dr.HasRows)
                        {
                            while (dr.Read())
                            {
                                su = new Supplier();

                                su.SupplierID = dr.GetInt32(0);
                                su.SupplierName = dr.GetString(1);
                                su.SupplierAddress1 = dr.GetString(2);
                                su.SupplierAddress2 = dr.GetString(3);
                                su.SupplierCity = dr.GetInt32(4);
                                su.SupplierCountry = dr.GetInt32(5);
                                su.SupplierPinCode = dr.GetString(6);
                                su.SupplierEmail = dr.GetString(7);
                                su.SupplierTelephone = dr.GetString(8);

                                suc.Add(su);
                            }

                        }

                        totalRows = int.Parse(cmd1.Parameters["@TotalRows"].ToString());

                        con1.Close();

                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message + " -- " + ex.InnerException);
            }
            finally
            {
            }

            return suc;

        }
0
 

Author Closing Comment

by:MadhuMenong
ID: 34211142
Thanks  buraksarica.
0
 
LVL 5

Expert Comment

by:buraksarica
ID: 34211147
That's because you add the parameter as input parameter on this line,

cmd1.Parameters.AddWithValue("@TotalRow", 0);

You should add this parameter as an output parameter like :

SqlParameter parameter = cmd1.Parameters.Add(
  "@TotalRow", SqlDbType.Int);
parameter.Direction = ParameterDirection.Output;

then you should get the value correctly.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question