By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,364 Members | 1,267 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,364 IT Pros & Developers. It's quick & easy.

fetching image from folder and display in listview

parshupooja
100+
P: 159
Contact Reply




1 point Member
propoo
Joined on 08-31-2007, 10:32 PM
Posts 3
Hey all ,

I am using following code to check if there is any image into my folder and if there are images it should load name into the listview. To do that I am first checking if there are any image and I am saving that name into datatable and finally assigining datatable as a source to listview.

My problem is it shows all the names of imags as first or single record in listview whereas if i try to display in label is shows a break

Result for label

Blue hills.jpg
Sunset.jpg
Water lilies.jpg
Winter.jpg


Result for listview

Blue hills.jpg<br/>Sunset.jpg<br/>Water lilies.jpg<br/>Winter.jpg<br/>
Expand|Select|Wrap|Line Numbers
  1. using System.IO;
  2.  
  3. public partial class Image : System.Web.UI.Page
  4. {
  5.     string name="";
  6.     string strDriveLetter = "C:/";
  7.     string strDefaultPath = "Documents and Settings/All Users/Documents/My Pictures/Sample Pictures";
  8.  
  9.     protected void Page_Load(object sender, EventArgs e)
  10.     {
  11.         if (!Page.IsPostBack)
  12.         {
  13.             DataTable myDt = new DataTable();
  14.             myDt = CreateDataTable();
  15.             Session["myDatatable"] = myDt;
  16.  
  17.  
  18.             this.ListBox1.DataSource = ((DataTable)Session["myDatatable"]).DefaultView;
  19.             this.ListBox1.DataBind();
  20.         } 
  21.  
  22.         string strPath;
  23.         // check if the query string variable "strPath" has a value
  24.         // if it does, we are navigating a folder that is under the root folder
  25.         // if it does not, we start at the root folder by default
  26.         if (Request.QueryString["strPath"] != null)
  27.         {
  28.             strPath = Request.QueryString["strPath"];
  29.         }
  30.         else
  31.         {
  32.             strPath = strDefaultPath;
  33.         }
  34.  
  35.         DisplayDirContents(strPath);
  36.  
  37.         AddDataToTable(this.name.ToString(), (DataTable)Session["myDatatable"]);
  38.  
  39.         this.ListBox1.DataSource = ((DataTable)Session["myDatatable"]).DefaultView;
  40.         this.ListBox1.DataBind();
  41.     }
  42.  
  43.     private void DisplayDirContents(string strPath)
  44.     {   // function to display list of folders and files in specified path 
  45.         // define some arrays 
  46.         DirectoryInfo[] aryDirectories;
  47.         FileInfo[] aryFiles;
  48.  
  49.         // variable to store the complete file system path to the folder
  50.         string strFSPath = strDriveLetter + strPath;
  51.  
  52.         // some string manipulation to get the name of the current script
  53.         string strCurrentScript = Request.ServerVariables["URL"];
  54.         string[] arrURL = strCurrentScript.Split('/');
  55.         strCurrentScript = arrURL[arrURL.Length - 1];
  56.  
  57.         // instantiate local instance of DirectoryInfo object
  58.         DirectoryInfo objCurrentDirectory = new DirectoryInfo(strFSPath);
  59.  
  60.         // get the directories present in the current folder
  61.         // as an array of DirectoryInfo objects
  62.         aryDirectories = objCurrentDirectory.GetDirectories();
  63.         // get the files present in the current folder
  64.         // as an array of FileInfo objects
  65.         aryFiles = objCurrentDirectory.GetFiles("*.jpg");
  66.  
  67.         // check if the current folder has any files
  68.         if (aryFiles.Length != 0)
  69.         {
  70.             // if it does, then iterate and 
  71.             // display the information about each file
  72.             foreach (FileInfo objFile in aryFiles)
  73.             {
  74.  
  75.  
  76.                 name += objFile.ToString();
  77.                 name += "&lt;br/>";
  78.                 Label1.Text += objFile.ToString();
  79.                 Label1.Text += "&lt;br/>";
  80.  
  81.             }
  82.  
  83.         } 
  84.     }
  85.  
  86.     private DataTable CreateDataTable()
  87.     {
  88.         DataTable myDataTable = new DataTable();
  89.         DataColumn myDataColumn;
  90.         myDataColumn = new DataColumn();
  91.         myDataColumn.DataType = Type.GetType("System.String");
  92.         myDataColumn.ColumnName = "name";
  93.         myDataTable.Columns.Add(myDataColumn);
  94.         return myDataTable;
  95.     }
  96.  
  97.     private void AddDataToTable(string name, DataTable myTable)
  98.     {
  99.         DataRow row;
  100.         row = myTable.NewRow();
  101.         row["name"] = name.ToString();
  102.         myTable.Rows.Add(row);
  103.     }
  104.  
  105.  
  106.  
  107. }
  108.  
Plz help
Nov 14 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.