Solved

String append does not work

Posted on 2014-11-05
7
81 Views
Last Modified: 2015-01-09
System.Text.StringBuilder sb = new System.Text.StringBuilder();
                 sb.Append("javascript:openjobdetails('employer_info.aspx?SupplierId=");
                 sb.Append(GlobalFunctions.Encrypt(employerId.ToString()));
                 sb.Append("&prth_act=info");
                 if (!string.IsNullOrEmpty(referenceNumber))
                 {
                     sb.Append("&ref_num=");
                     sb.Append(referenceNumber);
                 }
                 
                 sb.Append("')");

The last append does not work. I get the following output:
javascript:openjobdetails('employer_info.aspx?SupplierId=ODM=&prth_act=info&ref_num=PM/12
0
Comment
Question by:shanthi_joseph
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40424082
it has to work, you must have that line after using sb to get the output.
0
 
LVL 34

Expert Comment

by:it_saige
ID: 40424090
Is there anything between the last } and the last sb.Append line?  I just whipped up a quick example and got the correct output.

using System;
using System.Text;

public class Program
{
	public static void Main()
	{
		string empID = "ODM";
		string refNum = "PM/12";
		StringBuilder sb = new StringBuilder();
		sb.Append("javascript:openjobdetails('employer_info.aspx?SupplierId=");
		sb.Append(empID);
		sb.Append("&prth_act=info");
		if (!string.IsNullOrEmpty(refNum))
		{
			sb.Append("&ref_num=");
			sb.Append(refNum);
		}
		sb.Append("')");
		Console.WriteLine(sb.ToString());

		// Wait for user input to close the program.
		Console.ReadLine();
	}
}

Open in new window


Produces the the following output:Capture.JPG
-saige-
0
 

Author Comment

by:shanthi_joseph
ID: 40425022
public string GetCompanyLogo(object objEmployerId, object objReferenceNumber)
    {
        string strCompanyLogo = string.Empty;

        string referenceNumber = string.Empty;
        if (objReferenceNumber != null)
        {
            referenceNumber = Convert.ToString(objReferenceNumber);
        }

        if (objEmployerId != null)
        {
            int employerId = Convert.ToInt32(objEmployerId);

            List<Users> users = Users_List.SelectByID(employerId);
             if (users.Count > 0)
             {
                 String thumbCompanyLogo = users[0].ThumbCompanyLogo;

                 System.Text.StringBuilder sb = new System.Text.StringBuilder();
                 sb.Append("javascript:openjobdetails('employer_info.aspx?SupplierId=");
                 sb.Append(GlobalFunctions.Encrypt(employerId.ToString()));
                 sb.Append("&prth_act=info");
                 if (!string.IsNullOrEmpty(referenceNumber))
                 {
                     sb.Append("&ref_num=");
                     sb.Append(referenceNumber);
                 }
                 
                 sb.Append("')");

                 string href = sb.ToString();

                 string innerHtml = "<img src='images/No-logo.jpg' border='0' align='absmiddle' width='70px' title='View Details'/>";
                 if (!string.IsNullOrEmpty(thumbCompanyLogo) && File.Exists(Server.MapPath("uploadedfiles/" + thumbCompanyLogo)))
                 {
                     innerHtml = "<img src=\"uploadedfiles/" + thumbCompanyLogo + "\" border=\"0\" align=\"absmiddle\" width=\"70px\"   title=\"View Details\"/>";
                 }

                 strCompanyLogo = "<a href=" + href + ">" + innerHtml + "</a>";
             }
        }

        return strCompanyLogo;
    }

Open in new window



and the aspx file is as follows:

 <ItemTemplate>
        <tr style="font-size: medium">
            <td>
                <%# GetCompanyLogo(Eval("EmployerID"), Eval("Reference_Number")) %>

Open in new window

In the function, it returns only part of the string.
0
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 40426993
I suspect there's one or more spaces at the end of your variable referenceNumber. Since you didn't enclose the href in quotes in the output it has the effect of cutting off the url on your page. You could add quotes or use trim() but for a real solution you should always encode your attribute values and other html output in 2 steps:

1) url encode values of query parameters in URLs because they should not contain certain characters (like # and & which will be replaced by %xx)
sb.Append(Server.UrlEncode(referenceNumber));

Open in new window


2) html encode the entire href value because that should not contain other characters (like " and & which will be replaced by html entities)
strCompanyLogo = "<a href=\"" + Server.HtmlEncode(href) + "\">" + innerHtml + "</a>";

Open in new window

0
 

Author Comment

by:shanthi_joseph
ID: 40427536
This works perfectly.
0
 

Author Comment

by:shanthi_joseph
ID: 40539390
I've requested that this question be closed as follows:

Accepted answer: 0 points for shanthi_joseph's comment #a40427536

for the following reason:

It worked perfectly
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 40539391
Looking at the previous comment, I think accepting that comment as the solution was a mistake.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Recursive Folder Upload via Web 5 92
PHP OAuth Uncaught exception 'OAuthException' 3 80
Web Application Running Slow 8 63
Windows Tool to Build Android and iOS App 3 150
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
This video teaches users how to migrate an existing Wordpress website to a new domain.
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question