Solved

Gridview, objectdatasource update error message

Posted on 2008-10-01
12
1,862 Views
Last Modified: 2013-12-17
Hello

I'm having a hard time figuring out a problem I have been stuck with for two days now.
I'm developing a web project which contains a gridview, that should have a edit/update function.

First off I have have a method that returns a datatable that varies in size from time to time. Getting data from different database tables.
This method is the select method for the datasource, and it works fine.

Then I've added a Command field: Edit, Update, Cancel to the gridview and created a simple test update method that just change the text of a label on the page.
Added this update method to the datasource, but just cant get this to work.
On the datasource, the DataObjectTypeName is set to: System.Web.UI.WebControls.GridViewRow

When I click on the update link, the following error message is produced:
No parameterless constructor defined for this object.
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.MissingMethodException: No parameterless constructor defined for this object.

I haven't really worked that much with gridviews and datasources, but how does the update method get the correct row, the one that are being edited?
And I guess this is related to my problem?

Regards
Marius

My code:

//Update method

public void UpdateGridView(GridViewRow test)

{

            Label1.Text = "Updated! ";           

}
 

//The gridview and datasource

<asp:GridView ID="gwSelectedForm" runat="server" DataSourceID="ObjectDataSource1" 

        onrowcreated="gwSelectedForm_RowCreated" 

        onrowupdating="gwSelectedForm_RowUpdating">

    <Columns>

        <asp:CommandField ShowEditButton="True" />

    </Columns>

</asp:GridView>
 

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 

    SelectMethod="SetGridView" TypeName="LookupEdit.Home" 

    DataObjectTypeName="System.Web.UI.WebControls.GridViewRow" 

    UpdateMethod="UpdateGridView">

    <SelectParameters>

        <asp:ControlParameter ControlID="ddlFormAccess" DefaultValue="" Name="lookup" 

            PropertyName="SelectedValue" Type="String" />

    </SelectParameters>

</asp:ObjectDataSource>

Open in new window

0
Comment
Question by:Contestoas
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 15

Assisted Solution

by:NazoUK
NazoUK earned 350 total points
ID: 22621812
The Gridview will automatically pass the values from the row being edited to your update method. It can do this in one of 2 ways: If you specify a dataobjecttypename it will try to create an object of that type using the values in the row being edited. It will then pass this object as a parameter to your updatemethod. This is used when you are binding the gridview to a collection of business objects.
If you don't specify a dataobjecttypename the object datasource will try to pass each value from the gridview row as a parameter to your updatemethod.

In your case as you are using a datatable, I recommend you don't use the dataobjecttypename property.
The UpdateMethod should be in the same class as your SelectMethod: LookupEdit.Home
You should also add the name of the primary key for the row to the DataKeyValues attribute of the gridview, this way the primary key will be passed as a parameter to the update method also.

An an example, let's say your table had 2 fields: a primary key called ID which is an integer, and a string field called Name

Your update method must look something like:
public void update(int ID, string Name)
{
..
}

You can of course make the method static and have a return value if you want but the paramters must match those from your gridview.
0
 

Author Comment

by:Contestoas
ID: 22621842
Hi
Thank you for answering.

My problem is that the parameters for the update is not always the same.
It can vary for each time. Thats why I trie to send in the row.
Is there a way to check how many parameters there is, and create them on the fly?

I've added the primary key to the DataKeyValues in the gridview.

Still I dont understand why the error message is produced.

Regards
Marius
0
 
LVL 21

Assisted Solution

