ASP.NET - load image in image control using image path

Asked By Nitish Gupta
08-Aug-11 11:52 PM
hello,
i am working in project in which there is registration form.
i this form i want to upload image of candidate and show image  in image control in form..
i have store image path in database and want to show image in image control through this image path..when page loads..
i am using asp.net C# with visual studio 2008 and sql server 2005.
thanx in advance..
  Vickey F replied to Nitish Gupta
09-Aug-11 12:01 AM

First you have to store image in your local project folder.
second read path from database and assign to ImageUrl Property of Image Control.

try this code-

 

protected void GetDetails_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("con string ");
SqlDataAdapter da;
string mySQL = "SELECT imgPath FROM emptable where empid='" + TxtEmpId.Text + "' ";
da = new SqlDataAdapter(mySQL, con);
con.Open();
DataSet ds = new DataSet();
da.Fill(ds);

if(ds.Tables[0].Rows.Count>0)
{

//Now Filling Details-

Image1.ImageURL=ds.Tables[0].Row[0]["imgPath"].ToString();
}
}

Try this code and let me know.

  Ravi S replied to Nitish Gupta
09-Aug-11 12:15 AM
HI

Try this simple by giving the path:

Image1.ImageUrl = "status/available.jpg";
  Ravi S replied to Nitish Gupta
09-Aug-11 12:16 AM
HI

When you are saving or uploading file to your directory on server, it needs Rooted path,but while getting the saved image back we can directly refer that directory and get the uploaded image just after uploading, see the small code snippet

protected void btn_Click(object sender, EventArgs e)
{
   
   if (flUpload.HasFile)
   {
   flUpload.SaveAs(Server.MapPath("Images") + "//" + flUpload.FileName);
   imgUpload.ImageUrl ="Images/" + flUpload.FileName;
   }
}

Here you can see i have just taken back the uploaded image in image control called imgUpload and over there i have not done server.mappath but instead, i have directly refer images directory where image has been uploaded, see the following snapshot of before uploading and after uploading
  Anoop S replied to Nitish Gupta
09-Aug-11 12:51 AM
refer this code

public string GetConnectionString()

    {

      //sets the connection string from your web config file "ConnString" is the name of your Connection String

      return System.Configuration.ConfigurationManager.ConnectionStrings["MyConsString"].ConnectionString;

    }

    public void ProcessRequest(HttpContext context)

    {

      string id = context.Request.QueryString["id"]; //get the querystring value that was pass on the ImageURL (see GridView MarkUp in Page1.aspx)

      if (id != null)

      {

       

        MemoryStream memoryStream = new MemoryStream();

        SqlConnection connection = new SqlConnection(GetConnectionString());

        string sql = "SELECT * FROM TblImages WHERE Id = @id";

       

        SqlCommand cmd = new SqlCommand(sql, connection);

        cmd.Parameters.AddWithValue("@id", id);

        connection.Open();

 

        SqlDataReader reader = cmd.ExecuteReader();

        reader.Read();

       

        //Get Image Data

        byte[] file = (byte[])reader["Image"];

       

        reader.Close();

        connection.Close();

        memoryStream.Write(file, 0, file.Length);

        context.Response.Buffer = true;

        context.Response.BinaryWrite(file);

        memoryStream.Dispose();

 

      }

    }

 

    public bool IsReusable {

      get {

        return false;

      }

    }


http://geekswithblogs.net/dotNETvinz/archive/2009/04/24/displaying-image-to-image-control-based-on-user-selection-in.aspx
  Devil Scorpio replied to Nitish Gupta
09-Aug-11 01:43 AM
Hi Nitish,

Here is the code to load image in image control using image path

protected void Button1_Click(object sender, EventArgs e)
  {
    if (FileUpload1.HasFile)
    try
    {
      FileUpload1.SaveAs("C:\\Uploads\\" +
       FileUpload1.FileName);
      Label1.Text = "File name: " +
       FileUpload1.PostedFile.FileName + "<br>" +
 
// ADD THIS BELOW LINE TO DISPLAY UPLOADED IMAGE IN YOUR PICTURE CONTROL
 
YourImageControl.URL = FileUpload1.PostedFile.FileName;
       FileUpload1.PostedFile.ContentLength + " kb<br>" +
       "Content type: " +
       FileUpload1.PostedFile.ContentType;
    }
    catch (Exception ex)
    {
      Label1.Text = "ERROR: " + ex.Message.ToString();
    }
    else
    {
    Label1.Text = "You have not specified a file.";
    }
  }
  dipa ahuja replied to Nitish Gupta
09-Aug-11 03:38 AM
hi.....

when you create new user page, there may be one Image control.. in that set imageurl as ur default image, such as default.jpg..

now when user want to choose any other image , he will select any image by using image upload button..

but at the time of creating account if user dont want to upload image then this default.jpg will be selected as his/her display pictr.

