Creating a client server application i have managed to get the server to link to the database and send all the records over to the client and it shows the first record in all the correct text boxes but when i try and click on my next button it shows the error
“Index was out of range.Must be non-negative and less than the size of the collection. Parameter name: index”
Heres the code i am using for this
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
client c1;
int counter = 0;
string results = "";
List<House> loadedHouses = new List<House>();
public Form1()
{
InitializeComponent();
c1 = new client();
c1.runClient();
textBox1 = Convert.ToString(Counter);
}
private int _counter; //It defaults to 0, setting it to 0 is redundant.
public int Counter
{
get { return _counter; }
set
{
if (Equals(_counter, value)) return;
if (value < 0) return;
if ((loadedHouses != null) && (value > loadedHouses.Count)) return;
_counter = value;
GetHouse();
}
}
private void GetFirst()
{
Counter = 0;
}
private void Getprevious()
{
Counter--;
}
private void Getnext()
{
Counter++;
GetHouse();
}
private void Getlast()
{
Counter = ((loadedHouses == null) ? 0 : loadedHouses.Count);
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Funsold_Click(object sender, EventArgs e)
{
}
private void Fsold_Click(object sender, EventArgs e)
{
}
private void Fall_Click(object sender, EventArgs e)
{
string aQuery = "SELECT * FROM houses";
string result = c1.getStringfromServer(aQuery);
string[] Rows = result.Split('*');
//try
//{
foreach (string r in Rows)
{
string[] h = new string[5];
h = r.Split(',');
MessageBox.Show(h[0]);
// need to show the first record
House newhouse = new House();
if (h[0] == "")
{
MessageBox.Show("ARRRHHHJJJ CRASH!");
}
else
{
newhouse.ID = int.Parse(h[0]);
newhouse.Address = Convert.ToString(h[1]);
newhouse.Type = Convert.ToChar(h[2]);
newhouse.Cost = int.Parse(h[3]);
newhouse.Sold = Convert.ToString(h[4]);
loadedHouses.Add(newhouse);
ID_Number.Text = Convert.ToString(h[0]);
address1.Text = Convert.ToString(h[1]);
type1.Text = Convert.ToString(h[2]);
cost1.Text = Convert.ToString(h[3]);
sold1.Text = Convert.ToString(h[4]);
}
}
// }
// catch (Exception qq)
//{
// MessageBox.Show (qq.Message);
//}
}
public void GetHouse()
{
int m = Counter;
House thisHouse = Counter < loadedHouses.Count ? loadedHouses[Counter] : null;
ID_Number.Text = Convert.ToString(thisHouse.ID);
address1.Text = Convert.ToString(thisHouse.Address);
type1.Text = Convert.ToString(thisHouse.Type);
cost1.Text = Convert.ToString(thisHouse.Cost);
sold1.Text = Convert.ToString(thisHouse.Sold);
}
private void Next_Click(object sender, EventArgs e)
{
Getnext();
}
}
}
Any Ideas?
Thank You
I don’t see anywhere in your code you guarding against going outside the bounds of the array.
Here’s a suggestion to fix your issues…
Though, you should also disabled/enable the buttons based on if the user should be able to click them.