This is default featured slide 4 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 1 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 2 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 4 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

Me And My Respected Teacher Mr Kamal Sheel Mishra

Mr. K.S. Mishra is HOD of Computer Science from SMS Varanasi where I have completed my MCA

Me And My Respected Teacher Mr Udayan Maiti

Mr. Udayan Maiti is a senior .Net Expert and has guided many professionals of multi national Companies(MNC)

Me And My Best Friend Mr Ravinder Goel

Mr. Ravinder Goel is a senior Software Engineer and now he is working Wipro Technology

Tuesday, 21 August 2012

File Handling in C# ..NET ( How to Save Image Binary Format in Database Table and retrieve it..)


Frist we  create table TableImage in database test and image is saved  in table binary  format………………………………………

use test
Create table TableImage (Id int primary key,Name nvarchar(50),UserImage varbinary(max))



How  to  save  image  in table in Binary Format and how to retrive it in Windows Application
(C# .NET)………………..


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
namespace FileHandlingExp
{
    public partial class StoreImageInDataBase : Form
    {
        public StoreImageInDataBase()
        {
            InitializeComponent();
        }

        private void StoreImageInDataBase_Load(object sender, EventArgs e)
        {

        }

        object objimage;
        SqlDataReader dr;

//Code  for  select image  from  any Drive……………………………………………

         private void btnSelectImage_Click(object sender, EventArgs e)
            {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                txtimagesource.Text = openFileDialog1.FileName;
            }
        }

//Code  for  save image in table  in binary format

        private void btnSaveImage_Click(object sender, EventArgs e)
        {
            FileStream fs = new FileStream(txtimagesource.Text, FileMode.Open);
            byte[] data = new byte[fs.Length];
            fs.Read(data, 0, Convert.ToInt32(fs.Length));
            SqlConnection con = new SqlConnection("Data Source=KUSH-PC;Initial Catalog=test;Integrated Security=True");
            SqlCommand com = new SqlCommand("insert into TableImage(Id,Name,UserImage) values(@id,@name,@image)", con);
            com.Parameters.AddWithValue("@id",Convert.ToInt32(txtid.Text));
            com.Parameters.AddWithValue("@name", txtname.Text);
            com.Parameters.AddWithValue("@image", data);
            con.Open();
            com.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("Image Added");
        }


//Code  for  show image(binary format) in PictureBox  from table  …………………………

        private void btnLoadImage_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("Data Source=KUSH-PC;Initial Catalog=test;Integrated Security=True");
            SqlCommand com = new SqlCommand("select Id,Name,UserImage from TableImage where Id=@id", con);
            com.Parameters.AddWithValue("@id", Convert.ToInt32(txtid.Text));
          
            con.Open();
            dr = com.ExecuteReader();
            if (dr.HasRows)
            {
                dr.Read();
                txtname.Text= ""+dr["Name"];
                objimage = dr["UserImage"];

                }

          //objimage = com.ExecuteScalar();
            byte[] data;
            data = (byte[])objimage;
            MemoryStream ms = new MemoryStream(data);
            pictureBox1.Image = Image.FromStream(ms);
            con.Close();
        }

//Code for  go to next page…………………………………………

        private void btnNextPage_Click(object sender, EventArgs e)
        {
            CopyFile cf = new CopyFile();
            cf.Show();
        }
      }
}


After retrieve  image  from table image is  stored  in binary format  and after retrieve  image show  in PictureBox Control in Windows Application in C# .NET






How  to  copy  image  from  another  location from  computer drive and paste  in other drive in computer in Windows Application in C# .NET


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace FileHandlingExp
{
    public partial class CopyFile : Form
    {
        public CopyFile()
        {
            InitializeComponent();
        }

 
       // Code for next page
        private void btnNext_Click(object sender, EventArgs e)
        {
            StoreImageInDataBase si = new StoreImageInDataBase();
            si.Show();
        }

 // Code for  how  to select image  from Computer drive …………..

        private void btnSelectSource_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                txtsourceaddress.Text = openFileDialog1.FileName;
            }
        }

