Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

datatable empty upon retrieval

Status
Not open for further replies.

proteome

Technical User
May 14, 2003
115
US
The problem is that the datatble upon calling the printout method contains no data.
Code:
Mywebservicelass
{
public DataTable ledger;
[WebMethod]
    public void Init()
    {

        Random rand = new Random();
        
        //adds columns to the table
        this.ledger.Columns.Add("accountNumber", typeof(System.Int32));
        this.ledger.Columns.Add("transactionType", typeof(System.String));
        this.ledger.Columns.Add("transactionAmount", typeof(System.Int32));
        
        int count = rand.Next(20) + 1;
        int[] accounts = {12345, 34567, 98765, 76543, 02468};
        string[] types = { "d", "w" };
        
        for (int i = 0; i < count; i++)
        {
            DataRow newRow = ledger.NewRow();
            newRow["accountNumber"] = accounts[rand.Next(5)];
            newRow["transactionType"] = types[rand.Next(2)];
            newRow["transactionAmount"] = rand.Next(100);
            ledger.Rows.Add(newRow);
            ledger.AcceptChanges();
        }
        
       
      
    }
[WebMethod]
    public string PrintRows()
    {
        string x ="";
        
        DataRow[] rows = ledger.Select();
        x += "Bank Ledger\r\n";
        x += "accountNumber\ttransactionType\ttransactionAmount\r\n";
        if (rows.Length <= 0)
        {
            x+= "no rows found\r\n";
            return x;
        }
        foreach (DataRow r in rows)
        {
            foreach (DataColumn c in r.Table.Columns)
            {
                x+= "\t {0}" + r[c].ToString();
            }
        }
        return x;
    }
 
How are you calling the PrintRows() function: You need to do something like this to display the results:
Code:
Service1 srv = new Service1(); 
string s; 
srv.Init(); 
s = srvx.PrintRows(); 
Response.Write(s);

Jim
 
I have find my error I need to declare the DataTable as static.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top