Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > collecting data from dataset
 

collecting data from dataset

I have the following code to collect data from a dataset. I put in the msgbox so I can see whats going on.

Dim cntr As Integer = 0

Dim cnt As Integer = CostItemsBindingSource.Count

MsgBox("cnt= " & cnt)

ReDim CostCodeArray(cnt)

ReDim CostNameArray(cnt)

Dim ciRow As BuildersEstimatorDatabaseDataSet.CostItemsRow

For Each ciRow In BuildersEstimatorDatabaseDataSet.CostItems.Rows

CostCodeArray(cntr) = ciRow.CostCode

CostNameArray(cntr) = ciRow.CostName

MsgBox("cntr= " & cntr & vbLf & "CostCode(" & cntr & ")= " & CostCodeArray(cntr))

cntr = cntr + 1

Next

Dim i As Integer

For i = 0 To (CostCodeArray.Count - 1)

MsgBox("i= " & i & vbLf & "Cost Code= " & CostCodeArray(i))

Next

The problem is that I am getting one more item in my array that is blank than there are rows of data in my dataset. What am I doing wrong here?

Thanks

Randy0128  Friday, October 17, 2008 11:45 AM

Hi Randy0128,

Suppose the row count of the collection is 20, cnt variable will be assigned value 20. Let’s see the initial value of cntr is zero, so we get to know the 20th of the row will be assigned to CostCodeArray[19], and probably the extra row in the array is CostCodeArray[20].

Best Regards,

Bruce Zhou

Windows Forms General FAQs
Windows Forms Data Controls and Databinding FAQs

Bruce.Zhou  Wednesday, October 22, 2008 4:13 AM

Hi Randy0128,

Did you mean you were getting one more row in the array than the row collection of the data table? I tested your code, everything seems OK. Please check whether the length of the array is equal to the count of the rows.

Please also make sure there are not any blank rows in the underline data table.

Best Regards,

Bruce Zhou

Bruce.Zhou  Tuesday, October 21, 2008 1:29 PM

When I do a check with the msgboxes I get the number of items in the bindingsource by using cnt = bindingsource.count. An extra row gets put into the array 1 more than the cnt. I don't understand why. If I redim the array to 'cnt -1'it get the right number of items in the array. Any ideas why?

Thanks

Randy0128  Tuesday, October 21, 2008 2:23 PM

Hi Randy0128,

Suppose the row count of the collection is 20, cnt variable will be assigned value 20. Let’s see the initial value of cntr is zero, so we get to know the 20th of the row will be assigned to CostCodeArray[19], and probably the extra row in the array is CostCodeArray[20].

Best Regards,

Bruce Zhou

Windows Forms General FAQs
Windows Forms Data Controls and Databinding FAQs

Bruce.Zhou  Wednesday, October 22, 2008 4:13 AM

Thanks for your help. I redim the array to (cnt - 1) and that seemed to work. I thought that if array was redim to a number say 20 it would only allow 20 rows in the array.

Randy0128  Wednesday, October 22, 2008 10:37 AM

You can use google to search for other answers

Custom Search

More Threads

• Change BindingNavigator.Validate behaviour
• DataGridView columns automatically added and removed several times...
• Delete record InvalidOperationException error
• DataGridView CurrentCell jumps to wrong row
• override PaintErrorIcon
• Remote sql server not connecting using windows forms
• mail component or or Outlook component in VB 2005 to send mail
• DataGridView Filter (RaiseListChangedEvents & SuspendBinding)
• DataGridView Filtering Row
• datagridviewcheckboxcell problem