// Code for  copy  image  where is  paste in  Computer drive …………..

        private void btnLocateSource_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                txtdestinationaddress.Text = saveFileDialog1.FileName;
            }

          
        }
 // Code for  how  to save image  ……………………………………

        private void btnCopyPaste_Click(object sender, EventArgs e)
        {
            FileStream fssource = new FileStream(txtsourceaddress.Text, FileMode.Open);
            FileStream fsdest = new FileStream(txtdestinationaddress.Text, FileMode.Create);
            while (true)
            {
                int i;
                i = fssource.ReadByte();
                if (i == -1)
                    break;
                fsdest.WriteByte(Convert.ToByte(i));
            }
            fsdest.Close();
            MessageBox.Show("Image copy in Location");
          
        }
        
    }

}






Sunday, 19 August 2012

How to Use FileUpload in Asp.Net ,How to Save Image ,Picture in Website Folder


First we  craete  a  table UserImage using database test

use test
create  table UserImage(SrNo int  primary key,Name nvarchar(50),UserImg nvarchar(max))

After than we  create  a Web from  like  this and  take  a Folder Image  in our  WebSite  for  saving  image  which are  upload  by  user………..




This  is  source code of Default.aspx……………………..


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

<!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>
    <style type="text/css">
        .style1 {
            width: 100%;
        }
        .style2
        {
            width: 351px;
        }
    </style>
</head>
<body>
   
    <form id="form1" runat="server">
    <div>
    <table class="style1">
        <tr>
            <td colspan="3"><center><asp:Label ID="Label1" runat="server" Text="Save Image in Image folder and Database" Font-Bold="true"></asp:Label></center>
               
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="SrNo" runat="server" Text="SrNo"></asp:Label>
            </td>
            <td class="style2">
                <asp:TextBox ID="txtsrno" runat="server"></asp:TextBox>
            </td>
            <td rowspan="5">
                <asp:Image ID="Image1" runat="server" Height="91px" Width="99px" />
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="Label4" runat="server" Text="Name"></asp:Label>
            </td>
            <td class="style2">
                <asp:TextBox ID="txtname" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="Label5" runat="server" Text="Image"></asp:Label>
            </td>
            <td class="style2">
                <asp:FileUpload ID="FileUpload1" runat="server" />
            </td>
        </tr>
        <tr>
            <td>
                &nbsp;</td>
            <td class="style2">

                <asp:Button ID="BSave" runat="server" onclick="BSave_Click" Text="Save" />
            <asp:Button ID="BSelect" runat="server" onclick="BSelect_Click" Text="Select" />
                <asp:Button ID="BDelete" runat="server" onclick="BDelete_Click" Text="Delete" />
&nbsp;<asp:Button ID="BUpdate" runat="server" onclick="BUpdate_Click" Text="Update" />
            </td>
        </tr>
        <tr>
            <td>
                &nbsp;</td>
            <td class="style2">
                <asp:Label ID="Message" runat="server" Text="Label" Visible="False"></asp:Label>
            </td>
        </tr>
    </table>
    </div>
    </form>
</body>
</html>



We create  Connection  global  for  Website  in web.config …………………

<configuration>
  <connectionStrings>
    <add name="kush" connectionString="Data Source=KUSH-PC; Initial Catalog=test;Integrated Security=True" providerName="System.Data.SqlClient"/>
  </connectionStrings>
  <system.web>
    <compilation debug="true" targetFramework="4.0"/>
  </system.web>
</configuration>


This  code  for  Default.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.Data.SqlClient;
using System.Configuration;

// using this  namespace  for   DirectoryInfo and FileInfo

using System.IO;

