• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1745
  • Last Modified:

ASP.NET FileUpload Control and VB.NET

I am having some weird behavior with a file upload control. It is not recognizing when a file is added to the FileUpload. When I browse for a file or type in a physical path, I get the message back ... You have not specified a file.

The FileUpload1.HasFile is evaluating to False, even when I browse and a file name is in the box or if I manually type in a file path.

Any ideas?
Thanks,
Joe
ASCX
-------------------
<asp:FileUpload ID="FileUpload1" runat="server" />
            &nbsp;
            <asp:LinkButton ID="LinkButton1" runat="server">Upload</asp:LinkButton>
            &nbsp;
<asp:Label ID="Label1" runat="server"></asp:Label>
 
--------------------------------
VB.NET Code behind
 
--------------------------------
 
    Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton1.Click
 
        If FileUpload1.HasFile Then
            Try
                FileUpload1.SaveAs("C:\Uploads\" & _
                   FileUpload1.FileName)
                Label1.Text = "File name: " & _
                   FileUpload1.PostedFile.FileName & "<br>" & _
                   "File Size: " & _
                   FileUpload1.PostedFile.ContentLength & " kb<br>" & _
                   "Content type: " & _
                   FileUpload1.PostedFile.ContentType
            Catch ex As Exception
                Label1.Text = "ERROR: " & ex.Message.ToString()
            End Try
        Else
            Label1.Text = "You have not specified a file."
        End If
 
    End Sub
--------------------------------

Open in new window

0
NursingCorp
Asked:
NursingCorp
  • 8
  • 8
  • 2
  • +1
1 Solution
 
NursingCorpAuthor Commented:
When I remove the If statement, I get the following:

ERROR: Object reference not set to an instance of an object.
0
 
amar31282Commented:
It is working fine at my end

The only thing to note is this that the path C:\Uploads\ should exist physically.

Means uploads folder should exist at c drive
0
 
amar31282Commented:
put a break point at

 If FileUpload1.HasFile Then

and see if the processing goes inside "if" or not
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
mahadevan_vCommented:
Try this

 FileUpload1.PostedFile.SaveAs("C:\Uploads\" & _
                   FileUpload1.FileName)
0
 
