Friday, March 23, 2012

FileUpload control and MS SQL

I am trying to use the FileUpload control to save a filename to a SQL database. I'm using the OnUpdating event for my SQLDataSource to add the filename to the UpdateParameters. The OnUpdating event is firing but the FileName doesn't get added to the database. Any pointers as to why that might be would be very helpful.

Here's the code. Thanks much.

<%

@.PageLanguage="VB"MasterPageFile="~/MasterPage.master"Title="File Upload Testing" %>

<

scriptrunat="server">Sub Page_Load()

Response.Write(

"UpdateParameters(0)=" & dsLabel.UpdateParameters(0).Name &"<br />")

Response.Write(

"UpdateParameters(1)=" & dsLabel.UpdateParameters(1).Name &"<br />")EndSubSub dsLabel_OnUpdating(ByVal senderAsObject,ByVal eAs System.Web.UI.WebControls.SqlDataSourceCommandEventArgs)Dim FileUploadControlAs FileUpload = DetailsView1.FindControl("FileUpload1")Dim strFileNameAsString = FileUploadControl.FileName

dsLabel.UpdateParameters(0).DefaultValue = strFileName

dsLabel.UpdateParameters(1).DefaultValue = Request.QueryString(

"rgstnID")

Response.Write(

"dsLabel.UpdateParameters(0).DefaultValue = " & dsLabel.UpdateParameters(0).DefaultValue &"<br />")

Response.Write(

"dsLabel.UpdateParameters(1).DefaultValue = " & dsLabel.UpdateParameters(1).DefaultValue &"<br />")EndSub

</

script>

<

asp:ContentID="Content1"ContentPlaceHolderID="ContentPlaceHolder1"Runat="Server">

<

h1>File Upload</h1>

This page should allow me to use the FileUpload Control to save a filename to an MS SQL Database.

<br/><br/> <asp:DetailsViewID="DetailsView1"runat="server"AutoGenerateRows="False"DataSourceID="dsLabel"Height="50px"Width="125px"><Fields><asp:TemplateFieldHeaderText="File"SortExpression="labelName"><EditItemTemplate><asp:FileUploadID="FileUpload1"runat="server"/></EditItemTemplate><ItemTemplate><asp:LabelID="Label1"runat="server"Text='<%# Bind("labelName") %>'></asp:Label></ItemTemplate></asp:TemplateField><asp:CommandFieldButtonType="Button"ShowEditButton="True"/></Fields></asp:DetailsView><br/><br/><asp:SqlDataSourceID="dsLabel"runat="server"ConnectionString="Data Source=FILESERVER1;Initial Catalog=IM;Integrated Security=True"OnUpdating="dsLabel_OnUpdating"ProviderName="System.Data.SqlClient"SelectCommand="SELECT labelName FROM tblProductRegistration WHERE (registrationID = @.registrationID)"UpdateCommand="UPDATE tblProductRegistration SET labelName = @.LabelName WHERE (registrationID = @.registrationID)"><UpdateParameters><asp:ParameterConvertEmptyStringToNull="True"Name="LabelName"/><asp:QueryStringParameterType="Int64"Name="registrationID"QueryStringField="rgstnID"/></UpdateParameters><SelectParameters><asp:QueryStringParameterName="registrationID"QueryStringField="rgstnID"/></SelectParameters></asp:SqlDataSource>

</

asp:Content>Everything in the code seems to be ok. The code is to update an existing row in database but not insert. So make sure you have a rowwith the rgstnID in your databasae when updating.|||

Thanks very much for the quick reply,Iori_Jay. That's what's puzzling me, I only want to support UPDATE and the code looks good to me. Am I missing anything that would cause the Updating event to fire but prevent the UPDATE command from actually executing?

Thanks.

Charlie

No comments:

Post a Comment