Click here to Skip to main content
16,004,458 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have saved few images in the "Images" folder under solution explorer.
My Code is as follows (code behind file) -
C#
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 -

ASP.NET
<%@ 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.
Posted
Updated 1-Aug-17 1:51am
v3
Comments

1 solution

Hi, you should convert image to byte[] in C# (which is equivalent to varbinary(max) for sqlserver) if you want to save the image in database.
Or as your code shows...store the filepath and retrieve file from file system.
 
Share this answer
 

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