Click here to Skip to main content
16,004,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,

How to set default value to the file upload control and also how to insert that value inside the database. Please mention the whole code i dont want pseudocode.
The database table i am using is user_reg_tb and the column i need to insert the image is user_image
Thanks in advance
Posted
Updated 14-Feb-12 21:42pm
v3

why don't you googled?

try this

C#
foreach (Item item in Items)
{
  string path = Path.Combine("~/images/", item.categoryImage);
  item.categoryImage = File.Exists(Server.MapPath(path)) ? item.categoryImage : GetImageDependingOnItemType();
}
 
Share this answer
 
XML
Example
---------------------
1.step 1 create table in sql(2005/2008) like below.

in sql
--------
Id  = varchar(50)
Image=image

2.step2 design .aspx form like below


in .aspx
-----------
<div>

    <table align="center"><tr><td>
        <asp:Label ID="lblid" runat="server" Text="ID"></asp:Label></td><td>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td></tr>
            <tr><td>Choose File</td><td><asp:FileUpload ID="FileUpload1" runat="server" />
        </td></tr>
               <tr><td></td><td>
                   <asp:Button ID="Button1" runat="server"
            Text="Submit" onclick="Button1_Click" /></td></tr>
            </table>

         <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
        <Columns>
        <asp:TemplateField HeaderText="ID">
        <ItemTemplate>
            <asp:Label ID="Label4" runat="server" Text='<%#Eval("id") %>'></asp:Label>
        </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField>
        <ItemTemplate>
            <asp:Image ID="Image1" runat="server" ImageUrl='<%#"Handler2.ashx?ID="+Eval("id")

%>'/>
        </ItemTemplate>
        </asp:TemplateField>
        </Columns>
        </asp:GridView>
    </div>


3.step3 add one Handler.ashx to your project
4.here clearly observe in .aspx ID(ImageUrl='<%#"Handler2.ashx?ID) and in .ashx file( string id =

context.Request.QueryString["ID"];)  will keep same

in Handler2.ashx
----------------------


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace _3tier
{
    /// <summary>
    /// Summary description for Handler2
    /// </summary>
    public class Handler2 : IHttpHandler
    {
        string cs =

ConfigurationManager.ConnectionStrings["msdbConnectionString"].ConnectionString;
        public void ProcessRequest(HttpContext context)
        {
            string id = context.Request.QueryString["ID"];
            SqlConnection con = new SqlConnection(cs);
            string query = "select * from img Where id=" + id;
            con.Open();
            SqlCommand cmd = new SqlCommand(query, con);
            SqlDataReader dr;
            dr = cmd.ExecuteReader();
            dr.Read();
            context.Response.BinaryWrite((Byte[])(dr[1]));
            context.Response.End();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}


5.step 5 write the code in .aspx.cs like below

.aspx.cs
------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace _3tier
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        string cs =

ConfigurationManager.ConnectionStrings["msdbConnectionString"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {
                int length = FileUpload1.PostedFile.ContentLength;
                byte[] imgbyte = new byte[length];
                HttpPostedFile IMG = FileUpload1.PostedFile;
                IMG.InputStream.Read(imgbyte, 0, length);
                SqlConnection con = new SqlConnection(cs);
                string query1 = "insert into img(id,image) values(@id,@image)";
                SqlCommand cmd = new SqlCommand(query1, con);
                con.Open();
                cmd.Parameters.Add("@id", SqlDbType.VarChar, 50).Value = TextBox1.Text;
                //cmd.Parameters.Add("@Name", SqlDbType.VarChar, 50).Value = TextBox2.Text;
                cmd.Parameters.Add("@image", SqlDbType.Image).Value = imgbyte;
                cmd.ExecuteNonQuery();
                string query2 = "select * from img";
                SqlDataAdapter da = new SqlDataAdapter(query2, con);
                DataSet ds = new DataSet();
                da.Fill(ds);
                GridView1.DataSource = ds;
                GridView1.DataBind();
                con.Close();

            }
            TextBox1.Text = string.Empty;
        }
    }
}
 
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