mathieu_cupryk
asked on
Urgent!!! Urgent! Only one row is being added to table.
When I trace the below code. I see that there are at least there rows for a MaxPeriod of
3. But the table just keeps adding the a new row to the same place.
For Index As Integer = 0 To MaxPeriod - 1
' Get date period
StoredProcName = dbName & ".dbo.usp_GetDatePeriodFro mTo"
cmd = dbManager.GetStoredProcCom mand(Store dProcName)
dbManager.AddInParameter(c md, "@YEARMONTH", DbType.String, DtPeriod)
DtPeriod = IncrementDate(DtPeriod, DateIndex)
' Put the yearmonth, start date ,and end date in a dataset
Dim myDs As DataSet = dbManager.ExecuteDataSet(c md)
' check myTable first
Dim myRow As DataRow
Dim myTable As DataTable = myDs.Tables(0)
If myTable.Rows.Count >= Index Then
myRow = myTable.Rows(Index)
End If
Dim row As DataRow = myTable.NewRow()
row.ItemArray = myRow.ItemArray
Next
Return retVal
3. But the table just keeps adding the a new row to the same place.
For Index As Integer = 0 To MaxPeriod - 1
' Get date period
StoredProcName = dbName & ".dbo.usp_GetDatePeriodFro
cmd = dbManager.GetStoredProcCom
dbManager.AddInParameter(c
DtPeriod = IncrementDate(DtPeriod, DateIndex)
' Put the yearmonth, start date ,and end date in a dataset
Dim myDs As DataSet = dbManager.ExecuteDataSet(c
' check myTable first
Dim myRow As DataRow
Dim myTable As DataTable = myDs.Tables(0)
If myTable.Rows.Count >= Index Then
myRow = myTable.Rows(Index)
End If
Dim row As DataRow = myTable.NewRow()
row.ItemArray = myRow.ItemArray
Next
Return retVal
ASKER
Dim myTable As DataTable = myDs.Tables(0)
?
?
ASKER
Dim myDs As DataSet = New DataSet()
ASKER
How about this?
Public Function GetBusinessCalendarPeriod( ByVal dbName As String, ByVal DatePeriod As String, ByVal txtMaxPeriod As String) As DataTable
Dim retVal As New DataTable("BusinessCalenda rs")
Dim DtPeriod As String = DatePeriod
Dim MaxPeriod As Integer = Int32.Parse(txtMaxPeriod)
Dim cmd As DbCommand
Dim tmpds As DataSet = New DataSet()
Dim StoredProcName As String = String.Empty
Dim DateIndex As Integer = 1
Dim myDs As DataSet = New DataSet()
'We get the divisions
For Index As Integer = 0 To MaxPeriod - 1
' Get date period
StoredProcName = dbName & ".dbo.usp_GetDatePeriodFro mTo"
cmd = dbManager.GetStoredProcCom mand(Store dProcName)
dbManager.AddInParameter(c md, "@YEARMONTH", DbType.String, DtPeriod)
DtPeriod = IncrementDate(DtPeriod, DateIndex)
' Put the yearmonth, start date ,and end date in a dataset
myDs = dbManager.ExecuteDataSet(c md)
' check myTable first
Dim myRow As DataRow
Dim myTable As DataTable = myDs.Tables(0)
If myTable.Rows.Count >= Index Then
myRow = myTable.Rows(Index)
End If
Dim row As DataRow = myTable.NewRow()
row.ItemArray = myRow.ItemArray
Next
Return retVal
End Function
Public Function GetBusinessCalendarPeriod(
Dim retVal As New DataTable("BusinessCalenda
Dim DtPeriod As String = DatePeriod
Dim MaxPeriod As Integer = Int32.Parse(txtMaxPeriod)
Dim cmd As DbCommand
Dim tmpds As DataSet = New DataSet()
Dim StoredProcName As String = String.Empty
Dim DateIndex As Integer = 1
Dim myDs As DataSet = New DataSet()
'We get the divisions
For Index As Integer = 0 To MaxPeriod - 1
' Get date period
StoredProcName = dbName & ".dbo.usp_GetDatePeriodFro
cmd = dbManager.GetStoredProcCom
dbManager.AddInParameter(c
DtPeriod = IncrementDate(DtPeriod, DateIndex)
' Put the yearmonth, start date ,and end date in a dataset
myDs = dbManager.ExecuteDataSet(c
' check myTable first
Dim myRow As DataRow
Dim myTable As DataTable = myDs.Tables(0)
If myTable.Rows.Count >= Index Then
myRow = myTable.Rows(Index)
End If
Dim row As DataRow = myTable.NewRow()
row.ItemArray = myRow.ItemArray
Next
Return retVal
End Function
I was expecting something more like:
Public Function GetBusinessCalendarPeriod( ByVal dbName As String, ByVal DatePeriod As String, ByVal txtMaxPeriod As String) As DataTable
Dim retVal As New DataTable("BusinessCalenda rs")
retVal.Columns.Add("yearmo nth")
retVal.Columns.Add("startd ate")
retVal.Columns.Add("enddat e")
Dim DtPeriod As String = DatePeriod
Dim MaxPeriod As Integer = Int32.Parse(txtMaxPeriod)
Dim StoredProcName As String = dbName & ".dbo.usp_GetDatePeriodFro mTo"
'We get the divisions
For Index As Integer = 0 To MaxPeriod - 1
' Get date period
DtPeriod = IncrementDate(DtPeriod, Index + 1)
'Dim cmd As DbCommand = dbManager.GetStoredProcCom mand(Store dProcName)
dbManager.AddInParameter(c md, "@YEARMONTH", DbType.String, DtPeriod)
Dim row As DataRow = retVal.NewRow() ' Create new row as the index in the for increases
'Put the yearmonth, start date ,and end date in a dataset
Dim tmpds As Dataset = dbManager.ExecuteDataSet(c md) ' This will put it in a dataset
row.ItemArray = tmpds.Tables(0).Rows(0).It emArray
retVal.Rows.Add(row)
Next
Return retVal
End Function
Public Function GetBusinessCalendarPeriod(
Dim retVal As New DataTable("BusinessCalenda
retVal.Columns.Add("yearmo
retVal.Columns.Add("startd
retVal.Columns.Add("enddat
Dim DtPeriod As String = DatePeriod
Dim MaxPeriod As Integer = Int32.Parse(txtMaxPeriod)
Dim StoredProcName As String = dbName & ".dbo.usp_GetDatePeriodFro
'We get the divisions
For Index As Integer = 0 To MaxPeriod - 1
' Get date period
DtPeriod = IncrementDate(DtPeriod, Index + 1)
'Dim cmd As DbCommand = dbManager.GetStoredProcCom
dbManager.AddInParameter(c
Dim row As DataRow = retVal.NewRow() ' Create new row as the index in the for increases
'Put the yearmonth, start date ,and end date in a dataset
Dim tmpds As Dataset = dbManager.ExecuteDataSet(c
row.ItemArray = tmpds.Tables(0).Rows(0).It
retVal.Rows.Add(row)
Next
Return retVal
End Function
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
why did u put 'Dim cmd As DbCommand = dbManager.GetStoredProcCom mand(Store dProcName)
in comments?
in comments?
ASKER
Excellent Excellent Job!!!
You keep recreating the table every time through the loop.
If you want to ADD to the table, create a table OUTSIDE the loop and add rows INSIDE.