I have saved few images in the "Images" folder under solution explorer.
My Code is as follows (code behind file) -
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.IO;
namespace Shop
{
public partial class addimage : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\Users\\Sayan\\Documents\\Saanvi.mdf;Integrated Security=True;Connect Timeout=30");
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Buttonuploadimage_Click(object sender, EventArgs e)
{
FileUpload f = new FileUpload();
if (f.HasFile)
{
string str = f.FileName;
f.PostedFile.SaveAs(Server.MapPath(".") + "//Images//" + str);
string path = "~//Images//" + str.ToString();
con.Open();
SqlCommand cmd=new SqlCommand("Insert into Images values('" + path + "')", con);
cmd.ExecuteNonQuery();
con.Close();
Labeluploadimage.Text = "Image uploaded successfully";
}
else
{
Labeluploadimage.Text = "Pls. upload an image";
}
After placing a breakpoint & "debug", I saw that f.HasFile is showing "False".
My Source file is as follows -
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="addimage.aspx.cs" Inherits="Shop.addimage" %>
<!DOCTYPE html>
<title>
.auto-style1 {
width: 100%;
}
.auto-style2 {
width: 127px;}
<div>
</div>
<table class="auto-style1"><tbody><tr><td class="auto-style2">Upload</td><td>
<asp:FileUpload ID="imageupload" runat="server" visible="true" EnableViewState="False" />
</td></tr><tr><td class="auto-style2"> </td><td>
<asp:Button ID="Buttonuploadimage" runat="server" OnClick="Buttonuploadimage_Click" Text="Upload Image" OnDataBinding="Buttonuploadimage_Click" />
</td></tr><tr><td class="auto-style2"> </td><td> </td></tr><tr><td class="auto-style2">
<asp:Label ID="Labeluploadimage" runat="server">
</td><td> </td></tr></tbody></table>
<asp:GridView ID="GridViewimageupload" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
Summary of problems -
1. The image is not uploaded when I'm running the page in browser. Instead it is saying "no file selected". Therefore I understand that the if condition is not executed even if I'm selecting an image. Only the Else part is executed.
2. I'm not able to store the image in the database table.
What I have tried:
I have tried the following code in C# to upload the image and then store them to the database table.