Wednesday, March 04, 2015

How To Upload Profile Pic Using Dotnet 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;
public partial class _Default : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
   {


   }
   SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=kranthi;Integrated Security=True");
   string path;
   protected void Button1_Click(object sender, EventArgs e)
   {


       if (FileUpload1.HasFile)
       {
           path = Server.MapPath("~/Images/" + FileUpload1.FileName);
           FileUpload1.SaveAs(path);


       }
       SqlCommand cmd = new SqlCommand("insert into upload2 values('" + txtusername.Text + "','" + txtpassword.Text + "','" + FileUpload1.FileName+ "')", con);
       con.Open();
       int i = cmd.ExecuteNonQuery();
       Response.Write(i + "record inserted");
       con.Close();
   }
   protected void LinkButton1_Click(object sender, EventArgs e)
   {
       string filepath = (sender as LinkButton).CommandArgument;
       Response.ContentType = ContentType;
       Response.AppendHeader("Content-Disposition", "attachment ; filename=" + Path.GetFileName(filepath));
       Response.WriteFile(filepath);
       Response.End();



   }
}

profilepic show



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.Data;


public partial class Default2 : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
   {


   }
   SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=kranthi;Integrated Security=True");
  
   protected void Button1_Click(object sender, EventArgs e)
   {
       SqlCommand cmd = new SqlCommand("select image1 from upload2 where username='" + txtusername.Text + "' and password='" + txtpassword.Text + "'", con);
       con.Open();
       SqlDataReader dr = cmd.ExecuteReader();
       while (dr.Read() == true)
       {
           string s = "~/Images/" + dr[0].ToString();
           Image1.ImageUrl = s;
       }
       con.Close();
      
       }


     
       
       
   }

No comments:

Post a Comment