Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1912
  • Last Modified:

Gridview, objectdatasource update error message

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
Contestoas
Asked:
Contestoas
  • 5
  • 4
  • 2
  • +1
7 Solutions
 
NazoUKCommented:
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
 
ContestoasAuthor Commented:
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
 
silemoneCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
SameerJagdaleCommented:
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
 
ContestoasAuthor Commented:
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
 
SameerJagdaleCommented:
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
 
ContestoasAuthor Commented:
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
 
NazoUKCommented:
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
 
NazoUKCommented:
Also, if you don't know the columns that will be there at design time, how do you intend to do the update?
0
 
ContestoasAuthor Commented:
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
 
NazoUKCommented:
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
 
ContestoasAuthor Commented:
Yeah, I guess so :(
Anyway, thanks for all your help people:)

Regards
Marius
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 5
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now