0

I am trying to read a string into an array and I get the error "Cannot implecitly convert type 'string' to 'string[]'.

The error occurs here:

string[] sepText = result.Tables[0].Rows[0].Field<string>("WebHTML").UrlDecode();

My full if else statement is below:

if (!string.IsNullOrEmpty(result.Tables[0].Rows[0].Field<string>("WebHTML")))
            {
                string[] sepText = result.Tables[0].Rows[0].Field<string>("WebHTML").UrlDecode();
                NewsContent.Text = sepText[1];
                if (!string.IsNullOrEmpty(sepText[0]))
                    Image1.ImageUrl = sepText[0];
                else
                    Image1.Visible = false;
                NewsTitle.Text = String.Format("<a href=\"{0}/news/{1}/{2}.aspx\">{3}</a>", Extensions.GetServerName(true), result.Tables[0].Rows[0].Field<int>("News_Item_ID"), result.Tables[0].Rows[0].Field<string>("Title").UrlFormat(), result.Tables[0].Rows[0].Field<string>("Title"));
                Hyperlink1.NavigateUrl = String.Format("{0}/news/{1}/{2}.aspx", Extensions.GetServerName(true), result.Tables[0].Rows[0].Field<int>("News_Item_ID"), result.Tables[0].Rows[0].Field<string>("Title").UrlFormat());
            }
            else
            {
                Hyperlink1.Visible = false;
                Image1.Visible = false;
            }

Thank you for your help!

EDIT Code for URL Decode:

public static string UrlDecode(this string str)
{
    return System.Web.HttpUtility.UrlDecode(str);
}
2
  • 1
    Show the code for UrlDecode. Apparently you're expecting it to return an array, but it isn't. Commented Oct 10, 2011 at 14:44
  • 1
    A string is not a string array. What is the point of using an array here? Commented Oct 10, 2011 at 14:44

6 Answers 6

3

result.Tables[0].Rows[0].Field<string>("WebHTML") is going to give you the value of the WebHTML field in the first row in the first table which is a single string rather than a string[].

You may want to show your code for UrlDecode() since it looks like a custom implementation rather than one of the built-in framework versions.

You also declare the UrlDecode method to take a string as a parameter and return a string. Remember, a string is not the same thing as a string array.

Sign up to request clarification or add additional context in comments.

2 Comments

I use the UrlDecode on different sections of the project. How will I modify it to fit the current situation?
@CKallemeres - I'm not sure what you're expecting it to do. The code you have should only work with a single string...no idea where the array would come from.
1

It seems that you are trying to put:

result.Tables[0].Rows[0].Field<string>("WebHTML").UrlDecode();

which returns a string, into an array of strings. Simply delare your sepText variable as a string rather than a string array and you should be good to go, e.g.:

string sepText = result.Tables[0].Rows[0].Field<string>("WebHTML").UrlDecode();

Later in your code you will clearly need to read the contents of the string like this:

Image1.ImageUrl  =sepText;

Comments

0

Assuming the UrlDecode you are using is the one from here then the result is a string and not a string[] !

Comments

0

UrlDecode returns a string and you are assigning it to an array.

If you want the parts you will have to use the string to create an Url object.

Url url = new Url(result.Tables[0].Rows[0].Field<string>("WebHTML"));

and then get the parts.

See: Get url parts without host

Comments

0

I don't think URLDecode works the way you think it works. All URLDecode does is remove URL encoding from a string. It does not return an array of strings - only the decoded value of the string you gave it.

http://msdn.microsoft.com/en-us/library/system.web.httputility.urldecode.aspx

Example: Your web browser replaces a space with %20. This changes the %20 back to a space.

Comments

0

That's because the result of this line is "string" and you're trying to assign it to an array since UrlDecode do not produce an array. What you probably wanted is to use a method split() to create an array of separators?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.