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;
}