by:silemone
silemone earned 50 total points
ID: 22621896
just use a simple if/else statement to check if the value coming in for a parameter is null or string.empty... if so, then you have to accomodate for that, but you can't just stop passing parameters if parameters are expected and also, if 10 parameters are needed then you must always pass 10 parameters.  so make your dataset ALWAYS take every parameter that's needed...but if a certain condition is met, just make the parameter = DBNull.Value or String.Empty...(DBNull.Value is better since if it's a date, etc...it will work as String.Empty is only for strings).  Then you can dynamically build your sql statement (filter dynamically) in MSSQL...(maybe use isNull or @sqlQueryBuilder where based on some conditions, you build your sqlstatement)

Only other way is to create many datasources that take different parameter and based on how many parameters are being passed, dynamically choose which dataset/datasource you will use (the long way...);
0
 
LVL 13

Assisted Solution

by:SameerJagdale
SameerJagdale earned 100 total points
ID: 22621915
well, if you are working with objectdatasource, your class will hold methods for update, select, etc and when you configure the datasource, you have to select the appropiate methods.
this is my class declaration:

namespace LookupEdit
{
/// <summary>
/// Summary description for Class1
/// </summary>
public class Home
{
public Home()
{
//
// TODO: Add constructor logic here
//
}
private int _id;
public int Id
{
get { return _id; }
set { _id = value; }
}
 
private string _road;
public string Road
{
get { return _road; }
set { _road = value; }
}
private string _address;
public string Address
{
get { return _address; }
set { _address = value; }
}
public bool UpdateMethod(int id,string road, string address)
{
//code
return true;
}
public Home GetHome(int id)
{
//
return new Home();
}
}
}

<asp:GridView ID="gwSelectedForm" runat="server">

            </asp:GridView>

            <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" TypeName="LookupEdit.Home" SelectMethod="GetHome" UpdateMethod="UpdateMethod">

                <UpdateParameters>

                    <asp:Parameter Name="id" Type="Int32" />

                    <asp:Parameter Name="road" Type="String" />

                    <asp:Parameter Name="address" Type="String" />

                </UpdateParameters>

                <SelectParameters>

                    <asp:Parameter DefaultValue="1" Name="id" Type="Int32" />

                </SelectParameters>

            </asp:ObjectDataSource>

Open in new window

0
 

Author Comment

by:Contestoas
ID: 22621945
So it all comes down to that I simply just can't send the gridviewrow in as a parameter?
Is there maybe a way to override the objectDataSource update method?
0
 
LVL 13

Assisted Solution

by:SameerJagdale
SameerJagdale earned 100 total points
ID: 22621964
well you can very well override the method to accept all parameters, you can then pass the values using row. something like row.id,row.name,etc.
 
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:Contestoas
ID: 22622006
all this help is great, I understand now better how the ObjectDataSource/GridView works.
But I still dont understand why I get the error message.
I have a constructor in my class:
public Home()
{
}
I might add that the class is a public partial class Home : System.Web.UI.Page.
But I've also tried connecting the datasource to a Test.cs class with the same methods, and I get the same error message.
0
 
LVL 15

Accepted Solution

by:
NazoUK earned 350 total points
ID: 22622105
The problem is that by using DataObjectTypeName=GridViewRow you are telling the objectdatasource that it should create a GridViewRow that has the values contained in your row.

Take for example if I used a DataObjectTypeName="Person" to represent my class of person:

Public Class Person
{
      public int ID {get;set;}
      public string Name {get;set;}
}

the ObjectDataSource would first create an instance of my class, then assign the values from the row to it, equivalent of:

Person obj = new Person()
obj.ID = ID value from Row
obj.Name = Name value from Row

and so on. Now if I use my class to fill out the gridview but use GridViewRow as the DataObjectTypeName then it will try to do the following:

GridViewRow obj = new GridViewRow() <--- Can't do because there isn't a constructor with no parameters
obj.ID = ID value from Row
obj.Name = Name value from Row

obviously this doesn't make any sense because the gridViewRow doesn't have the properties that your class does.

In other words, you CAN'T use GridViewRow as a DataObjectTypeName
0
 
LVL 15

Assisted Solution

by:NazoUK
NazoUK earned 350 total points
ID: 22622114
Also, if you don't know the columns that will be there at design time, how do you intend to do the update?
0
 

Author Comment

by:Contestoas
ID: 22622128
Thank you, I now understand why the error occures.
I'm programming up against an API which have methods that will handle the update, so I just need to get the changed values.
0
 
LVL 15

Assisted Solution

by:NazoUK
NazoUK earned 350 total points
ID: 22622200
From the sound of it you'd be better off handling the update manually than using an objectdatasource update method then.

The arguments for the onrowupdating event of the gridview allows you to see the old values and new values of each item. See here for more info:

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewupdateeventargs.aspx
0
 

Author Comment

by:Contestoas
ID: 22622265
Yeah, I guess so :(
Anyway, thanks for all your help people:)

Regards
Marius
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now