Error de servicios web Encontré el error en servicios web

These method in AutoComplete.asmx

[WebMethod]
public List<string> GetCountries(string prefixText)
{
     SqlConnection con = new SqlConnection(
         ConfigurationManager.ConnectionStrings[
             "MarinaNewConnectionString"].ToString());

     con.Open();

     SqlCommand cmd = new SqlCommand(
         "select * from Marina where Country like @Name+'%'", con);

     cmd.Parameters.AddWithValue("@Name", prefixText);

     SqlDataAdapter da = new SqlDataAdapter(cmd); 
     DataTable dt = new DataTable();

     da.Fill(dt);

     List<string> CountryNames = new List<string>();

     for(int i=0;i<dt.Rows.Count;i++)
     {
         CountryNames.Add(dt.Rows[i][5].ToString());
     }

     return CountryNames;
}

Encontre el error

System.InvalidOperationException: There was an error generating the XML document. --->

System.InvalidCastException: Unable to cast object of type

EDIT:

The SQL for the table:

CREATE TABLE [dbo].[Marina]( 
    [SNo] [int] IDENTITY(1,1) NOT NULL, 
    [WebSource] [varchar](500) NULL, 
    [MarinaName] [varchar](500) NULL, 
    [Region] [varchar](500) NULL, 
    [Address] [varchar](500) NULL, 
    [Country] [varchar](500) NULL, 
    [fax] [varchar](500) NULL, 
    [Phone] [varchar](500) NULL, 
    [Email] [varchar](500) NULL, 
    [Website] [varchar](500) NULL, 
    [latitude] [varchar](500) NULL, 
    [langitude] [varchar](500) NULL, 
    [NumberOfMoorings] [varchar](500) NULL, 
    [MGiD] [varchar](500) NULL, 
    [Association] [varchar](200) NULL,
) ) ON [PRIMARY]

El StackTrace:

System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidCastException: Unable to cast object of type 'System.Collections.Generic.List`1[System.String]' to type 'System.String[]'. at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write3_Arr‌​ayOfString(Object o) at Microsoft.Xml.Serialization.GeneratedAssembly.ArrayOfStringSerializer1.Serialize(‌​Object objectToSerialize, XmlSerializationWriter writer)

preguntado el 02 de febrero de 12 a las 10:02

¿Puedes publicar el seguimiento de la pila? -

This is my table CREATE TABLE [dbo].[Marina]( [SNo] [int] IDENTITY(1,1) NOT NULL, [WebSource] [varchar](500) NULL, [MarinaName] [varchar](500) NULL, [Region] [varchar](500) NULL, [Address] [varchar](500) NULL, [Country] [varchar](500) NULL, [fax] [varchar](500) NULL, [Phone] [varchar](500) NULL, [Email] [varchar](500) NULL, [Website] [varchar](500) NULL, [latitude] [varchar](500) NULL, [langitude] [varchar](500) NULL, [NumberOfMoorings] [varchar](500) NULL, [MGiD] [varchar](500) NULL, [Association] [varchar](200) NULL, ) ) ON [PRIMARY] -

System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidCastException: Unable to cast object of type 'System.Collections.Generic.List`1[System.String]' to type 'System.String[]'. at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write3_ArrayOfString(Object o) at Microsoft.Xml.Serialization.GeneratedAssembly.ArrayOfStringSerializer1.Serialize(Object objectToSerialize, XmlSerializationWriter writer) -

Which line is causing the error? Can you run it in debug mode? -

everything looks fine ... pls run by adding some dummy data without db connection and try ...CountryNames.Add("Piush");CountryNames.Add("shukla"); -

1 Respuestas

I think you won't be able to return a List<string> object. Return a string array rather than a generic.

[WebMethod]
public string[] GetCountries(string prefixText)
{
     // .....

     return CountryNames.ToArray();
}

Respondido 02 Feb 12, 15:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.