0

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. 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();
            }
        }
    }
6
  • you have a datatable and I assume that it's returning a single row..? then why do you use an Inner foreach loop..? also what if the structure of the database changes..? you should access the dataRow[0]["ColumnName"] or access the foreach(var item in dr.ItemArray) personally I do not see the need for 2 loops.. and if you return more than 1 row..then your totally screwed... Commented Nov 16, 2015 at 22:17
  • How about to debug your code and make sure the table has rows before displaying/assigning the values Commented Nov 16, 2015 at 22:17
  • @MethodMan Thank you for your comment the database structure will not change, however I agree with you that I am totally screwed and I seem to have got it displaying the first row in the database but I need it to iterate through the rows, is this the wrong way of doing it? The second loop was used for stopping continuous looping Commented Nov 16, 2015 at 22:30
  • @BenFegan you can you the answer provided with the foreach(var item in dr.ItemArray use the debugger and you can quickly see how to assign the proper text boxes with the correct item[0] ...[8] Commented Nov 16, 2015 at 22:36
  • @MethodMan I have added the changes but still only will display one row but there are many rows in the table, I guess I'm going about this wrong any other suggestion thanks for your help Commented Nov 16, 2015 at 23:02

3 Answers 3

1
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.

Sign up to request clarification or add additional context in comments.

1 Comment

this should be a for loop not a foreach loop
0

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. 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:

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

7 Comments

what do you get when you assign any of the TextBox.Text = to dataRowm[0].ToString() for example.. also where is dataRow being defined in your non answer.. ? this would fill all the text boxes text with dt.Rows[0].ToString() "System.Data.DataRow"
I'm not sure I follow what you're saying. DataRow is certainly declared elsewhere in his code, so I was just stealing that. But yes, it needs to be declared, preferably local to that method. As far as the other part, It's not doing DataRow.ToString(), it's doing DataRow[idx].ToString, which will depend on the type of object at DataRow[idx].
DataTable has Rows property.. so you can do the following without a loop since it's not returning more than one row. table.Rows[0].ItemArray[0] .... your example is not correct
That's exactly what the code I posted is doing. 'dataRow = table.Rows[0]' is getting a reference to the row itself, not the dataTable. Afterwards, 'dataRow[0].ToString()' is getting the Object at position 0 in the ROW, not in the TABLE, and it's calling ToString() on that object.
you need to declare the variable of dataRow also really no need to pass int in this dataRow = table.Rows[ind];
|
0

Not sure why you are passing int ind in this method it's not needed.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.