?
Solved

Gridview, objectdatasource update error message

Posted on 2008-10-01
12
Medium Priority
?
1,892 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 15

Assisted Solution

by:NazoUK
NazoUK earned 1400 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 200 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 13

Assisted Solution

by:SameerJagdale
SameerJagdale earned 400 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 400 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
 

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 1400 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 1400 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 1400 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

765 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