Click here to Skip to main content
15,793,452 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am trying to upload the file in sql server but it always shows fileupload is null
kindly help..
ASPX
<%@ Page Language="C#" MasterPageFile="~/Header/DMSMain.Master" AutoEventWireup="true" CodeBehind="Budgetlistdetails.aspx.cs" Inherits="DMS.Masters.Budgetlistdetails" %>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
...
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:Label ID="lblselfile" runat="server" CssClass="label_text">Select File</asp:Label>
        <asp:FileUpload ID="FileUpload1"  runat="server" Height="19px" Width="132px"  />
        <asp:Button ID="btnupload" runat="server" Text="Upload" OnClick="btnupload_Click"  />

        <Triggers>
            <asp:PostBackTrigger ControlID="btnupload" />
            <asp:AsyncPostBackTrigger ControlID="btnupload"/>
        </Triggers>
        
        <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Please select a file" ValidationGroup="a" ControlToValidate="FileUpload1"></asp:RequiredFieldValidator>
    </ContentTemplate>
    </asp:UpdatePanel>
</asp:Content>
C#
protected void btnupload_Click(object sender, EventArgs e)
{
    HttpPostedFile postedFile = FileUpload1.PostedFile;
    string filename = Path.GetFileName(postedFile.FileName);
    int filesize = postedFile.ContentLength;
    
    Stream stream = postedFile.InputStream;
    BinaryReader br = new BinaryReader(stream);
    byte[] bytes = br.ReadBytes((int)stream.Length);
    using (MasterManagement mMgmt = new MasterManagement(General.ConnString()))
    {
        mMgmt.budgetupload(txtbudid.Text.Trim(), bytes, filename, filesize, Session["UId"].ToString());
        LblError.Visible = true;
        LblError.Text = "Budget details added successfuly and send for authorization";
    }
    HyperLink1.NavigateUrl = "~/Imageviewer.aspx?ID=" + txtbudid.ToString();
}


What I have tried:

i tried adding the trigger but still same issue
Posted
Updated 8-Jul-20 1:08am
v2
Comments
ZurdoDev 7-Jul-20 15:28pm    
Only post relevant code. Please cleanup the code you have posted to just relevant code and also post the C# code you have.
Member 13998042 7-Jul-20 15:31pm    
protected void btnupload_Click(object sender, EventArgs e)
{
HttpPostedFile postedFile = FileUpload1.PostedFile;
string filename = Path.GetFileName(postedFile.FileName);
int filesize = postedFile.ContentLength;

Stream stream = postedFile.InputStream;
BinaryReader br = new BinaryReader(stream);
byte[] bytes = br.ReadBytes((int)stream.Length);
using (MasterManagement mMgmt = new MasterManagement(General.ConnString()))
{
mMgmt.budgetupload(txtbudid.Text.Trim(), bytes, filename, filesize, Session["UId"].ToString());
LblError.Visible = true;
LblError.Text = "Budget details added successfuly and send for authorization";
}
HyperLink1.NavigateUrl = "~/Imageviewer.aspx?ID=" + txtbudid.ToString();
}
this is the c# code
DerekT-P 8-Jul-20 6:00am    
So what, exactly, is "always null"? FileUpload1? "bytes"? Your original question says "upload the file in sql server but it always shows fileupload is null" but you've still shown us no reference to Sql Server, or how you're expecting the file contents to get there. In your question, what do you mean by "it"? The column in a d/b table somewhere??
Member 13998042 12-Jul-20 11:21am    
The file upload was null(fileupload.hasfile) function , but now its been resolved thanks for the help

1 solution

You've set btnUpload to be both a PostBackTrigger and an AsyncPostBackTrigger. It should only be a PostBackTrigger, since file uploads don't work via AJAX in an UpdatePanel.

You also need to make sure you have the correct enctype set on your <asp:form>:
C#
public void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        Page.Form.Enctype = "multipart/form-data";
    }
}
 
Share this answer
 
Comments
Parthasarathy Mandayam 17-Jul-23 3:35am    
why should it be !Page.IsPostBack?
Also I have the fileupload working fine in another sample program without this enctype stuff

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900