Guids are not very guid! Updating in DetailsView,FormView (Object must implement IConvertible)

Last post 11-15-2009 7:43 AM by pliant. 28 replies.

Sort Posts:

  • Guids are not very guid! Updating in DetailsView,FormView (Object must implement IConvertible)

    12-01-2005, 12:00 PM
    • Member
      32 point Member
    • rellymr
    • Member since 11-30-2005, 3:55 PM
    • Glasgow
    • Posts 10
    I've been playing around with the new data controls (DetailsView,FormView) and have been having problems when attempting to update a record that has a uniqueidentifier as its primary key.

    I get the error message:

    Object must implement IConvertible.

    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.InvalidCastException: Object must implement IConvertible.

    I gather this is because
    there is a bug that has propagated from the beta version.

    One suggested work around is from
    http://64.233.183.104/search?q=cache:GDjA62POtgcJ:scottonwriting.net/sowBlog/archive/11162005.aspx+Implicit+conversion+from+data+type+sql_variant+to+uniqueidentifier+is+not+allowed.+Use+the+CONVERT+function+to+run+this+query.&hl=en

    The crux of the problem, it appears, is that the <asp:Parameter> value for the uniqueidentifier field is, by default, set to Type=”Object”. To fix this, simply remove the Type property altogether. That is, change the SqlDataSource parameter setting from something like:

    <asp:SqlDataSource ...>
      <InsertParameters>
        <asp:Parameter Name=”UserId” Type=”Object” />
        ...
      </InsertParameters>
    </asp:SqlDataSource>

    to:

    <asp:SqlDataSource ...>
      <InsertParameters>
        <asp:Parameter Name=”UserId”  />
        ...
      </InsertParameters>
    </asp:SqlDataSource>

    This change worked for me; once the Type was removed the exception ceased and the updates/inserts worked as expected.

    Unfortunately this only partially worked for me as while it is fine for deletes it won't work for updates.

    If anyone can help shed any light on this I would greatly appreciate it.

    Cheers

    Mark
  • Re: Guids are not very guid! Updating in DetailsView,FormView (Object must implement IConvertible)

    12-01-2005, 12:18 PM
    • Star
      12,857 point Star
    • Motley
    • Member since 10-14-2005, 5:26 PM
    • West Chicago, IL
    • Posts 2,297
    Try Type Empty.
  • Re: Guids are not very guid! Updating in DetailsView,FormView (Object must implement IConvertible)

    12-02-2005, 3:51 AM
    • Member
      32 point Member
    • rellymr
    • Member since 11-30-2005, 3:55 PM
    • Glasgow
    • Posts 10

    Unfortunately that didn't work.

    Its just on the update I get the problem for some reason the delete and insert work fine.

    My update stored procedure works fine on its own and the html fpr my sql datasource looks fine. (below)

    <asp:SqlDataSource ID="sqlDSPotentialClients" runat="server" ProviderName="System.Data.SqlClient" ConnectionString="Data Source=STOCKHOLM\SQLSERVER2005;Initial Catalog=MembershipDemo;Integrated Security=True"

    DeleteCommand="usp_tbl_potential_clients_delete" DeleteCommandType="StoredProcedure"

    InsertCommand="usp_tbl_potential_clients_insert" InsertCommandType="StoredProcedure"

    SelectCommand="usp_tbl_potential_clients_select_all" SelectCommandType="StoredProcedure"

    UpdateCommand="usp_tbl_potential_clients_update" UpdateCommandType="StoredProcedure" >

    <DeleteParameters>

    <asp:ControlParameter ControlID="DetailsView" Name="id" PropertyName="SelectedValue" Type="Empty"/>

    </DeleteParameters>

    <UpdateParameters>

    <asp:ControlParameter ControlID="DetailsView" Name="id" PropertyName="SelectedValue" Type="Empty"/>

    <asp:ControlParameter ControlID="DetailsView" Name="company_name" PropertyName="SelectedValue" Type="String" />

    <asp:ControlParameter ControlID="DetailsView" Name="trading_as" PropertyName="SelectedValue" Type="String" />

    <asp:ControlParameter ControlID="DetailsView" Name="contact_title" PropertyName="SelectedValue" Type="Int32" />

    <asp:ControlParameter ControlID="DetailsView" Name="contact_name" PropertyName="SelectedValue" Type="String" />

    <asp:ControlParameter ControlID="DetailsView" Name="address1" PropertyName="SelectedValue" Type="String" />

    <asp:ControlParameter ControlID="DetailsView" Name="address2" PropertyName="SelectedValue" Type="String" />

    <asp:ControlParameter ControlID="DetailsView" Name="address3" PropertyName="SelectedValue" Type="String" />

    <asp:ControlParameter ControlID="DetailsView" Name="address4" PropertyName="SelectedValue" Type="String" />

    <asp:ControlParameter ControlID="DetailsView" Name="postcode" PropertyName="SelectedValue" Type="String" />

    <asp:ControlParameter ControlID="DetailsView" Name="phone" PropertyName="SelectedValue" Type="String" />

    <asp:ControlParameter ControlID="DetailsView" Name="fax" PropertyName="SelectedValue" Type="String" />

    <asp:ControlParameter ControlID="DetailsView" Name="lead" PropertyName="SelectedValue" Type="Boolean" />

    </UpdateParameters>

    <InsertParameters>

    <asp:ControlParameter ControlID="DetailsView" Name="company_name" PropertyName="SelectedValue" Type="String" />

    <asp:ControlParameter ControlID="DetailsView" Name="trading_as" PropertyName="SelectedValue" Type="String" />

    <asp:ControlParameter ControlID="DetailsView" Name="contact_title" PropertyName="SelectedValue" Type="Int32" />

    <asp:ControlParameter ControlID="DetailsView" Name="contact_name" PropertyName="SelectedValue" Type="String" />

    <asp:ControlParameter ControlID="DetailsView" Name="address1" PropertyName="SelectedValue" Type="String" />

    <asp:ControlParameter ControlID="DetailsView" Name="address2" PropertyName="SelectedValue" Type="String" />

    <asp:ControlParameter ControlID="DetailsView" Name="address3" PropertyName="SelectedValue" Type="String" />

    <asp:ControlParameter ControlID="DetailsView" Name="address4" PropertyName="SelectedValue" Type="String" />

    <asp:ControlParameter ControlID="DetailsView" Name="postcode" PropertyName="SelectedValue" Type="String" />

    <asp:ControlParameter ControlID="DetailsView" Name="phone" PropertyName="SelectedValue" Type="String" />

    <asp:ControlParameter ControlID="DetailsView" Name="fax" PropertyName="SelectedValue" Type="String" />

    <asp:ControlParameter ControlID="DetailsView" Name="lead" PropertyName="SelectedValue" Type="Boolean" />

    </InsertParameters>

    </asp:SqlDataSource>

    I've traced it in the sql profiler and the update doesn't get that far so its not a database issue.

    Any help would be great as I am stumped.

    Mark

  • Re: Guids are not very guid! Updating in DetailsView,FormView (Object must implement IConvertible)

    12-06-2005, 3:50 AM
    • Contributor
      5,653 point Contributor
    • Eilon
    • Member since 06-26-2002, 6:14 PM
    • Redmond, WA
    • Posts 965

    I think the issue might partially be a bug in the SqlDataSource control (someone else founds it not too long ago, if I recall).

    I'm pretty sure that the workaround was to handle the "pre" event for the operation (e.g. Updating) and set the DbType manually:

    e.Command.Parameters["myguidparam"].DbType = DbType.Guid;

    And that solves the problem. It's unfortunate that the TypeCode enum (which is what Parameter uses for its Type property) doesn't have a Guid value.

    Thanks,

    Eilon

    Blog: http://weblogs.asp.net/LeftSlipper/
  • Re: Guids are not very guid! Updating in DetailsView,FormView (Object must implement IConvertible)

    12-06-2005, 6:26 AM
    • Member
      32 point Member
    • rellymr
    • Member since 11-30-2005, 3:55 PM
    • Glasgow
    • Posts 10

    Thanks for the suggestion Eilon.

    Unfortunately the DetailsViewUpdateEventArgs (and FormViewUpdateEventArgs) don't contain a definition for command. Using the following code

    protected void DetailsView_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)

    {

    e.Command.Parameters["id"].DbType = DbType.Guid;

    }

    I get the error below:

    Error 387 'System.Web.UI.WebControls.DetailsViewUpdateEventArgs' does not contain a definition for 'Command'

    It looks like the DetailsViewUpdateEventArgs (and FormViewUpdateEventArgs) only allow Get access to the public properties.

    Any other suggestions would be greatly appreciated.

    Regards

    Mark

     

  • Re: Guids are not very guid! Updating in DetailsView,FormView (Object must implement IConvertible)

    12-06-2005, 7:29 PM
    • Contributor
      5,653 point Contributor
    • Eilon
    • Member since 06-26-2002, 6:14 PM
    • Redmond, WA
    • Posts 965

    Sorry about that, I should have been more specific. I meant to handle the data source's Updating event (SqlDataSource.Updating), not the data bound control.

     

    Thanks,

    Eilon

    Blog: http://weblogs.asp.net/LeftSlipper/
  • Re: Guids are not very guid! Updating in DetailsView,FormView (Object must implement IConvertible)

    12-07-2005, 3:55 AM
    • Member
      32 point Member
    • rellymr
    • Member since 11-30-2005, 3:55 PM
    • Glasgow
    • Posts 10

    Thanks for the reply Eilon,

    Unfortunately that still doesn't work Sad [:(].

    I added the event to the sqldatasource as you suggested.

    protected void sqlDSPotentialClients_Updating(object sender, SqlDataSourceCommandEventArgs e)

    {

    e.Command.Parameters["id"].DbType = DbType.Guid;

    }

    And I put a breakpoint on it but the exception throws before the breakpoint is hit.

    I've attached the stack trace below

    [InvalidCastException: Object must implement IConvertible.]
       System.Convert.ChangeType(Object value, TypeCode typeCode, IFormatProvider provider) +2562898
       System.Web.UI.WebControls.Parameter.GetValue(Object value, String defaultValue, TypeCode type, Boolean convertEmptyStringToNull, Boolean ignoreNullableTypeChanges) +257
       System.Web.UI.WebControls.Parameter.get_ParameterValue() +92
       System.Web.UI.WebControls.ParameterCollection.GetValues(HttpContext context, Control control) +282
       System.Web.UI.WebControls.SqlDataSourceView.InitializeParameters(DbCommand command, ParameterCollection parameters, IDictionary exclusionList) +344
       System.Web.UI.WebControls.SqlDataSourceView.ExecuteUpdate(IDictionary keys, IDictionary values, IDictionary oldValues) +451
       System.Web.UI.DataSourceView.Update(IDictionary keys, IDictionary values, IDictionary oldValues, DataSourceViewOperationCallback callback) +179
       System.Web.UI.WebControls.DetailsView.HandleUpdate(String commandArg, Boolean causesValidation) +1199
       System.Web.UI.WebControls.DetailsView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +545
       System.Web.UI.WebControls.DetailsView.OnBubbleEvent(Object source, EventArgs e) +162
       System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +56
       System.Web.UI.WebControls.DetailsViewRow.OnBubbleEvent(Object source, EventArgs e) +117
       System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +56
       System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +106
       System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +175
       System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +31
       System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +32
       System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +242
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3840

    If you have any further ideas that would be great.

    Cheers

    Mark


  • Re: Guids are not very guid! Updating in DetailsView,FormView (Object must implement IConvertible)

    02-06-2006, 5:26 PM
    • Contributor
      2,716 point Contributor
    • gsuttie2002
    • Member since 09-07-2002, 10:17 AM
    • Scotland, UK
    • Posts 579

    Hi Eilon

    Is there any resoluton to this problem above?

    Thanks
    Gregor

    Gregor Suttie
    MCSD, MCAD, MCSD.Net
  • Re: Guids are not very guid! Updating in DetailsView,FormView (Object must implement IConvertible)

    02-26-2006, 5:02 PM
    • Contributor
      2,716 point Contributor
    • gsuttie2002
    • Member since 09-07-2002, 10:17 AM
    • Scotland, UK
    • Posts 579

    Any update on this Eilon?

    Ta
    Gregor

    Gregor Suttie
    MCSD, MCAD, MCSD.Net
  • Re: Guids are not very guid! Updating in DetailsView,FormView (Object must implement IConvertible)

    02-27-2006, 12:29 AM
    • Star
      12,857 point Star
    • Motley
    • Member since 10-14-2005, 5:26 PM
    • West Chicago, IL
    • Posts 2,297
    Instead of DBType.Guid, try sqldbtype.uniqueidentifier
  • Re: Guids are not very guid! Updating in DetailsView,FormView (Object must implement IConvertible)

    02-27-2006, 9:25 PM
    • Member
      25 point Member
    • bdiaz
    • Member since 02-28-2006, 2:16 AM
    • Houston, TX
    • Posts 5
    Have you tried something like this?

        protected void Page_Load(object sender, EventArgs e)
        {
            FormView1.ItemUpdating += new FormViewUpdateEventHandler(FormView1_ItemUpdating);
        }

        protected void FormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e)
        {
            IOrderedDictionary values = e.NewValues;
            String key = "id";

            if ( values.Contains(key) )
            {
                String id = String.Format("{0}", values[key]);
                values.Remove(key);
                values.Add(key, new Guid(id));
            }
        }

  • Re: Guids are not very guid! Updating in DetailsView,FormView (Object must implement IConvertible)

    03-10-2006, 5:28 PM
    • Member
      588 point Member
    • gtjr921
    • Member since 10-27-2004, 11:22 AM
    • Cincinnati
    • Posts 187
    did this get resolved I am having the same issue
  • Re: Guids are not very guid! Updating in DetailsView,FormView (Object must implement IConvertible)

    03-11-2006, 9:27 AM
    • Contributor
      2,716 point Contributor
    • gsuttie2002
    • Member since 09-07-2002, 10:17 AM
    • Scotland, UK
    • Posts 579

    Nope I'm afraid not - was hoping it would have by now.

    Cheers
    Gregor

    Gregor Suttie
    MCSD, MCAD, MCSD.Net
  • Re: Guids are not very guid! Updating in DetailsView,FormView (Object must implement IConvertible)

    03-16-2006, 4:07 PM
    • Member
      25 point Member
    • bdiaz
    • Member since 02-28-2006, 2:16 AM
    • Houston, TX
    • Posts 5
    Gregor

    Did you happen to try what I suggested on 27-Feb-2006?

    Bobby
  • Re: Guids are not very guid! Updating in DetailsView,FormView (Object must implement IConvertible)

    04-11-2006, 3:26 AM
    • Member
      10 point Member
    • Hojez
    • Member since 04-02-2006, 1:49 AM
    • Posts 2

    i got the result.

    that is uniqueidentifier is a STRING Type.

    when you try  to change that parameter like <Parameter Name=***, Type= String >

     

    it must work.

     

    :D

     

    But one problem i comes that is , in sql 2000, it shows that there is only a typ called bit

    But in VS2005,there is a type called boolean.,

     

    THey cannot change to each other. and i faced the same caution that Object must implement IConvertible

     

    。。。。。。

    So, who can solve? 

Page 1 of 2 (29 items) 1 2 Next >