mahadevan_vCommented:
Try this sample code..
string file = @"C:\uploads" + FileUpload1.PostedFile.FileName.Substring(FileUpload1.PostedFile.FileName.LastIndexOf(@"\"));
        FileUpload1.PostedFile.SaveAs(file);

This should work

U can use Server.Mappath () instead of using physical path..
0
 
NursingCorpAuthor Commented:
Thanks for the suggestions. The physical path C:\Uploads does exists.

I tried FileUpload1.PostedFile.SaveAs("C:\Uploads\" & _
                   FileUpload1.FileName)

but still getting ERROR: Object reference not set to an instance of an object. when I remove the if statement.
0
 
amar31282Commented:
use ID:24068224 and see if it is going inside the condition or not.

if and see if the exception is occurring then what is the exception
0
 
NursingCorpAuthor Commented:
There are Rad Ajax controls on the page. I think that it is not completing a full postback and that is why FileUpload1.HasFile is evaluating to False
0
 
amar31282Commented:
try putting EnableViewState="true" inside the FileUpload1
0
 
NursingCorpAuthor Commented:
It works on a page by itself, just not on the page with the Rad Ajax controls.
0
 
NursingCorpAuthor Commented:
I put EnableViewState="true" inside FileUpload1 and it still give me ERROR: Object reference not set to an instance of an object.
0
 
amar31282Commented:
please post the aspx code.please tell On which line you are getting this error
0
 
amar31282Commented:
I am sure the error is not because of fileupload it is because of some other control left uninitialized
0
 
NursingCorpAuthor Commented:
It is .ascx (building a DNN module)
<%@ Control Language="VB" AutoEventWireup="false" CodeFile="Main.ascx.vb" Inherits="DesktopModules_AddFacility_Main" %>
<%@ Register assembly="Telerik.Web.UI" namespace="Telerik.Web.UI" tagprefix="telerik" %>
 
 
<telerik:RadTabStrip ID="RadTabStrip1" runat="server" 
    MultiPageID="RadMultiPage1" SelectedIndex="4" Width="800px" 
    Skin="Default2006">
    <Tabs>
        <telerik:RadTab runat="server" Text="General Information">
        </telerik:RadTab>
        <telerik:RadTab runat="server" Text="Contact Information">
        </telerik:RadTab>
        <telerik:RadTab runat="server" Text="Facility Bill Rates">
        </telerik:RadTab>
        <telerik:RadTab runat="server" Text="Facility Pay Rates">
        </telerik:RadTab>
        <telerik:RadTab runat="server" Text="Customer Notes" Selected="True">
        </telerik:RadTab>
    </Tabs>
</telerik:RadTabStrip>
<telerik:RadMultiPage ID="RadMultiPage1" Runat="server" SelectedIndex="4">
    <telerik:RadPageView ID="RadPageView1" runat="server" Width="800px">
        <table width="800" border="0" cellpadding="6" cellspacing="0">
        <tr><td colspan="4">
            <br /></td></tr>
       
        <tr>
        <td width="150"><span class="Normal">
            <b>Customer Name:</b></span></td>
        <td width="250"> 
            <telerik:RadTextBox ID="customerName" Runat="server" Skin="Default2006" 
                Width="200px" MaxLength="49">
            </telerik:RadTextBox>
            </td>
        <td width="150"> </td>
        <td width="250"> </td>
        </tr>
        
        <tr>
        <td width="150" valign="top"><span class="Normal">Physical Address:</span></td>
        <td width="250"> 
            <telerik:RadTextBox ID="physicalAddress" Runat="server" Skin="Default2006" 
                Width="200px">
            </telerik:RadTextBox> 
            <br /
            <telerik:RadTextBox ID="physicalAddress2" Runat="server" Skin="Default2006" 
                Width="200px">
            </telerik:RadTextBox>
            </td>
        <td width="150" valign="top" bgcolor="#F5F5F5">
            <span class="Normal">Billing Address:</span></td>
        <td width="250" bgcolor="#F5F5F5"> 
                    <telerik:RadTextBox ID="billingAddress" Runat="server" Skin="Default2006" 
                Width="200px">
            </telerik:RadTextBox> <br />
            <telerik:RadTextBox ID="billingAddress2" Runat="server" Skin="Default2006" 
                Width="200px">
            </telerik:RadTextBox>        
        </td>
        </tr>
        
        <tr>
        <td width="150"><span class="Normal">City:</span></td>
        <td width="250">
            <telerik:RadTextBox ID="physicalCity" Runat="server" Skin="Default2006" 
                Width="200px">
            </telerik:RadTextBox>
        </td>
        <td width="150" bgcolor="#F5F5F5"><span class="Normal">City:</span></td>
        <td width="250" bgcolor="#F5F5F5">
            <telerik:RadTextBox ID="billingCity" Runat="server" Skin="Default2006" 
                Width="200px">
            </telerik:RadTextBox>
        </td>
        </tr>
        
        <tr>
        <td width="150"><span class="Normal">State:</span></td>
        <td width="250"> 
            <telerik:RadComboBox ID="physicalState" Runat="server" 
                DataSourceID="statesSqlDataSource" DataTextField="State" DataValueField="Value" 
                Skin="Default2006" Width="200px">
                <CollapseAnimation Duration="200" Type="OutQuint" />
            </telerik:RadComboBox>
            </td>
        <td width="150" bgcolor="#F5F5F5"><span class="Normal">State:</span></td>
        <td width="250" bgcolor="#F5F5F5"> 
             <telerik:RadComboBox ID="billingState" Runat="server" 
                DataSourceID="statesSqlDataSource" DataTextField="State" DataValueField="Value" 
                Skin="Default2006" Width="200px">
                <CollapseAnimation Duration="200" Type="OutQuint" />
            </telerik:RadComboBox>
        </td>
        </tr>
        
        <tr>
        <td width="150"><span class="Normal">Zip Code:</span></td>
        <td width="250">            
 
            <telerik:RadMaskedTextBox ID="physicalZip" Runat="server" Mask="#####" 
                ResetCaretOnFocus="True" Skin="Default2006" Width="80px">
            </telerik:RadMaskedTextBox>
 
            </td>
        <td width="150" bgcolor="#F5F5F5"><span class="Normal">Zip Code:</span></td>
        <td width="250" bgcolor="#F5F5F5"> 
            <telerik:RadMaskedTextBox ID="billingZip" Runat="server" Mask="#####" 
                ResetCaretOnFocus="True" Skin="Default2006" Width="80px">
            </telerik:RadMaskedTextBox>
        </td>
        </tr>
        
        <tr>
        <td width="150"><span class="Normal">Directional Location:</span><br /><span class="SideHelp">(For Job Board)</span></td>
        <td width="250"> 
            <telerik:RadComboBox ID="directionalLocation" Runat="server" Skin="Default2006" 
                Width="200px">
                <Items>
                    <telerik:RadComboBoxItem runat="server" Text="North" Value="North" />
                    <telerik:RadComboBoxItem runat="server" Text="East" Value="East" />
                    <telerik:RadComboBoxItem runat="server" Text="South" Value="South" />
                    <telerik:RadComboBoxItem runat="server" Text="West" Value="West" />
                    <telerik:RadComboBoxItem runat="server" Text="Central" Value="Central" />
                    <telerik:RadComboBoxItem runat="server" Text="Northeast" Value="Northeast" />
                    <telerik:RadComboBoxItem runat="server" Text="Northwest" Value="Northwest" />
                    <telerik:RadComboBoxItem runat="server" Text="Southeast" Value="Southeast" />
                    <telerik:RadComboBoxItem runat="server" Text="Southwest" Value="Southwest" />
                </Items>
                <CollapseAnimation Duration="200" Type="OutQuint" />
            </telerik:RadComboBox>
            </td>
        <td width="150"><span class="Normal">Worker's Comp:</span><br /><span class="SideHelp">SIC | NAICS</span></td>
        <td width="250">
            <telerik:RadTextBox ID="sicCode" Runat="server" Skin="Default2006" 
                Width="80px">
            </telerik:RadTextBox> <span class="Normal"> | </span>
            
            <telerik:RadTextBox ID="naicsCode" Runat="server" Skin="Default2006" 
                Width="80px">
            </telerik:RadTextBox>
        </td>
        </tr>
        
        <tr>
        <td width="150"><span class="Normal">Assigned Sales Rep:</span></td>
        <td width="250"> 
            <telerik:RadComboBox ID="salesRep" Runat="server" 
                DataSourceID="salesRepsSqlDataSource" DataTextField="DisplayName" 
                DataValueField="DisplayName" Skin="Default2006" Width="200px">
                <CollapseAnimation Duration="200" Type="OutQuint" />
            </telerik:RadComboBox>
            </td>
        <td width="150"><span class="Normal">Orientation Req'd:</span></td>
        <td width="250"> 
            <telerik:RadComboBox ID="orientationRequired" Runat="server" Skin="Default2006" 
                Width="200px" AutoPostBack="True">
                <Items>
                    <telerik:RadComboBoxItem runat="server" Text="Yes" Value="Yes" />
                    <telerik:RadComboBoxItem runat="server" Selected="True" Text="No" Value="No" />
                </Items>
                <CollapseAnimation Duration="200" Type="OutQuint" />
            </telerik:RadComboBox>
            <br />
            <telerik:RadTextBox ID="orientation" Runat="server" Skin="Default2006" 
                Width="200px" Visible="False">
            </telerik:RadTextBox>
            </td>
        </tr>
        
        <tr>
        <td width="150"><span class="Normal">Facility Type:</span></td>
        <td width="250"> 
            <telerik:RadComboBox ID="facilityType" Runat="server" Skin="Default2006" 
                Width="200px">
                <Items>
                    <telerik:RadComboBoxItem runat="server" Text="Hospital" Value="Hospital" />
                    <telerik:RadComboBoxItem runat="server" Text="LTC/SNF" Value="LTC/SNF" />
                    <telerik:RadComboBoxItem runat="server" Text="Clinic" Value="Clinic" />
                    <telerik:RadComboBoxItem runat="server" Text="Office" Value="Office" />
                    <telerik:RadComboBoxItem runat="server" Text="Home Care" Value="Home Care" />
                    <telerik:RadComboBoxItem runat="server" Text="Other" Value="Other" />
                </Items>
                <CollapseAnimation Duration="200" Type="OutQuint" />
            </telerik:RadComboBox>
            </td>
        <td width="150"><span class="Normal">Number of Beds:</span></td>
        <td width="250"> 
            <telerik:RadTextBox ID="numberOfBeds" Runat="server" Skin="Default2006" 
                Width="80px">
            </telerik:RadTextBox>
            </td>
        </tr>
        
        <tr>
        <td colspan="4"><span class="Normal">Nurse to Patient Ratio:  Critical Care:</span> 
            
            <telerik:RadTextBox ID="ratioICU" Runat="server" 
                Skin="Default2006" Width="40px">
            </telerik:RadTextBox>
 
        <span class="Normal">:1   General: </span>
 
            <telerik:RadTextBox ID="ratioGeneral" Runat="server" 
                Skin="Default2006" Width="40px">
            </telerik:RadTextBox>
       
        <span class="Normal">:1   Step-Down: </span>
        
            <telerik:RadTextBox ID="ratioStepDown" Runat="server" 
                Skin="Default2006" Width="40px" Culture="English (United States)" 
                LabelCssClass="radLabelCss_Default2006">
            </telerik:RadTextBox>
        
        <span class="Normal">:1</span>
       
          
        </td>
        </tr>
        
        <tr>
        <td width="150"><span class="Normal">Status:</span></td>
        <td width="250"> 
            <telerik:RadComboBox ID="facilityStatus" Runat="server" Skin="Default2006" 
                Width="200px">
                <Items>
                    <telerik:RadComboBoxItem runat="server" Text="Active" Value="Active" />
                    <telerik:RadComboBoxItem runat="server" Selected="True" Text="Prospect" 
                        Value="Prospect" />
                </Items>
                <CollapseAnimation Duration="200" Type="OutQuint" />
            </telerik:RadComboBox>
            </td>
        <td width="150"></td>
        <td width="250"> 
 
            </td>
        </tr>
        
        <tr>
        <td width="150"><span class="Normal">Upload Contract:</span></td>
        <td width="250" valign="middle">            
            <asp:FileUpload ID="FileUpload1" runat="server" EnableViewState="true" />
            &nbsp;
            <asp:LinkButton ID="LinkButton1" runat="server">Upload</asp:LinkButton>
            &nbsp;
              <asp:Label ID="Label1" runat="server"></asp:Label>
            </td>
        </tr>
        
        <tr>
        <td width="150"> </td>
        <td width="250"> 
           
            </td>
        <td width="150"> </td>
        <td width="250"> </td>
        </tr>
        </table>
    </telerik:RadPageView>
    <telerik:RadPageView ID="RadPageView2" runat="server">
        Contact Information<br />
    </telerik:RadPageView>
    <telerik:RadPageView ID="RadPageView3" runat="server">
        Facility Pay Rates<br />
    </telerik:RadPageView>
    <telerik:RadPageView ID="RadPageView4" runat="server">
        Facility Pay Rates<br />
    </telerik:RadPageView>
    <telerik:RadPageView ID="RadPageView5" runat="server">
        Customer Notes<br />
    </telerik:RadPageView>
</telerik:RadMultiPage>
 
<asp:SqlDataSource ID="statesSqlDataSource" runat="server" 
    ConnectionString="<%$ ConnectionStrings:SiteSqlServer %>" 
    SelectCommand="SELECT [Region] as 'State', [Region] as 'Value' FROM [Geo_Regions] WHERE [RegionID] &lt;&gt; 791">
</asp:SqlDataSource>
 
 
<asp:SqlDataSource ID="salesRepsSqlDataSource" runat="server" 
    ConnectionString="<%$ ConnectionStrings:SiteSqlServer %>" 
    SelectCommand="SELECT DisplayName FROM Users WHERE UserID IN (SELECT UserID FROM UserRoles WHERE RoleID IN (33,37))">
</asp:SqlDataSource>

Open in new window

0
 
NursingCorpAuthor Commented:
Line 240
0
 
EspavoCommented:
I've found that FileUploads don't work in an AJAX UpdatePanel... (Don't know why though...)
0
 
amar31282Commented:
you will need to use the telerik uploader instead of asp Fileupload


something like this

 
                   

you can see the radupload samples to make it work
0
 
NursingCorpAuthor Commented:
After a long night of research on the Telerik site, I have found a solution to make the file upload control work with the Telerik controls, Please see the code snippet for what finally worked.

I had to put the file upload inside an asp:UpdatePanel and then when the upload button was clicked, it ran my codebehind to save the file, but it also had to trigger a full postback.
        <asp:UpdatePanel runat="server" id="UpdatePanel1">
          <ContentTemplate >
          
            <asp:FileUpload ID="FileUpload1" runat="server" EnableViewState="true" />
            &nbsp;
            <asp:LinkButton ID="LinkButton1" runat="server">Upload</asp:LinkButton>
            &nbsp;
              <asp:Label ID="Label1" runat="server"></asp:Label>
              
                    </ContentTemplate >
         <triggers>
          <asp:PostbackTrigger controlid ="LinkButton1" />
          </triggers>
          </asp:UpdatePanel>

Open in new window

0
 
amar31282Commented:
great..
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 8
  • 8
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now