Click here to Skip to main content
14,734,630 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi i am trying to make a view all method for my webservice using c#.
I am unsure how to make the method that returns all the values in the
table and not just the first row.

Is there a way of returning an arraylist.
I have heard that you can use DataSet, however i am unsure how i am
supposed to use DataSet:confused:

This is my method so far.

using (SqlConnection myConnection = new SqlConnection(/*Connection Strings info part is long*/)
{
  myConnection.Open();
  SqlCommand myCommand = new SqlCommand("exec ViewAll", myConnection);
  myCommand .CommandType = CommandType.Text;
  SqlDataReader myReader= myCommand .ExecuteReader();
  ArrayList myArrList = new ArrayList();
  while (myReader.Read())
  {
    myArrayList.Add(myReader["MachineName"].ToString());
    myArrayList.Add(myReader["UserName"].ToString());
    myArrayList.Add(myReader["TimePowerdOn"].ToString());
    myArrayList.Add(myReader["OsVersion"].ToString());
    myArrayList.Add(myReader["IpAddress"].ToString());
    myArrayList.Add(myReader["NetVersion"].ToString());
    myArrayList.Add(myReader["BrowserName"].ToString());
    myArrayList.Add(myReader["BrowserVersion"].ToString());
    myArrayList.Add(myReader["TimeStamp"].ToString());
 }


[Modified: changed the code tag to a pre tag. It formats it better]
Posted
Updated 3-Aug-10 8:36am
v3
Comments
koool.kabeer 3-Aug-10 8:59am
   
you dont know how to use DataSets?
if so you can learn that from Ado.Net tutorial...
koool.kabeer 3-Aug-10 9:01am
   
follow this link to learn about ado.net..
http://msdn.microsoft.com/en-us/library/h43ks021(VS.71).aspx

Instead of dealing with complex types, I have a single method in my web services that accepts a stored proc name and a xmlstring of parameters. The web service method translates the xml parameters into sql parameters, and calls the stored proc, which then returns its datasat as xml.

One type to rule them all. :)

I wrote a tip/trick that discusses this technique:

Pass Dynamic List of Parameters to Web Service[^]

When you get your dataset back, simply load it into a dataset, and Bob's your uncle.
   
v2
yes, you can use an ArrayList, but it's not what I would suggest.

If you really wanted to use a List, I would create a new class. That class would contain all of the information for each row such as the MachineName, UserName, etc... Then for each row, you create a new instance of that class and fill in it's properties from the reader.

Then, you create a List<yourclass> and for each new item, you add it to the list. Then, you could pass that list back.

Or you could just as easily use a DataTable and use it's Load method to load the SqlDataReader as vijayr suggested and pass the DataTable back.

It really depends on what all you're going to do with the data.
   
use the IDataReader
DataTable oDt = new DataTable();
IDataReader myReader= null;

get the reader and the load it

oDt.Load(myReader);
   

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