so in that case when u fire  insert query , there may be a field like "imageurl" in that field if user selected any image then insert that uploaded image, If not then insert default.jpg as user's  imageurl field of table..

try this code:
protected void btnUpload_Click(object sender, EventArgs e)
{
  if (FileUpload1.HasFile)
  {
    try
    {
      string FolderPathToSave = Server.MapPath("~/images");
 
      //Save Image in Images Folder
 
      FileUpload1.SaveAs(FolderPathToSave + @"\" + FileUpload1.FileName);
      Status.Text = "File name: " +
          FileUpload1.PostedFile.FileName + "<br>";
      //Save ImageURL in Database
 
      string conn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
 
      SqlConnection connection = new SqlConnection(conn);
      connection.Open();
      SqlCommand comm = new SqlCommand("Insert into table (name,url) values (@name,@url)", connection);
      comm.Parameters.AddWithValue("name""file1");
      comm.Parameters.AddWithValue("url"Path.GetFileName(FileUpload1.PostedFile.FileName));
      comm.ExecuteNonQuery();
      connection.Close();
 
    }
    catch (Exception ex)
    {
      Status.Text = "ERROR: " + ex.Message.ToString();
        
    }
    Image1.ImageUrl = "images/" + FileUpload1.FileName;
  }
}


Create New Account
help
YearBuilt> <Bedrooms> 2< / Bedrooms> <Bathrooms> 2< / Bathrooms> <Features / > <Highlights / > < / Details> <Media> <Item id = "9580351" mimeType = "image / jpeg" medium = "image" caption = "View of one of the bedrooms!"> <Url style = "Gallery"> http: / / media.point2.com / p2a com / p2a / listing / 7c33 / a6dd / 7f6f / 144c1e3b73e2a67e71e8 / w64h48.jpg< / Url> < / Item> <Item id = "9580352" mimeType = "image / jpeg" medium = "image" caption = "Relax in the living room and take in the incredible views!"> <Url style = "Gallery com / p2a / listing / a0e5 / 2702 / 685e / 8b7fd1fb35da34b1e966 / w64h48.jpg< / Url> < / Item> <Item id = "9580353" mimeType = "image / jpeg" medium = "image" caption = "Renovated bathrooms. . ."> <Url style = "Gallery"> http: / / media.point2.com / p2a / listing / b779 / 1108 / c524 com / p2a / listing / b779 / 1108 / c524 / dd51cfb96d4ffaa8b261 / w64h48.jpg< / Url> < / Item> <Item id = "9580354" mimeType = "image / jpeg" medium = "image" caption = "Entertain friends in the living space in front of breathtaking views!"> <Url style = "Gallery
Net hi friends Any one send frequently asked Important questions in C# .Net, ADO .Net, Asp .Net and Sql Server. . . . . . . . tx in Advance. . . . . . Hi, Find this. . (B)What is an IL? (B I) what is the difference between Convert.toString and .toString () method? (A) What is Native Image Generator (Ngen.exe)? (A) If we have two version of same assembly in GAC how A) What is scavenging? (B) What are different types of caching using cache object of ASP.NET? (B) How can you cache different version of same page using ASP.NET cache object? (A) How will implement Page Fragment Caching? (B) Can you compare ASP.NET sessions with classic ASP? (B) Which are the various modes of storing ASP.NET
provider. In ADO.NET you communicate with the database through a data adapter (an OleDbDataAdapter, SqlDataAdapter, OdbcDataAdapter, or OracleDataAdapter object), which makes calls to an OLE DB provider or the APIs cant update the database from the recordset. ADO.NET the data adapter allows you to control how the changes to the dataset are transmitted to the database. On order to get user id = sa;workstation id = IBM-6BC8A0DACEF;packet size = 4096?; String strCom1 = ”SELECT * FROM employee”; SqlDataAdapter sqlDa1 = new SqlDataAdapter(strCom1, strCon); ds1.Tables.Add(”employee”); sqlDa1.Fill(ds1, 40, 50, ds1.Tables[”employee”].TableName connectionstring); String strCom1 = ”byroyalty”; sqlCom1 = new SqlCommand(strCom1, sqlCon1); sqlCom1.CommandType = CommandType.StoredProcedure; sqlDa1 = new SqlDataAdapter(sqlCom1); SqlParameter myPar = new SqlParameter(”@percentage”, SqlDbType.Int); sqlCom1.Parameters.Add (myPar); myPar.Value = 40 in Sql server as var_name int How do you separate business logic while creating an ASP.NET application? There are two level of asp.net debugging 1. Page level debugging For this we have to edit the page level
Migration from ASP to ASP.net How to convert ASP site to ASP.NET site using C# http: / / www.asp.net / downloads / archived-v11 / migration-assistants / asp-to-aspnet hi, ASP.NET framework is very much different from unstrucured ASP and there is no correct way to