I am uploading File using file Upload control and I have one button to upload it on server but but it gives error at first line in code as
Object reference not set to an instance of an object
I have File upload control as
<asp:FileUpload ID="fileuploadDoc" class="form-control" runat="server" />
also Have server side Validation as
<asp:RequiredFieldValidator ID="RequiredFieldVal" ControlToValidate="fileuploadDoc" runat="server" ErrorMessage="Choose File" ValidationGroup="valProPic" Display="Dynamic" Font-Size="Smaller" ForeColor="#CC0000"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator6" ControlToValidate="fileuploadDoc" ErrorMessage="Only .png & .jpg images are alloweded" ValidationExpression="([a-zA-Z0-9\s_\\.\-:])+(.png|.jpg)$" runat="server" ValidationGroup="valProPic" Display="Dynamic" Font-Size="Smaller" ForeColor="#CC0000" />
and button as
<asp:LinkButton runat="server" ID="btnaddfileuplod" ValidationGroup="valProPic" CssClass="btn btn-primary" OnClick="btnaddfileuplod_Click">Upload</asp:LinkButton>
<asp:LinkButton runat="server" ID="btnDeleteProPic" CausesValidation="false" CssClass="btn btn-default" OnClick="btnDeleteProPic_Click">Delete</asp:LinkButton>
What I have tried:
Uplaod Button as
<asp:LinkButton runat="server" ID="btnaddfileuplod" ValidationGroup="valProPic" CssClass="btn btn-primary" OnClick="btnaddfileuplod_Click">Upload</asp:LinkButton>
On click Handlar as
protected void btnaddfileuplod_Click(object sender, EventArgs e)
{
int size1 = fileuploadDoc.PostedFile.ContentLength;
bool flag = false;
if (size1 > 0)
{
try
{
string imgPath = "";
string filename = Path.GetFileName(fileuploadDoc.PostedFile.FileName);
string extension = System.IO.Path.GetExtension(fileuploadDoc.FileName);
imgPath = "img/" + Common.Get_SystemDate_IndianFormate().ToString("ddMMyyhhmmss") + extension;
fileuploadDoc.SaveAs(Server.MapPath(imgPath));
flag = true;
lblUploadedProPicPath.Text = imgPath;
imgProfile.ImageUrl = imgPath;
}
catch (Exception ex) { flag = false; }
}
if (!flag)
{
AlertMesg(false, "File uploading fail. Please try again...!");
}
}
for another page I have Same Button and same code It Working Fine there
but here it is giving exception at first Line