How to upload images in the folder and save in SQL database in C#?

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

<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="~/MasterPage.master"  CodeFile="product.aspx.cs" Inherits="product" %>

<asp:Content ID="Content1" runat ="server" ContentPlaceHolderID ="ContentPlaceHolder1">
<asp:Button ID="Button4" runat="server" onclick="Button4_Click" Text="Add" />
<asp:Button ID="Button5" runat="server" onclick="Button5_Click" Text="Edit" />


<asp:Panel ID="Panel1" runat="server">    <table class="style3">
        <tr>
            <td class="style4">
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style4">
                Product</td>
            <td>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style4">
                Image</td>
            <td>
                <asp:FileUpload ID="FileUpload1" runat="server" />
            </td>
        </tr>
        <tr>
            <td class="style4">
                &nbsp;</td>
            <td>
                <asp:Image ID="Image1" runat="server" Width="75px" Height="75px"/>
            </td>
        </tr>
        <tr>
            <td class="style4">
                &nbsp;</td>
            <td>
                <asp:Button ID="Button1" runat="server" Text="Add" Width="150px"
                    onclick="Button1_Click" />
            </td>
        </tr>
        <tr>
            <td class="style4">
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
    </table>
    </asp:Panel>
    <asp:Panel ID="Panel2" runat="server">
    <table class="style3">
        <tr>
            <td class="style4">
                Product</td>
            <td>
                <asp:DropDownList ID="DropDownList1" runat="server" Width="175px"
                    onselectedindexchanged="DropDownList1_SelectedIndexChanged"
                    AutoPostBack="True">
                </asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td class="style4">
                Image</td>
            <td>
           
                <asp:Image ID="Image2" runat="server" Width="75px" Height="75px"/>
             
            </td>
        </tr>
        <tr>
            <td class="style4">
                &nbsp;</td>
            <td>
              <asp:FileUpload ID="FileUpload2" runat="server" />
            </td>
        </tr>
        <tr>
            <td class="style4">
                NewImageName</td>
            <td>
                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style4">
                &nbsp;</td>
            <td>
                <asp:Button ID="Button2" runat="server" Text="Edit" Width="150px"
                    onclick="Button2_Click" />
                <asp:Button ID="Button3" runat="server" Text="Delete" Width="150px"
                    onclick="Button3_Click" />
            </td>
        </tr>
        <tr>
            <td class="style4">
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
    </table>

    </asp:Panel>
</asp:Content>
<asp:Content ID="Content2" runat="server" contentplaceholderid="head">
    <style type="text/css">
        .style3
        {
            width: 26%;
            background-color: #FF9933;
        }
        .style4
        {
            width: 88px;
        }
    </style>
