1

The error is simple but i wont be able to solve this i hope u guys help me in this

This is my aspx page code

 <asp:TreeView ID="PictureTree" runat="server" ShowLines="True"> <SelectedNodeStyle Font-Bold="True" /> <NodeStyle ImageUrl="~/Images/Folder.jpg" /> </asp:TreeView> </td> <td style="width:auto;text-align:center;" valign="top"> <asp:DataList ID="PicturesInFolder" runat="server" Width="100%" CellPadding="5"> <ItemTemplate> <h3><asp:Label runat="server" ID="FileNameLabel" Text='<%# System.IO.Path.GetFilenameWithoutExtension(Eval("Name")) %>'></asp:Label></h3> <asp:Image runat="server" ID="Picture" ImageUrl='<%# PictureTree.SelectedValue & Eval("Name").ToString() %>' /> <br /><br /> </ItemTemplate> <AlternatingItemStyle BackColor="#E0E0E0" /> </asp:DataList> <asp:Label runat="server" ID="NoPicturesInFolderMessage" Font-Italic="True" Visible="False" EnableViewState="False"> There are no pictures in the selected folder... </asp:Label> 

This is my .cs code

private const object VirtualImageRoot = "~/Images/Departments/"; protected void Page_Load(object sender, System.EventArgs e) { // On the first page visit, populate the photo tree and select the root node if (!Page.IsPostBack) { PopulateTree(); PictureTree.Nodes[0].Select(); PictureTree_SelectedNodeChanged(PictureTree, EventArgs.Empty); } } private void PopulateTree() { // Populate the tree based on the subfolders of the specified VirtualImageRoot DirectoryInfo rootFolder = new DirectoryInfo(Server.MapPath(VirtualImageRoot)); TreeNode root = AddNodeAndDescendents(rootFolder, null); // Add the root to the TreeView PictureTree.Nodes.Add(root); } private TreeNode AddNodeAndDescendents(DirectoryInfo folder, TreeNode parentNode) { // Add the TreeNode, displaying the folder's name and storing the full path to the folder as the value... string virtualFolderPath; if ((parentNode == null)) { virtualFolderPath = VirtualImageRoot; } else { virtualFolderPath = (parentNode.Value + (folder.Name + "/")); } TreeNode node = new TreeNode(folder.Name, virtualFolderPath); // Recurse through this folder's subfolders DirectoryInfo[] subFolders = folder.GetDirectories(); foreach (DirectoryInfo subFolder in subFolders) { TreeNode child = AddNodeAndDescendents(subFolder, node); node.ChildNodes.Add(child); } return node; // Return the new TreeNode } protected void PictureTree_SelectedNodeChanged(object sender, System.EventArgs e) { // Refresh the DataList whenever a new node is selected DisplayPicturesInFolder(PictureTree.SelectedValue); } private void DisplayPicturesInFolder(string virtualFolderPath) { // Security check: make sure folderPath starts with VirtualImageRoot and doesn't include any ".." if ((!virtualFolderPath.StartsWith(VirtualImageRoot) || (virtualFolderPath.IndexOf("..") >= 0))) { throw new ApplicationException("Attempting to view a folder outside of the public image folder!"); } // Get information about the files in the specified folder DirectoryInfo folder = new DirectoryInfo(Server.MapPath(virtualFolderPath)); FileInfo[] fileList = folder.GetFiles(); PicturesInFolder.DataSource = fileList; PicturesInFolder.DataBind(); // If there are no pictures in the folder, display the NoPicturesInFolderMessage Label PicturesInFolder.Visible = (PicturesInFolder.Items.Count > 0); NoPicturesInFolderMessage.Visible = !PicturesInFolder.Visible; } } 

i got error in private const object VirtualImageRoot = "~/Images/Departments/";

That VirtualImageRoot is an object A const field of a reference type other than string can only be initialized with null

how can i solve this thank in advance

4
  • 7
    Why is it not a String? Commented Jun 21, 2013 at 11:45
  • what i need is to show images from sub-folder of Images/Departmentfolder through datalist some give me this solution but its in vb when i convert it into c# i found this solution it work in vb but not in c# Commented Jun 21, 2013 at 11:57
  • view DisplayPicturesInFolder function you will get to know Commented Jun 21, 2013 at 12:07
  • Marc is right, there is no reason this should not be a String. Commented Jun 21, 2013 at 12:11

1 Answer 1

15

const supports very few types; if yours isn't one of them, then in the general case you should use static readonly instead:

private static readonly object VirtualImageRoot = "~/Images/Departments/"; 

However, it is very unclear why this isn't a string in your case:

private const string VirtualImageRoot = "~/Images/Departments/"; 
Sign up to request clarification or add additional context in comments.

6 Comments

i edit my question please have a look at it when i use your code i got this Error 4 Operator '&' cannot be applied to operands of type 'string' and 'string'
Where would you use an & with a pair of strings?
@amitesh + is string.Concat, not & - unless you are in VB
@Marc actually i ask a question and some reply me in vb and when i change my code in c# i got this error i guess the problem is in this line <asp:Image runat="server" ID="Picture" ImageUrl='<%# PictureTree.SelectedValue & Eval("Name").ToString() %>' />
@amitesh then you need to figure out which language you are coding in, and adjust accordingly
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.