#1
  1. No Profile Picture
    Contributing User
    ASP Explorer (0 - 99 posts)

    Join Date
    Aug 2004
    Location
    London
    Posts
    40
    Rep Power
    11

    Server.UrlEncode(); Problems


    Hi All,

    I having problems with Server.UrlEncode(); I'm trying to output data from the Database in Url encoded format. The objRdr["name"] outputs the query fine but when i use it with UrlEncode(objRdr["name"])); it outputs the following error:

    CS1502: The best overloaded method match for 'System.Web.HttpServerUtility.UrlEncode(string)' has some invalid arguments

    Here is the code that i'm using:

    PHP Code:
    <%@ Page Language="C#" Debug="true"%>
    <%@ 
    Import Namespace="System.Data.SqlClient" %>

    <
    script language="C#" runat="server">
    //vars
    int numRecords;

        
    void Page_Load(){
        
    SqlConnection objConn = new SqlConnection("Server=(local); " +
            
    "Database=Database; User ID=sa; Password=password; Trusted_Connection=False");
        
    SqlCommand objCmd = new SqlCommand("SELECT * FROM tbl_maze"objConn);
        
    objConn.Open();
        
    SqlDataReader objRdr objCmd.ExecuteReader();
        
    Response.ContentType "text/plain";
        
        while (
    objRdr.Read()) {         
            
    numRecords numRecords 1;
            
    Response.Write("&name_" numRecords "=");
            
    Response.Write(Server.UrlEncode(objRdr["name"]));
            
    Response.Write("&score_" numRecords "=");
            
    Response.Write(objRdr["score"]);
        }
        
    objRdr.Close();
        
    objConn.Close();
    }
    </script> 
    Thanks,

    Jake
  2. #2
  3. Consuming Human
    ASP Adventurer (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Norway
    Posts
    883
    Rep Power
    128
    Could be something the value of objRdr["name"] that causes this error; try using a text like Server.UrlEncode("try>this"); and see if it raises the same error.
    Solutions brought to you by Raskenlund ICT & Media
  4. #3
  5. No Profile Picture
    Contributing User
    ASP Explorer (0 - 99 posts)

    Join Date
    Aug 2004
    Location
    London
    Posts
    40
    Rep Power
    11
    I tried Server.UrlEncode("try>this"); and it works fine (without an error). I can also output the database variable with the following code and it works fine.

    Response.Write(objRdr["name"]);
  6. #4
  7. Consuming Human
    ASP Adventurer (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Norway
    Posts
    883
    Rep Power
    128
    and what about
    Code:
    string Test = objRdr["name"];
    string Test2 = Server.UrlEncode(Test);
    Response.Write (Test2);
  8. #5
  9. No Profile Picture
    Contributing User
    ASP Explorer (0 - 99 posts)

    Join Date
    Aug 2004
    Location
    London
    Posts
    40
    Rep Power
    11
    Thanks kboek,

    I have tried that the recieved the following error:

    Cannot implicitly convert type 'object' to 'string'

    I guess this was the original problem. What is the best was to convert an object to a string?

    Thanks,
  10. #6
  11. No Profile Picture
    Contributing User
    ASP Explorer (0 - 99 posts)

    Join Date
    Aug 2004
    Location
    London
    Posts
    40
    Rep Power
    11
    I have found a resolution. Here is the code:

    string dbString1 = (string)objRdr["name"];
    Response.Write(Server.UrlEncode(dbString1));

    Please let me know if you know of a better way of doing this.

    Thanks for your help.

Similar Threads

  1. Replies: 28
    Last Post: July 5th, 2005, 03:26 AM
  2. Problems Comparing dates between database / ASP
    By Pluto in forum ASP Development
    Replies: 1
    Last Post: June 7th, 2005, 12:14 PM
  3. Shopping Cart/Inventory Problems!!
    By K4tz in forum ASP Development
    Replies: 1
    Last Post: April 8th, 2005, 09:17 AM
  4. Replies: 1
    Last Post: August 6th, 2004, 03:39 PM
  5. Duplicate data entry raise problems..help pls
    By shanny in forum Microsoft Access Help
    Replies: 1
    Last Post: April 22nd, 2004, 11:55 AM

IMN logo majestic logo threadwatch logo seochat tools logo