Substring error

SitePoint’s very useful in my first year before I found a classic asp document.

01.If InStr( Request.ServerVariables("HTTP_REFERER"), "google") > 0 Then    
KeyURL = Request.ServerVariables("HTTP_REFERER")    
  
' Remove all up to q=    
KeyLen = Len(KeyURL)    
kStart = InStr( KeyURL, "q=" )    
kStart = kStart  + 1    
KeyRight = KeyLen - kStart    
Keyword = Right( keyURL, KeyRight )    
  
' Check for trailing query string and remove text    
If Instr(Keyword, "&") > 0 Then    
kEnd = InStr(Keyword, "&")    
kEnd = kEnd - 1    
Keyword = Left( Keyword, kEnd )    
End If    
  
 Turn encoding into text phrase    
Keyword = Replace(Keyword, "+"," ")    
Keyword = Replace(Keyword, ",",", ")    
Keyword = " " & Keyword

asp.net please help me now I can not do.

if (Request.ServerVariables[“HTTP_REFERER”].IndexOf(“google”) > 0)
{
string KeyURL = Request.ServerVariables[“HTTP_REFERER”];
int KeyLen = KeyURL.Length;
int kStart = KeyURL.IndexOf(“q=”);
kStart = kStart + 1;
int KeyRight = KeyLen - kStart;
string Keyword = KeyURL.Substring(0, KeyRight);

       if (Keyword.IndexOf("&") > 0)
       {
        int kEnd = Keyword.IndexOf("&");
        kEnd = kEnd - 1;
        Keyword = Keyword.Substring(Keyword.Length - 1, (Keyword.Length - kEnd));
       }
       Keyword = Keyword.Replace("+", " ");
       Keyword = Keyword.Replace(",", ", ");
       Keyword += " "+ Keyword.ToString();

}

Server Error in ‘/’ Application.

Index and length must refer to a location within the string.
Parameter name: lengthDescription: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentOutOfRangeException: Index and length must refer to a location within the string.
Parameter name: length

Source Error:

Line 46: int kEnd = Keyword.IndexOf(“&”);
Line 47: kEnd = kEnd - 1;
Line 48: Keyword = Keyword.Substring(Keyword.Length - 1, (Keyword.Length - kEnd));
Line 49: }
Line 50: Keyword = Keyword.Replace(“+”, " ");

thanx

if (Request.ServerVariables["HTTP_REFERER"].IndexOf("google") > 0)
        {
        string KeyURL = Request.ServerVariables["HTTP_REFERER"];
        //string KeyURL = "http://www.google.com.tr/url?sa=t&source=web&ct=res&cd=34&ved=0CCMQFjADOB4&url=http%3A%2F%2Fwww.ozelegitimkursum.com%2F&rct=j&q=%C3%B6zel+e%C4%9Fitim+kursu&ei=73boS47dNYTmnAPWuvS2BA&usg=AFQjCNH0tkCdAXknv0gwh94SQfwHlsHS3g";
        //string KeyURL = "q=%C3%B6zel+e%C4%9Fitim+kursu";
        //string KeyURL = @"laskjdfe?q=lskj&j=ad&dj=w"; 
        int KeyLen = KeyURL.Length;
        int kStart = KeyURL.IndexOf("q=");
        kStart = kStart + 2;
        int KeyRight = KeyLen - kStart;
        string Keyword = KeyURL.Substring(kStart, KeyRight);

        if (Keyword.IndexOf("&") > 0)
        {
            int kEnd = Keyword.IndexOf("&");
            kEnd = kEnd - 1;
            Keyword = Keyword.Substring(0, kEnd);
        }
        Keyword = Keyword.Replace("+", " ");
        Keyword = Keyword.Replace(",", ", ");
        Keyword = " " + Keyword.ToString();

What are the values of those variables?

Cheers,
D.

How can I make this code asp.net

If InStr( Request.ServerVariables(“HTTP_REFERER”), “google”) > 0 Then
KeyURL = Request.ServerVariables(“HTTP_REFERER”)

’ Remove all up to q=
KeyLen = Len(KeyURL)
kStart = InStr( KeyURL, “q=” )
kStart = kStart + 1
KeyRight = KeyLen - kStart
Keyword = Right( keyURL, KeyRight )

’ Check for trailing query string and remove text
If Instr(Keyword, “&”) > 0 Then
kEnd = InStr(Keyword, “&”)
kEnd = kEnd - 1
Keyword = Left( Keyword, kEnd )
End If

’ Turn encoding into text phrase
Keyword = Replace(Keyword, “+”," “)
Keyword = Replace(Keyword, “,”,”, ")
Keyword = " " & Keyword

You didn’t answer my question. Are you using Visual Studio? If so, place a breakpoint at the start of that code and step through it line-by-line. Inspect the values and then you will see why it’s breaking.

I’m sure somebody will be along shortly to spoon-feed you some code, but on the off chance that they don’t, use this opportunity to sharpen your problem solving skills.

Cheers,
D.