</asp:Content>
................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.Configuration;
using System.Data;
using System.Collections;
using System.Text;
using System.Data.SqlClient;
public partial class product : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ToString());
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Panel1.Visible = false;
            Panel2.Visible = false;
            bind_dropdown();
        }
    }

    SqlDataAdapter da;
    protected void bind_dropdown()
    {
        da = new SqlDataAdapter("select p_name from product", con);
        DataTable dt = new DataTable();
        da.Fill(dt);
        DropDownList1.DataSource = dt;
        DropDownList1.DataTextField = "p_name";
        DropDownList1.DataValueField = "p_name";
        DropDownList1.DataBind();
        DropDownList1.Items.Insert(0, new ListItem("--select--", "0"));
    
        
      
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        insert_product();
    }

    protected void insert_product()
    {
        con.Open();
        string name = FileUpload1.FileName;
        string path = Server.MapPath("");
        string str = path + "//" + "productimage" + "//" + name;
        FileUpload1.PostedFile.SaveAs(str);
        string s = "~" + "//" + "productimage" + "//" + name;
        string insert = "insert into product values('" + TextBox1.Text + "','" + s + "')";
        SqlCommand cmd = new SqlCommand(insert, con);
      
        cmd.ExecuteNonQuery();
        Image1.ImageUrl = s.ToString();
        con.Close();
        Response.Write("Insert successfully!");
        TextBox1.ReadOnly=true;
        

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        edit();
    }

    protected void deleteOldImage()
    {
        string str = "delete from product where p_name='" + DropDownList1.SelectedValue + "'"; ;
        cmd = new SqlCommand(str, con);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
    }
    SqlCommand cmd;
    protected void edit()
    {
        deleteimgfiel();
        int id3=id1(DropDownList1.SelectedItem.Text);
        con.Open();
        string name = FileUpload2.FileName;
        string path = Server.MapPath("");
        string str = path + "//" + "productimage" + "//" + name;
        FileUpload2.PostedFile.SaveAs(str);
        string s = "~" + "//" + "productimage" + "//" + name;
       
        string str1 = "update product set p_name='"+TextBox2.Text+"' ,p_path ='"+s+"' where p_id="+id3+"";
        cmd = new SqlCommand(str1,con);
        
        cmd.ExecuteNonQuery();
        con.Close();

       
    }
    int id2;
    protected int id1(string name)
    {
        
        string str="select p_id from product where p_name='"+name+"'";
        con.Open();
        cmd=new SqlCommand(str,con);
        SqlDataReader dr=cmd.ExecuteReader();
        
        if(dr.Read())
        {
            id2=Convert.ToInt32(dr[0]);
        }
        con.Close();
        return id2;

    }
    protected void deleteimgfiel()
    { 
    string str="select p_path from product where p_name='"+DropDownList1.SelectedItem+"'";
        cmd=new SqlCommand(str,con);
        con.Open();
         SqlDataReader dr=cmd.ExecuteReader();
         
        if(dr.Read())
        {
            string path1=Server.MapPath(dr[0].ToString());
            if(System.IO.File.Exists(path1))
            {
                System.IO.File.Delete(path1);
            }
        }

        con.Close();
    
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        Panel1.Visible = true;
        Panel2.Visible = false;
    }
    protected void Button5_Click(object sender, EventArgs e)
    {
        Panel1.Visible = false;
        Panel2.Visible = true;
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {

        string str = bindImage();
        Image2.ImageUrl = str;
       
    }

    protected string bindImage()
    {
        con.Open();
        string image = "";
        string str = "select p_path from product where p_name='"+DropDownList1.SelectedValue+"'";
        SqlCommand cmd;
        cmd = new SqlCommand(str,con);
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.Read())
        {
            image=dr[0].ToString();
        }
        con.Close();
        return image;
       
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        deleteimgfiel();
        int id3 = id1(DropDownList1.SelectedItem.Text);
        string str="delete from product where p_id="+id3+"";
        cmd = new SqlCommand(str, con);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
    }

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

No comments:

Post a Comment

Sunday, 16 December 2012

How to upload images in the folder and save in SQL database in C#?

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

<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="~/MasterPage.master"  CodeFile="product.aspx.cs" Inherits="product" %>

<asp:Content ID="Content1" runat ="server" ContentPlaceHolderID ="ContentPlaceHolder1">
<asp:Button ID="Button4" runat="server" onclick="Button4_Click" Text="Add" />
<asp:Button ID="Button5" runat="server" onclick="Button5_Click" Text="Edit" />


<asp:Panel ID="Panel1" runat="server">    <table class="style3">
        <tr>
            <td class="style4">
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style4">
                Product</td>
            <td>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style4">
                Image</td>
            <td>
                <asp:FileUpload ID="FileUpload1" runat="server" />
            </td>
        </tr>
        <tr>
            <td class="style4">
                &nbsp;</td>
            <td>
                <asp:Image ID="Image1" runat="server" Width="75px" Height="75px"/>
            </td>
        </tr>
        <tr>
            <td class="style4">
                &nbsp;</td>
            <td>
                <asp:Button ID="Button1" runat="server" Text="Add" Width="150px"
                    onclick="Button1_Click" />
            </td>
        </tr>
        <tr>
            <td class="style4">
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
    </table>
    </asp:Panel>
    <asp:Panel ID="Panel2" runat="server">
    <table class="style3">
        <tr>
            <td class="style4">
                Product</td>
            <td>
                <asp:DropDownList ID="DropDownList1" runat="server" Width="175px"
                    onselectedindexchanged="DropDownList1_SelectedIndexChanged"
                    AutoPostBack="True">
                </asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td class="style4">
                Image</td>
            <td>
           
                <asp:Image ID="Image2" runat="server" Width="75px" Height="75px"/>
             
            </td>
        </tr>
        <tr>
            <td class="style4">
                &nbsp;</td>
            <td>
              <asp:FileUpload ID="FileUpload2" runat="server" />
            </td>
        </tr>
        <tr>
            <td class="style4">
                NewImageName</td>
            <td>
                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style4">
                &nbsp;</td>
            <td>
                <asp:Button ID="Button2" runat="server" Text="Edit" Width="150px"
                    onclick="Button2_Click" />
                <asp:Button ID="Button3" runat="server" Text="Delete" Width="150px"
                    onclick="Button3_Click" />
            </td>
        </tr>
        <tr>
            <td class="style4">
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
    </table>

    </asp:Panel>
