?
Solved

C# string length ASP.NET

Posted on 2012-09-16
8
Medium Priority
?
823 Views
Last Modified: 2012-09-22
Hey experts, I am trying to dynamically load a Google Maps iFrame into a window, but I'm having an issue with strings.

I have the code below in a razor view engine.

@{ 
    string map_address = Model.google_address_webpage + ";output=embed";
    map_address.Trim();
 }

 @map_address
 @map_address.Length

<div>
<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="@map_address"></iframe><br /><small><a href="@map_address" style="color:#0000FF;text-align:left">View Larger Map</a></small>

Open in new window


The output is:

https://maps.google.com/maps?oe=utf-8&client=firefox-a&ie=UTF-8&q=hodi%27s+half+note&fb=1&gl=us&hq=hodi%27s+half+note&cid=0,0,9272037293271263376&ei=6zJOUPW4OIry9gSgnYGwBg&ved=0CCMQrwswAA ;output=embed
1013

Also, the Iframe doesn't work with error 414: the url is too long.

So, even though the link is 200 some characters, it is not trimming and is still somehow 1013 characters long. This is not working for my dynamic iFrame idea.

in the database, the string has a max length of 1000, and I've also tried this code with no luck:

@{ 
    string map_address = Model.google_address_webpage;
    map_address.Trim();
    map_address += ";output=embed";
 }

Open in new window


Please help me trim the extra space off of this string so I can embed it into the iframe code correctly.

-Jeff
0
Comment
Question by:jeffiepoo
  • 5
  • 3
8 Comments
 
LVL 26

Expert Comment

by:Alan Warren
ID: 38404307
Hi Jeff,

If you populate the iframes 'id' and 'runat' attributes, you will be able to address/reference the iframe by id.
this.iframe1.src = Left(map_address, 1000).Trim();

<iframe id="iframe1" runat="server" width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src=""></iframe>

Is the db type nvarchar or varchar?
Nvarchar types are padded, varchar types trim better.

Alan
0
 
LVL 6

Author Comment

by:jeffiepoo
ID: 38404338
It is nvarchar, what is the difference as far as padding is concerned?

Trim() doesn't work. I could try the trim function in the context you provided though if you think it would help...

Thanks for any explanation of your thought process.

-Jeff
0
 
LVL 26

Accepted Solution

by:
Alan Warren earned 2000 total points
ID: 38404371
Apologies Jeff, it's nchar that is padded.

If I create a table with two fields one of type nchar and one of type varchar, both the same size, then select from the table, the nchar field will return a value according to the size of the field, right padded with ansi space chars.

In this select result, the nchar_field has returned 5 characters + 45 space chars, which are selectable if you highlight them, whereas the varchar_field has returned 5 chars only.

Using TSQL we would trim the nchar field by using RTRIM(nchar_field), then it would only return 5 chars. Oddly enough though if you apply a LEN on either field, they will both return 5 as the length.

I think your issue is somehow related to this.

ID      nchar_field      varchar_field
1      abcde                                                   abcde

As an aside, all google maps have a short url available when you get the link, the short url for the link you posted is: http://goo.gl/maps/oxBZN


Alan
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 6

Author Comment

by:jeffiepoo
ID: 38407038
That was the issue. Now I don't get a 414 error, but I get a blank iFrame. A different issue but still the issue in this thread was fixed. Thanks!

I just changed nvarchar to varchar etc...

It would still be nice to learn how to make this change once I've received the variable in C# as a string... Seeing as the Trim() method doesn't work. Very strange.

-Jeff
0
 
LVL 26

Expert Comment

by:Alan Warren
ID: 38407849
Hi Jeff,

Been having a look at razor, been putting it off for a while, it's actually quite sharp.
Didn't like the way the default razor website totally avoids integration with the .net membership server. It was weird, if I added a user using the website admin tool, the new user gets created in the aspnet_users table, but if I add a new user using the register.vbhtml interface the new user gets created in a new table 'Profile'. When I get a chance, I think I will attempt to fully implement the .net membership provider into the default razor site created by VWD 2010.

Anyhow all that aside, while getting my head around the razor way of doing things I came across this snippet and I am wondering if you are running into this issue:
If the string that you want to display contains a backslash character (\) or double quotation marks ( " ), use a verbatim string literal that's prefixed with the @ operator. (In C#, the \ character has special meaning unless you use a verbatim string literal.)
<!-- Embedding a backslash in a string -->
@{ var myFilePath = @"C:\MyFolder\"; }
<p>The path is: @myFilePath</p>

Alan
0
 
LVL 26

Expert Comment

by:Alan Warren
ID: 38407892
Hi Jeff,

You mentioned you are putting the url into a database at some point, is the url actually making it into the db, or are you getting an empty string in the table field?


Alan
0
 
LVL 26

Expert Comment

by:Alan Warren
ID: 38407956
Hi jeff,
this works ok, it's vb but easy enough to convert.
@Code
    
    ' Set the layout page and page title
    Layout = "~/_SiteLayout.vbhtml"
    PageData("Title") = "iframe"

    ' Initialize general variables
    Dim url As String = "http://neurotreatment.com.au/"
    
    
    ' If this is a POST request, validate and process data
    If IsPost Then
        url = Request.Form("url")
    
    End If
    
    
End Code

<form method="post" action="">
    <fieldset>
        <legend>The Form</legend>
        <iframe src="@url"></iframe>
        <ol>
            <li class="email">
                <label for="url">Enter a URL:</label>
                <input type="text" id="url" name="url" title="url" value="@url" />
            </li>
        </ol>
        <p class="form-actions">
            <input type="submit" value="Navigate" title="Navigate" />
        </p>
    </fieldset>
</form>

Open in new window

Alan
0
 
LVL 6

Author Closing Comment

by:jeffiepoo
ID: 38425279
Thanks!
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

862 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