Wrox Programmer Forums
|
BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8
This is the forum to discuss the Wrox book Beginning ASP.NET 2.0 by Chris Hart, John Kauffman, David Sussman, Chris Ullman; ISBN: 9780764588501
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 31st, 2009, 03:54 PM
Registered User
 
Join Date: Sep 2009
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default InvalidCastException and IConvertible Error. Help Please.

I spent so long trying to fix the eror that I am having. Here are the errors and code. It is the Try out Page 279 - 283 related to uploading few fields from user input from Book Beginning ASP.NET 2.0 with C#.

I do not have problem with accessing data; however, this is something that I have no clue how to fix. It gives the erorr message when I try to insert the user data to sql database.

Here is the Code that I have on my Visual Studio 2008. and Thank in advance.
Code:
<%@ Page Title="" Language="C#" MasterPageFile="~/site.master" AutoEventWireup="true" CodeFile="GalleryUpload Enhanced.aspx.cs" Inherits="GalleryUpload" %>

<asp:Content ID="Content1" ContentPlaceHolderID="mainContent" Runat="Server">
    <asp:FileUpload ID="FileUpload1" runat="server" />
    <br />
    <asp:ListBox ID="ListBox1" runat="server" DataSourceID="SqlDataSourceFixtures" 
        DataTextField="FixtureDate" DataValueField="FixtureID"></asp:ListBox>
    <asp:SqlDataSource ID="SqlDataSourceFixtures" runat="server" 
        ConnectionString="<%$ ConnectionStrings:WroxUnitedMasterString %>" 
        SelectCommand="SELECT [FixtureID], [FixtureDate] FROM [Fixtures] ORDER BY [FixtureDate]">
    </asp:SqlDataSource>
    <br />
    <asp:Label ID="Label2" runat="server" Text="User Name"></asp:Label>
    :&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="TextBoxMemberName" runat="server"></asp:TextBox>
    <br />
    <asp:Label ID="Label1" runat="server" Text="Comments"></asp:Label>
    :&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="TextBoxNotes" runat="server"></asp:TextBox>
    <br />
    <asp:SqlDataSource ID="SqlDataSourceCreateGalleryRecord" runat="server" 
        ConnectionString="<%$ ConnectionStrings:WroxUnitedMasterString %>" 
       
        InsertCommand="INSERT INTO [Gallery] ([FixtureID], [UploadedByMemberName], [Notes], [PictureURL]) VALUES (@FixtureID, @UploadedByMemberName, @Notes, @PictureURL)" 
        SelectCommand="SELECT * FROM [Gallery]" 
        >
        <InsertParameters>
            <asp:ControlParameter Name="FixtureID" ControlID= "ListBox1" Type="Int32" />
            <asp:ControlParameter Name="UploadedByMemberName" ControlID="TextBoxMemberName" Type="String" />
            <asp:ControlParameter Name="Notes" ControlID="TextBoxNotes" Type="String" />
            <asp:ControlParameter Name="PictureURL" ControlID="FileUpload1" Type="String" />
        </InsertParameters>
    </asp:SqlDataSource>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Upload" />
    <br />
</asp:Content>
For the C# code behind. For some reason, FileUploadReport command does not exist?? so I had to comment it out in order to test.
Code:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;


public partial class GalleryUpload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string ImagesFolder = "MatchImages";
        string savePath;
        string saveFile;

        if (FileUpload1.HasFile)
        {
            try
            {
                //perform the upload
                savePath = Path.Combine(Request.PhysicalApplicationPath, ImagesFolder);
                saveFile = Path.Combine(savePath, FileUpload1.FileName);
                FileUpload1.SaveAs(saveFile);
                //Display status of success
                //FileUploadReport.Text = "Your file was uploaded successfully.";
            }
            catch (Exception exUpload)
            {
                //display status of error
                //FileUploadReport.Text = exUpload.Message;
            }
            SqlDataSourceCreateGalleryRecord.Insert();
        }
        else
        {
           
            //FileUploadReport.Text = "You did not specify a file to upload.";
        }
    }
}
Here is the Error on Windows Browser when I press F10 from Visual Studio.

Server Error in '/WroxUnitedCS3' Application.
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.

Source Error:

Line 35: //FileUploadReport.Text = exUpload.Message;
Line 36: }
Line 37: SqlDataSourceCreateGalleryRecord.Insert();
Line 38: }
Line 39: else