public partial class _Default : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
       
        con = new SqlConnection(ConfigurationManager.ConnectionStrings["kush"].ConnectionString);
    }

    SqlConnection con;
    SqlCommand cmd;
    SqlDataReader dr;

    //this  code  for  Saving image  in Database  and  Image Folder which are exist  in      Website...............

    protected void BSave_Click(object sender, EventArgs e)

    {
       
        con.Open();
        cmd = new SqlCommand("insert into UserImage (SrNo,Name,UserImg) values (@SrNo,@Name,@UserImg)", con);
        cmd.Parameters.AddWithValue("@SrNo", Convert.ToInt32(txtsrno.Text));
        cmd.Parameters.AddWithValue("@Name", txtname.Text);
        cmd.Parameters.AddWithValue("@UserImg", FileUpload1.FileName);
        FileUpload1.SaveAs(Server.MapPath("~/Image/") + FileUpload1.FileName);
        cmd.ExecuteNonQuery();
         Message.Visible=true;
        Message.Text = "Add Successfully ";
        con.Close();
        txtname.Text = "";

 }

    //this  code  for  retrive image  from Database  and  Image Folder which are exist  in     Website...............

    protected void BSelect_Click(object sender, EventArgs e)
    {
       
        con.Open();
        cmd = new SqlCommand("select * from UserImage where SrNo=@SrNo", con);
        cmd.Parameters.AddWithValue("@SrNo", Convert.ToInt32(txtsrno.Text));
         dr=cmd.ExecuteReader();
         if (dr.Read())
         {
             txtname.Text = dr["Name"].ToString();
             Image1.ImageUrl = "~/Image/" + dr["UserImg"].ToString();

         }
    
        con.Close();
   
    }



    //this  code  for  delete image  from  Database  and  Image Folder which are exist  in  Website...............

    protected void BDelete_Click(object sender, EventArgs e)

    {

        DirectoryInfo dd = new DirectoryInfo(Server.MapPath("~/Image"));
        foreach (FileInfo ff in dd.GetFiles())
        {
            if (ff.Name == Path.GetFileName(Image1.ImageUrl))
                ff.Delete();
        }

         con.Open();
         cmd = new SqlCommand("delete from UserImage where SrNo=@SrNo", con);
         cmd.Parameters.AddWithValue("@SrNo", Convert.ToInt32(txtsrno.Text));
         cmd.ExecuteNonQuery();
         con.Close();
         txtname.Text = "";
         Message.Visible=true;
         Message.Text="Delete Successfully Image "+"For SrNo"+"-"+txtsrno.Text+"";
}

    //this  code  for  Update  image  in Database  and  Image Folder and  old  image  deleted  from Image Folder

    protected void BUpdate_Click(object sender, EventArgs e)
    {
        try
        {

            DirectoryInfo dd = new DirectoryInfo(Server.MapPath("~/Image"));
            foreach (FileInfo ff in dd.GetFiles())
            {
                if (ff.Name == Path.GetFileName(Image1.ImageUrl))
                    ff.Delete();
            }

            con.Open();
            cmd = new SqlCommand("update  UserImage set Name=@Name, UserImg=@UserImg  where SrNo=@SrNo", con);
            cmd.Parameters.AddWithValue("@SrNo", Convert.ToInt32(txtsrno.Text));
            cmd.Parameters.AddWithValue("@Name", txtname.Text);
            if (FileUpload1.HasFile)
            {
                cmd.Parameters.AddWithValue("@UserImg", FileUpload1.FileName);
            }
            else
            {
                cmd.Parameters.AddWithValue("@UserImg", Image1.ImageUrl);
            }

            FileUpload1.SaveAs(Server.MapPath("~/Image/") + FileUpload1.FileName);
            cmd.ExecuteNonQuery();
            con.Close();
            txtname.Text = "";
            Message.Visible = true;
            Message.Text = "update Successfully Image " + "For SrNo" + "-" + txtsrno.Text + "";
        }
        catch (Exception ex)
        {
            Message.Visible = true;
            Message.Text = ex.Message;
        }
       
    }
}




             Means  image will be  save Image  Folder  and  table in Database……………………..