创建客户端服务器应用程序我已设法让服务器链接到数据库并将所有记录发送到客户端,它显示所有正确文本框中的第一条记录但是当我尝试单击下一个时按钮显示错误
“索引超出范围。必须是非负数且小于集合的大小。参数名称:索引”
这是我正在使用的代码
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();
}
}
}
任何想法?
谢谢
答案 0 :(得分:1)
您是否尝试使用:
House thisHouse = counter < loadedHouses.Count ? loadedHouses[counter] : null
我的意思是:你确定你的名单只包含一条记录吗?因此,当你试图获得第二名时,你会得到例外......
private void Getnext()
{
if (counter < (loadedHouses.Count - 1))
{
counter++;
GetHouse();
}
}
答案 1 :(得分:0)
我没有在你的代码中看到任何防止超出数组范围的代码。
以下是修复问题的建议......
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++;
}
private void Getlast()
{
//Counter = results.Count(); //WTF? Why results.Count() when results is a string??
Counter = ((loadedHouses == null) ? 0 : loadedHouses.Count);
}
但是,您还应该根据用户 是否能够点击它们来禁用/启用按钮。
答案 2 :(得分:0)
您在loadedHouses中没有任何元素。它将在您的Fall_Click处理程序中填充,当单击“下一步”按钮时,很可能无法执行该处理程序。
更新:首先,请确保正确执行索引。
例如,行
if ((loadedHouses != null) && (value > loadedHouses.Count) return;
应该是
if ((loadedHouses != null) && (value >= loadedHouses.Count) return;
同样在你的GetLast功能中,你应该:
loadedHouses.Count - 1
答案 3 :(得分:0)
此外,你可以更新你的getPrevious方法,以确保你不会落入负值,反之亦然。
private void Getprevious()
{
counter--;
if (counter < 0)
counter = 0;
}
private void Getnext()
{
counter++;
if (counter > maxvaluesofrecords)
counter = max;
else
GetHouse();
}