Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

String append does not work

Posted on 2014-11-05
7
Medium Priority
?
92 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
Looking for a new Web Host?

Lunarpages' assortment of hosting products and solutions ensure a perfect fit for anyone looking to get their vision or products to market. Our award winning customer support and 30-day money back guarantee show the pride we take in being the industry's premier MSP.

 
LVL 35

Accepted Solution

by:
Robert Schutt earned 2000 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

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

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

      Install BugTracker on Windows 2008 server Step 1:  Install windows 2008 server 32 bit OS and configure IIS. Step 2:  Install SQL server ( SQL server 2005 or SQL server 2005 Express edition. The installer for 2008  version isn’t very f…
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
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…
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.

721 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