Question

I've been searching the internet with the string.format for my code and it seems that I can't find the right one that looks like on my code.

DataColumn dtCol;
dtCol = new DataColumn("ImagePath", System.Type.GetType("System.String"));
dtImages.Columns.Add(dtCol);
dtImages.Columns["ImagePath"].Expression = string.Format("<a href=\"'{0}'+ImageFilename\">View Image</a>", ImageDownloadPath);

(the ImageFilename is a column on my database table) the above code always throws an error of "Syntax error: Missing operand before '<' operator"

How do I do this properly?

Was it helpful?

Solution 4

Thanks for helping me out. I was able to figure out the way to do it. Thanks for all the posts.

alt text

OTHER TIPS

Perhaps try the following?

string.Format("<a href=\"{0}\\{1}\">View Image</a>", ImageDownloadPath, ImageFilename);

In your code you are using ''s around the string format identifier, which would have then shown up in your formatted string, and the ImageFilename property was not being used correctly. It would have simply been added as plain text.

The result of your string with the following values would be as such:

ImageFilename = "1.jpg";
ImageDownloadPath = "http://www.downloadme.com/images";

Yours: <a href="'http://www.downloadme.com/images'+ImageFilename">View Image</a>
Mine: <a href="http://www.downloadme.com/images/1.jpg">View Image</a>

It's not at all clear from the question, but I believe the problem isn't a compile-time one at all... not indeed one with string.Format. It's a problem with DataColumn.Expression. You're giving an expression which includes angle brackets, so it thinks you're trying to perform comparisons.

I can't say I know much about DataColumn.Expression, but you should look into how it quotes strings... and how it quotes quotes within strings! For example, this might work:

dtCol.Expression = string.Format
    ("'<a href=\"{0}'+ImageFilename+'\">View Image</a>'", 
     ImageDownloadPath);

However, I think it's likely to make your life a lot simpler if you didn't try to compute the HTML in the expression to start with. Can you really not apply processing any later?

Try this:

dtImages.Columns["ImagePath"].Expression = 
string.Format("&lt;a href=\"'{0}'+ImageFilename\"&gt;View Image&lt;/a&gt;", ImageDownloadPath); 

Im pretty sure the < and > are trying to be interpreted as part of the expression.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top