paslanmaz -1 Newbie Poster

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

next

refer below code


  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.;
  }

error

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:

int kEnd = Keyword.IndexOf("&");
kEnd = kEnd - 1;
Line 48: Keyword = Keyword.Substring(Keyword.Length - 1, (Keyword.Length - kEnd));
}
Keyword = Keyword.Replace("+", " ");

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.