How to save image into database and file path in C#?

..................source code.................

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="returnimage.aspx.cs" Inherits="returnimage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ImageButton ID="ImageButton1" runat="server" Width="100px" Height="100px"
            onclick="ImageButton1_Click" ImageUrl="~/image/j.jpg" />
       
        <asp:FileUpload ID="FileUpload1" runat="server" />
    </div>
    <asp:Label ID="Label1" runat="server" Text='<%#Eval("id") %>'></asp:Label>
    </form>
</body>
</html>
..............end source code...
..................code behind......................
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Text;
using System.Data.SqlClient;
using System.IO;
public partial class returnimage : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString());
    SqlCommand cmd;
    SqlDataReader dr;
    SqlDataAdapter da;
    DataSet ds = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
        }
    }

   
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
       string str= insertimage();
       string id = Label1.Text;
       string str1 = "update images set path='"+str+"' where id='"+id+"'";
       cmd = new SqlCommand(str1,con);
       con.Open();
       cmd.ExecuteNonQuery();
       con.Close();
        ImageButton1.ImageUrl=str;
        
    }
    
    protected string insertimage()
    {
        string kul="";
        string name = FileUpload1.FileName;
        string path = Server.MapPath("");
        string save = path + "//" + "image" + "//" + name;
        FileUpload1.PostedFile.SaveAs(save);
        kul = "~" + "//" + "image" + "//" + name;
        //string id = Label1.Text;
        string s = "insert into images values('" + kul + "')";
        con.Open();
        cmd = new SqlCommand(s, con);
        cmd.ExecuteNonQuery();
        con.Close();
        return kul;
        Response.Write("upload!");
        
    }
  

}
.........................end code behind..................
Share:

No comments:

Post a Comment

Sunday, 16 December 2012

How to save image into database and file path in C#?

..................source code.................

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="returnimage.aspx.cs" Inherits="returnimage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ImageButton ID="ImageButton1" runat="server" Width="100px" Height="100px"
            onclick="ImageButton1_Click" ImageUrl="~/image/j.jpg" />
       
        <asp:FileUpload ID="FileUpload1" runat="server" />
    </div>
    <asp:Label ID="Label1" runat="server" Text='<%#Eval("id") %>'></asp:Label>
    </form>
</body>
</html>
..............end source code...
..................code behind......................
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Text;
using System.Data.SqlClient;
using System.IO;
public partial class returnimage : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString());
    SqlCommand cmd;
    SqlDataReader dr;
    SqlDataAdapter da;
    DataSet ds = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
        }
    }

   
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
       string str= insertimage();
       string id = Label1.Text;
       string str1 = "update images set path='"+str+"' where id='"+id+"'";
       cmd = new SqlCommand(str1,con);
       con.Open();
       cmd.ExecuteNonQuery();
       con.Close();
        ImageButton1.ImageUrl=str;
        
    }
    
    protected string insertimage()
    {
        string kul="";
        string name = FileUpload1.FileName;
        string path = Server.MapPath("");
        string save = path + "//" + "image" + "//" + name;
        FileUpload1.PostedFile.SaveAs(save);
        kul = "~" + "//" + "image" + "//" + name;
        //string id = Label1.Text;
        string s = "insert into images values('" + kul + "')";
        con.Open();
        cmd = new SqlCommand(s, con);
        cmd.ExecuteNonQuery();
        con.Close();
        return kul;
        Response.Write("upload!");
        
    }
  

}
.........................end code behind..................

No comments:

Post a Comment

Popular

Blog Archive

Total Pageviews

Archive