Solved

ASP.NET FileUpload Control and VB.NET

Posted on 2009-04-04
19
1,707 Views
Last Modified: 2012-05-06
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
Comment
Question by:NursingCorp
  • 8
  • 8
  • 2
  • +1
19 Comments
 

Author Comment

by:NursingCorp
ID: 24068052
When I remove the If statement, I get the following:

ERROR: Object reference not set to an instance of an object.
0
 
LVL 14

Expert Comment

by:amar31282
ID: 24068128
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
 
LVL 14

Expert Comment

by:amar31282
ID: 24068224
put a break point at

 If FileUpload1.HasFile Then

and see if the processing goes inside "if" or not
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 4

Expert Comment

by:mahadevan_v
ID: 24068311
Try this

 FileUpload1.PostedFile.SaveAs("C:\Uploads\" & _
                   FileUpload1.FileName)
0
 
LVL 4

Expert Comment

by:mahadevan_v
ID: 24068332
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
 

Author Comment

by:NursingCorp
ID: 24068389
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
 
LVL 14

Expert Comment

by:amar31282
ID: 24068409
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
 

Author Comment

by:NursingCorp
ID: 24068410
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
 
LVL 14

Expert Comment

by:amar31282
ID: 24068421
try putting EnableViewState="true" inside the FileUpload1
0
 

Author Comment

by:NursingCorp
ID: 24068434
It works on a page by itself, just not on the page with the Rad Ajax controls.
0
 

Author Comment

by:NursingCorp
ID: 24068454
I put EnableViewState="true" inside FileUpload1 and it still give me ERROR: Object reference not set to an instance of an object.
0
 
LVL 14

Expert Comment

by:amar31282
ID: 24068462
please post the aspx code.please tell On which line you are getting this error
0
 
LVL 14

Expert Comment

by:amar31282
ID: 24068475
I am sure the error is not because of fileupload it is because of some other control left uninitialized
0
 

Author Comment

by:NursingCorp
ID: 24068478
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
 

Author Comment

by:NursingCorp
ID: 24068480
Line 240
0
 
LVL 10

Expert Comment

by:Espavo
ID: 24070347
I've found that FileUploads don't work in an AJAX UpdatePanel... (Don't know why though...)
0
 
LVL 14

Expert Comment

by:amar31282
ID: 24071017
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
 

Accepted Solution

by:
NursingCorp earned 0 total points
ID: 24071947
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
 
LVL 14

Expert Comment

by:amar31282
ID: 24072109
great..
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question