Click here to Skip to main content
15,908,437 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
My WebMethod returns arraylist like this:

C#
 [WebMethod]
    public static ArrayList ProcessIT(string strpid)
    {
      DataTable dt = new DataTable();
      ArrayList list = new ArrayList();
      string queryString = "select * from Registration where RegNo='"+strpid+"' and Deleted=1";
      using (SqlConnection con = new SqlConnection("Server=op- maurya;Database=DishaEyeHospitals;User ID=sa;Password=Password123;Trusted_Connection=False;"))
        {
            using(SqlCommand cmd = new SqlCommand(queryString, con))
             {
                con.Open();
                SqlDataAdapter adp = new SqlDataAdapter(cmd);
                adp.Fill(dt);
                foreach(DataRow dr in dt.Rows)
                    {
                        list.Add(dr);
                    }
              }       
        }
        return list;
    }
it work fine but i want to iterate this list in javascript
<script>
function getpid()
{
var _pid=document.getElementById('<%=txtpid.ClientID %>').value;
alert(_pid);

PageMethods.ProcessIT(_pid,onSucess,onError);
function onSucess(result) 
{
    for (var i in result) 
     {
       alert(response[i]);
     }
     return true;
}
function onError(result) 
{
    alert("Error");
  for (var i in result) 
     {
       alert(response[i]);
     }
     
     return false;
}
}   
</script>
Posted
Updated 2-Jun-13 20:27pm
v2
Comments
Sergey Alexandrovich Kryukov 3-Jun-13 2:18am    
How come you call it JavaScript?
—SA
Member 8233601 3-Jun-13 2:20am    
OnClientClick="return getpid()"
Sergey Alexandrovich Kryukov 3-Jun-13 2:26am    
And..?
Look, tag what's applicable and ask a question accurately...
—SA

1 solution

This looks like C#, not JavaScript. :-)

If so, you should never use ArrayList. This type was rendered obsolete as early as by v.2.0, when generics were introduced. Formally, the class wasn't marked with [Obsolete] attribute only because there is nothing wrong with leaving it in some legacy code, but in new development it never makes any sense. Use System.Collections.Generic.List<> instead.

The iteration is done exactly as you did with your data rows:
C#
System.Collections.Generic.List<SomeType> list = new System.Collections.Generic.List<SomeType>();
//...
foreach (SomeType item in list) { /* ... */ }

This code can be shortened with type inference:
C#
var list = new System.Collections.Generic.List<SomeType>();
//...
foreach (var item in list) { /* ... */ }


—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900