[英]Iterate through MySql database using C# displaying rows in textbox's foreach or while loop
I'm having a little trouble with my application, I have a Database connected and have already displays the values from the first row in the database, however I need to have the database iterate through each row and display them accordingly replacing what was previously there and ideally being delayed by a timer 我的应用程序遇到了一些麻烦,已经连接了数据库,并且已经显示了数据库第一行中的值,但是我需要让数据库遍历每一行并相应地显示它们,以替换以前的内容并最好由计时器延迟
I thought this was the correct way of going about it however it doesn't seem right as now it doesn't display anything and gives me a System.NullReferenceException. 我以为这是正确的解决方法,但是它似乎不正确,因为它现在不显示任何内容,并给我System.NullReferenceException。 I have tried to solve the issue but seems to be having no luck. 我试图解决这个问题,但似乎没有运气。 If you could recommend something to read, that would be greatly helpful just can't see a lot on the internet for my specific problem 如果您可以推荐阅读的内容,那将非常有帮助,只是对于我的特定问题在互联网上看不到太多
Any help will be great first time posting on here so sorry if things are in the wrong format. 任何帮助都会是第一次在这里发布的绝佳帮助,如果格式错误,抱歉。
private void FillInTextFields(DataTable table, int ind)
{
foreach(DataRow dr in table.Rows)
{
foreach(var item in dataRow.ItemArray)
{
dataRow = table.Rows[ind];
txtNHSNumber.Text = dataRow.ItemArray.GetValue(0).ToString();
txtFirstName.Text = dataRow.ItemArray.GetValue(1).ToString();
txtLastName.Text = dataRow.ItemArray.GetValue(2).ToString();
txtTimeDate.Text = dataRow.ItemArray.GetValue(3).ToString();
txtHeartRate.Text = dataRow.ItemArray.GetValue(4).ToString();
txtTemp.Text = dataRow.ItemArray.GetValue(5).ToString();
txtReps.Text = dataRow.ItemArray.GetValue(6).ToString();
txtDia.Text = dataRow.ItemArray.GetValue(7).ToString();
txtSys.Text = dataRow.ItemArray.GetValue(8).ToString();
}
}
}
foreach(var item in dataRow.ItemArray)
should be: 应该:
foreach(var item in dr.ItemArray)
can put a sleep at the end of the loop to delay before going back to the top. 可以在循环结束时进行睡眠,以延迟返回顶部之前的时间。
It seems you have a few issues. 看来您有一些问题。 First, your inner loop doesn't seem to be necessary. 首先,您的内循环似乎不是必需的。 Second, the outer loop is going through all the rows, even though you're specifically passing the row index into your method. 其次,即使您特别将行索引传递到方法中,外部循环也会遍历所有行。 Finally, it seems you're using your "dataRow" variable before it's assigned, although I'd have to see more code to be sure. 最后,似乎您在分配变量之前正在使用“ dataRow”变量,尽管我必须确保看到更多代码。 Consider this: 考虑一下:
private void FillInTextFields(DataTable table, int ind)
{
dataRow = table.Rows[ind];
txtNHSNumber.Text = dataRow.ItemArray.GetValue(0).ToString();
txtFirstName.Text = dataRow.ItemArray.GetValue(1).ToString();
txtLastName.Text = dataRow.ItemArray.GetValue(2).ToString();
txtTimeDate.Text = dataRow.ItemArray.GetValue(3).ToString();
txtHeartRate.Text = dataRow.ItemArray.GetValue(4).ToString();
txtTemp.Text = dataRow.ItemArray.GetValue(5).ToString();
txtReps.Text = dataRow.ItemArray.GetValue(6).ToString();
txtDia.Text = dataRow.ItemArray.GetValue(7).ToString();
txtSys.Text = dataRow.ItemArray.GetValue(8).ToString();
}
You'll just need to call this method with the particular index you want to display. 您只需要使用要显示的特定索引来调用此方法。
You can also remove the ItemArray.GetValue stuff, and just go with this: 您还可以删除ItemArray.GetValue东西,然后执行以下操作:
private void FillInTextFields(DataTable table, int ind)
{
dataRow = table.Rows[ind];
txtNHSNumber.Text = dataRow[0].ToString();
txtFirstName.Text = dataRow[1].ToString();
txtLastName.Text = dataRow[2].ToString();
txtTimeDate.Text = dataRow[3].ToString();
txtHeartRate.Text = dataRow[4].ToString();
txtTemp.Text = dataRow[5].ToString();
txtReps.Text = dataRow[6].ToString();
txtDia.Text = dataRow[7].ToString();
txtSys.Text = dataRow[8].ToString();
}
Not sure why you are passing int ind
in this method it's not needed. 不知道为什么不用这种方法传递int ind
了。
private void FillInTextFields(DataTable table)
{
txtNHSNumber.Text = table.Rows[0].ItemArray[0];
txtFirstName.Text = table.Rows[0].ItemArray[1];
txtLastName.Text = table.Rows[0].ItemArray[2];
txtTimeDate.Text = table.Rows[0].ItemArray[3];
txtHeartRate.Text = table.Rows[0].ItemArray[4];
txtTemp.Text = table.Rows[0].ItemArray[5];
txtReps.Text = table.Rows[0].ItemArray[6];
txtDia.Text = table.Rows[0].ItemArray[7];
txtSys.Text = table.Rows[0].ItemArray[8];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.