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>
:
<asp:TextBox ID="TextBoxMemberName" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Label1" runat="server" Text="Comments"></asp:Label>
:
<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