</asp:Content>
<asp:Content ID="Content2" runat="server" contentplaceholderid="head">
    <style type="text/css">
        .style3
        {
            width: 26%;
            background-color: #FF9933;
        }
        .style4
        {
            width: 88px;
        }
    </style>
</asp:Content>
................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.Configuration;
using System.Data;
using System.Collections;
using System.Text;
using System.Data.SqlClient;
public partial class product : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ToString());
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Panel1.Visible = false;
            Panel2.Visible = false;
            bind_dropdown();
        }
    }

    SqlDataAdapter da;
    protected void bind_dropdown()
    {
        da = new SqlDataAdapter("select p_name from product", con);
        DataTable dt = new DataTable();
        da.Fill(dt);
        DropDownList1.DataSource = dt;
        DropDownList1.DataTextField = "p_name";
        DropDownList1.DataValueField = "p_name";
        DropDownList1.DataBind();
        DropDownList1.Items.Insert(0, new ListItem("--select--", "0"));
    
        
      
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        insert_product();
    }

    protected void insert_product()
    {
        con.Open();
        string name = FileUpload1.FileName;
        string path = Server.MapPath("");
        string str = path + "//" + "productimage" + "//" + name;
        FileUpload1.PostedFile.SaveAs(str);
        string s = "~" + "//" + "productimage" + "//" + name;
        string insert = "insert into product values('" + TextBox1.Text + "','" + s + "')";
        SqlCommand cmd = new SqlCommand(insert, con);
      
        cmd.ExecuteNonQuery();
        Image1.ImageUrl = s.ToString();
        con.Close();
        Response.Write("Insert successfully!");
        TextBox1.ReadOnly=true;
        

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        edit();
    }

    protected void deleteOldImage()
    {
        string str = "delete from product where p_name='" + DropDownList1.SelectedValue + "'"; ;
        cmd = new SqlCommand(str, con);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
    }
    SqlCommand cmd;
    protected void edit()
    {
        deleteimgfiel();
        int id3=id1(DropDownList1.SelectedItem.Text);
        con.Open();
        string name = FileUpload2.FileName;
        string path = Server.MapPath("");
        string str = path + "//" + "productimage" + "//" + name;
        FileUpload2.PostedFile.SaveAs(str);
        string s = "~" + "//" + "productimage" + "//" + name;
       
        string str1 = "update product set p_name='"+TextBox2.Text+"' ,p_path ='"+s+"' where p_id="+id3+"";
        cmd = new SqlCommand(str1,con);
        
        cmd.ExecuteNonQuery();
        con.Close();

       
    }
    int id2;
    protected int id1(string name)
    {
        
        string str="select p_id from product where p_name='"+name+"'";
        con.Open();
        cmd=new SqlCommand(str,con);
        SqlDataReader dr=cmd.ExecuteReader();
        
        if(dr.Read())
        {
            id2=Convert.ToInt32(dr[0]);
        }
        con.Close();
        return id2;

    }
    protected void deleteimgfiel()
    { 
    string str="select p_path from product where p_name='"+DropDownList1.SelectedItem+"'";
        cmd=new SqlCommand(str,con);
        con.Open();
         SqlDataReader dr=cmd.ExecuteReader();
         
        if(dr.Read())
        {
            string path1=Server.MapPath(dr[0].ToString());
            if(System.IO.File.Exists(path1))
            {
                System.IO.File.Delete(path1);
            }
        }

        con.Close();
    
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        Panel1.Visible = true;
        Panel2.Visible = false;
    }
    protected void Button5_Click(object sender, EventArgs e)
    {
        Panel1.Visible = false;
        Panel2.Visible = true;
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {

        string str = bindImage();
        Image2.ImageUrl = str;
       
    }

    protected string bindImage()
    {
        con.Open();
        string image = "";
        string str = "select p_path from product where p_name='"+DropDownList1.SelectedValue+"'";
        SqlCommand cmd;
        cmd = new SqlCommand(str,con);
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.Read())
        {
            image=dr[0].ToString();
        }
        con.Close();
        return image;
       
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        deleteimgfiel();
        int id3 = id1(DropDownList1.SelectedItem.Text);
        string str="delete from product where p_id="+id3+"";
        cmd = new SqlCommand(str, con);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
    }

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

No comments:

Post a Comment

Popular

Blog Archive

Total Pageviews

Archive