Source File: c:\Users\Young Kim\Documents\Visual Studio 2008\Wrox Projects\WroxUnitedCS3\GalleryUpload Enhanced.aspx.cs Line: 37

Stack Trace:

[InvalidCastException: Object must implement IConvertible.]
System.Convert.ChangeType(Object value, TypeCode typeCode, IFormatProvider provider) +2877157
System.Web.UI.WebControls.Parameter.GetValue(Objec t value, String defaultValue, TypeCode type, Boolean convertEmptyStringToNull, Boolean ignoreNullableTypeChanges) +141
System.Web.UI.WebControls.Parameter.GetValue(Objec t value, Boolean ignoreNullableTypeChanges) +63
System.Web.UI.WebControls.ParameterCollection.GetV alues(HttpContext context, Control control) +301
System.Web.UI.WebControls.SqlDataSourceView.Initia lizeParameters(DbCommand command, ParameterCollection parameters, IDictionary exclusionList) +264
System.Web.UI.WebControls.SqlDataSourceView.Execut eInsert(IDictionary values) +104
System.Web.UI.WebControls.SqlDataSource.Insert() +16
GalleryUpload.Button1_Click(Object sender, EventArgs e) in c:\Users\Young Kim\Documents\Visual Studio 2008\Wrox Projects\WroxUnitedCS3\GalleryUpload Enhanced.aspx.cs:37
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
System.Web.UI.WebControls.Button.RaisePostBackEven t(String eventArgument) +110
System.Web.UI.WebControls.Button.System.Web.UI.IPo stBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565


Version Information: Microsoft .NET Framework Version:2.0.50727.4927; ASP.NET Version:2.0.50727.4927
 
Old December 31st, 2009, 04:26 PM
Friend of Wrox
 
Join Date: Jun 2007
Posts: 477
Thanks: 10
Thanked 19 Times in 18 Posts
Default

I've haven't got a bead on the solution at this point. However, the error looks like it's grumpy about some sloppiness in your types (not an uncommon problem ;) ) It identifies line 37 (which may or may not be the problem) which states...

SqlDataSourceCreateGalleryRecord.Insert();

So when the app goes to insert SqlDataSourceCreateGalleryRecord the table(?) it's trying to stick it needs a different data type than SqlDataSourceCreateGalleryRecord. The error indicates that it's trying to implicitly convert from SqlDataSourceCreateGalleryRecord's type to the type it needs but SqlDataSourceCreateGalleryRecord is not convertible (because it doesn't implement IConvertible which makes such things possible).

My recommendation is to find out the type needed for the Insert() operation. Then you have two options.

1. (Preferred) Redefine the data types you need to in order for SqlDataSourceCreateGalleryRecord to match what you need.

2. (Not as good, but will work) Use

Code:
variableToCallTheInsertOperation = Convert.Int32(SqlDataSourceCreateGalleryRecord);
or if not Int32, then whatever datatype your Insert() operation needs.

It may seem like a small difference, but it's easier to only handle one data type vs. handling two and converting from one to the other. There are certainly cases where you have to which is why we have Convert, but ANYTHING you can do to reduce program complexity will speed overall development time and make maintenance infinitely easier. When you reduce program complexity in any way, it increases the ease of understanding the code and leaves vital brain cells free to think about more important things like what YOU want to do.
__________________
-------------------------

Whatever you can do or dream you can, begin it. Boldness has genius, power and magic in it. Begin it now.
-Johann von Goethe

When Two Hearts Race... Both Win.
-Dove Chocolate Wrapper

Chroniclemaster1, Founder of www.EarthChronicle.com
A Growing History of our Planet, by our Planet, for our Planet.
 
Old January 6th, 2010, 07:10 PM
Registered User
 
Join Date: Sep 2009
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I will try to see if I can convert to each type to see if they are working. Since I am kinda new to this, I might succeed or fail t.t. Thank you for your respond though.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Object must implement IConvertible. shwetarani2002 ASP.NET 1.0 and 1.1 Basics 0 June 25th, 2007 07:16 AM
IConvertible Error hoailing22 ASP.NET 1.0 and 1.1 Basics 5 March 16th, 2006 12:18 AM
IConvertible error problem geek.net ADO.NET 1 February 14th, 2006 06:44 PM
System.InvalidCastException QueryInterface Error MTCVS .NET Web Services 1 November 30th, 2005 01:21 PM
Stored procedure IConvertible nk15 SQL Server ASP 1 November 1st, 2004 03:22 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.