Solved

String append does not work

Posted on 2014-11-05
7
71 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
7 Comments
 
LVL 142

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 32

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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Foolproof security solutions has become one of the key necessities of every e-commerce or Internet banking website. If you too own an online shopping site then its vital for you to equip your web portal with customer security features that can allow…
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

757 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now