Solved

String append does not work

Posted on 2014-11-05
7
80 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 33

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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Foreword (May 2015) This web page has appeared at Google.  It's definitely worth considering! https://www.google.com/about/careers/students/guide-to-technical-development.html How to Know You are Making a Difference at EE In August, 2013, one …
This video teaches users how to migrate an existing Wordpress website to a new domain.
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

763 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