Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hello Sir,
I have written a code in which i write an update method. This update method takes parameters. When i assign this to the object datasource it gives me errors:

The code behind:


C#
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Collections;
using System.Collections.Generic;

/// <summary>
/// Summary description for Class1
/// </summary>
namespace bll
{
public class Class1
{
	public Class1()
	{
		//
		// TODO: Add constructor logic here
		//
	}

   // private static readonly _connection;
    private string _title;
    private int _id;

    public string Movie
    {
        get{return _title;}
        set{
         _title = value;
         
        }
    }

    public int id
    {
        get{
              if(_id < 0 )
             throw new Exception("Id should be greater than 0");
       
            return _id;}
       
        set{
       
           _id=value;

        }

    }


    public List<class1> getall()
    {
        List<class1> results = new List<class1>();
        SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\Practice1\App_Data\Database.mdf;Integrated Security=True;User Instance=True");
        con.StatisticsEnabled = true;
        SqlCommand cmd = new SqlCommand("sel",con);
        cmd.CommandType = CommandType.StoredProcedure;
        using(con)
        {
            con.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            while(reader.Read())
            {
                Class1 c = new Class1();
                c.id = (int)reader["Id"];
                c.Movie= (string)reader["Movie"];
                results.Add(c);

            }
          
                    return results;


        }

  

    }


    public void delete(int i)
    {
        SqlConnection sc = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\Practice1\App_Data\Database.mdf;Integrated Security=True;User Instance=True");
        SqlCommand cmd = new SqlCommand("delete Movies where Id=@Id");
        cmd.Parameters.AddWithValue("@Id",i);
        using(sc)
        {
            sc.Open();
            cmd.ExecuteNonQuery();
        }


    }




    public void update(int i, string mov)
    {
        SqlConnection sc = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\Practice1\App_Data\Database.mdf;Integrated Security=True;User Instance=True");
        SqlCommand cmd = new SqlCommand("update Movies set Movie=@Movie where Id=@Id",sc);
        cmd.Parameters.AddWithValue("@Movie",mov);
        cmd.Parameters.AddWithValue("@Id",i);
        using (sc)
        {
            sc.Open();
            cmd.ExecuteNonQuery();

        }

    }



}
}


The .aspx page :

ASP.NET
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView  AutoGenerateEditButton="true" AutoGenerateDeleteButton="true"  DataSourceID="obj" ID="gv"  runat="server" />
    <asp:ObjectDataSource ID="obj" TypeName="bll.Class1" SelectMethod="getall" UpdateMethod="update" DeleteMethod="delete" runat="server" />
    </div>
    </form>
</body>
</html>


After running this the error is : This happens when i try to update any field:
ObjectDataSource 'obj' could not find a non-generic method 'update' that has parameters: Movie, id.
Posted
Updated 14-Oct-13 18:12pm
v2
Comments
MTProgrammer 17-Oct-13 18:44pm    
If you havent already figured this out, I would try to change your parameter names from i, mov in the Update method to coincide with the parameter names your Class1 property names (id, movie) and try again.
Rahul VB 25-Nov-13 22:40pm    
Thanks sir

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900