Object reference not set to an instance of an object.

       Public Function GetBusinessCalendarPeriod(ByVal dbName As String, ByVal DatePeriod As String, ByVal txtMaxPeriod As String) As DataTable

            Dim retVal As New DataTable("BusinessCalendars")
            Dim DtPeriod As String = DatePeriod
            Dim MaxPeriod As Integer = Int32.Parse(txtMaxPeriod)
            Dim cmd As DbCommand
            Dim tmpds As DataSet
            Dim StoredProcName As String = String.Empty


            'We get the divisions
            For Index As Integer = 0 To MaxPeriod - 1
                ' Get date period
                StoredProcName = dbName & ".dbo.usp_GetDatePeriodFromTo"
                cmd = dbManager.GetStoredProcCommand(StoredProcName)

                dbManager.AddInParameter(cmd, "@YEARMONTH", DbType.String, DtPeriod)
                ' Put the yearmonth, start date ,and end date in a dataset
                tmpds.Tables(0).Rows(Index).Item(0) = dbManager.ExecuteDataSet(cmd)
                DtPeriod = IncrementDate(DtPeriod, Index)






Object reference not set to an instance of an object.
tmpds.Tables(0).Rows(Index).Item(0) = dbManager.ExecuteDataSet(cmd)
Line 55:                
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.


                ' Add to dataset and build dataset Index + 1
            Next
            Return retVal

        End Function
mathieu_cuprykAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jens FiedererTest Developer/ValidatorCommented:
You are using the tmpds Dataset while it is still null.   You have to put a new dataset in there (plus add a table, etc.).
0
Jens FiedererTest Developer/ValidatorCommented:
I can't tell what "dbManager" is, but does ExecuteDataSet REALLY not return a dataset (in which case
tmpds = dbManager.ExecuteDataSet(cmd)
would make more sense).  If it doesn't, it has a funny name for something that neither accepts nor returns a dataset!
0
mathieu_cuprykAuthor Commented:
Private dbManager As Database = DatabaseFactory.CreateDatabase("DevConfigServer")

        Public Function IncrementDate(ByVal DtPeriod As String, ByVal IncrementAmount As Integer) As String
            Dim strResults As String
            Dim intYear As Integer
            Dim intMonth As Integer
            intYear = Int32.Parse(DtPeriod.Substring(0, 4))
            intMonth = Int32.Parse(DtPeriod.Substring(4, 2))
            intMonth += IncrementAmount
            If intMonth > 12 Then
                intYear += 1
                intMonth -= 12
            End If
            strResults = intYear.ToString & intMonth.ToString().PadLeft(2, "0")
            'strResults = intYear.ToString & intMonth.ToString
            Return strResults
        End Function
     

        Public Function GetBusinessCalendarPeriod(ByVal dbName As String, ByVal DatePeriod As String, ByVal txtMaxPeriod As String) As DataTable

            Dim retVal As New DataTable("BusinessCalendars")
            Dim DtPeriod As String = DatePeriod
            Dim MaxPeriod As Integer = Int32.Parse(txtMaxPeriod)
            Dim cmd As DbCommand
            Dim tmpds As DataSet
            Dim StoredProcName As String = String.Empty


            'We get the divisions
            For Index As Integer = 0 To MaxPeriod - 1
                ' Get date period
                StoredProcName = dbName & ".dbo.usp_GetDatePeriodFromTo"
                cmd = dbManager.GetStoredProcCommand(StoredProcName)

                dbManager.AddInParameter(cmd, "@YEARMONTH", DbType.String, DtPeriod)
                ' Put the yearmonth, start date ,and end date in a dataset
                tmpds.Tables(0).Rows(Index).Item(0) = dbManager.ExecuteDataSet(cmd)
                DtPeriod = IncrementDate(DtPeriod, Index)


                ' Add to dataset and build dataset Index + 1
            Next
            Return retVal

        End Function
Can you help me set this up.
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Jens FiedererTest Developer/ValidatorCommented:
Have you TRIED
     tmpds = dbManager.ExecuteDataSet(cmd)
instead of
     tmpds.Tables(0).Rows(Index).Item(0) = dbManager.ExecuteDataSet(cmd)
?

It makes a lot more sense that way.
0
mathieu_cuprykAuthor Commented:
How will I loop through the dataset and store each row that executes.
0
theGhost_k8Database ConsultantCommented:
Dim tmpds As DataSet
I am not able to see NEW DATASET(...) for tmpds
0
theGhost_k8Database ConsultantCommented:
tmpds.Tables(0).
this should show value nothing check in debug mode
0
mathieu_cuprykAuthor Commented:
Now I am very confused. What exactly do you want me to try. I appreciate any help.
0
theGhost_k8Database ConsultantCommented:
 Public Function GetBusinessCalendarPeriod(ByVal dbName As String, ByVal DatePeriod As String, ByVal txtMaxPeriod As String) As DataTable

            Dim retVal As New DataTable("BusinessCalendars")
            Dim DtPeriod As String = DatePeriod
            Dim MaxPeriod As Integer = Int32.Parse(txtMaxPeriod)
            Dim cmd As DbCommand
            Dim tmpds As DataSet <<<############## UNABLE TO FIND DEFINATION/ALLOCATION FOR DS
            Dim StoredProcName As String = String.Empty


            'We get the divisions
            For Index As Integer = 0 To MaxPeriod - 1
                ' Get date period
                StoredProcName = dbName & ".dbo.usp_GetDatePeriodFromTo"
                cmd = dbManager.GetStoredProcCommand(StoredProcName)

                dbManager.AddInParameter(cmd, "@YEARMONTH", DbType.String, DtPeriod)
                ' Put the yearmonth, start date ,and end date in a dataset

              ########HENCE ITS WILL BE ERROR HERE############  
              tmpds.Tables(0).Rows(Index).Item(0) = dbManager.ExecuteDataSet(cmd)
                DtPeriod = IncrementDate(DtPeriod, Index)


####### SO BEFORE ASSIGNING ANY VAUES TO DS U DO
tmpds = new dataset()
0
mathieu_cuprykAuthor Commented:
tmpds.Tables(0).Rows(Index).Item(0) = dbManager.ExecuteDataSet(cmd)
                DtPeriod = IncrementDate(DtPeriod, Index)
what should this be?
0
mathieu_cuprykAuthor Commented:
ublic Function GetBusinessCalendarPeriod(ByVal dbName As String, ByVal DatePeriod As String, ByVal txtMaxPeriod As String) As DataTable

            Dim retVal As New DataTable("BusinessCalendars")
            Dim DtPeriod As String = DatePeriod
            Dim MaxPeriod As Integer = Int32.Parse(txtMaxPeriod)
            Dim cmd As DbCommand
            Dim tmpds As DataSet = New DataSet("BUSINESS_CALENDAR")
            Dim StoredProcName As String = String.Empty
            Dim DateIndex As Integer

            'We get the divisions
            For Index As Integer = 0 To MaxPeriod - 1
                ' Get date period
                StoredProcName = dbName & ".dbo.usp_GetDatePeriodFromTo"
                cmd = dbManager.GetStoredProcCommand(StoredProcName)
                dbManager.AddInParameter(cmd, "@YEARMONTH", DbType.String, DtPeriod)
                DtPeriod = IncrementDate(DtPeriod, DateIndex)
                DateIndex = Index + 1

                ' Put the yearmonth, start date ,and end date in a dataset
                tmpds. = dbManager.ExecuteDataSet(cmd)
            Next
            Return retVal

        End Function
0
mathieu_cuprykAuthor Commented:
Still getting an error {"Cannot find table 0."}
0
mathieu_cuprykAuthor Commented:
tmpds.Tables(0).Rows(Index).Item(0) = dbManager.ExecuteDataSet(cmd)
0
mathieu_cuprykAuthor Commented:
Index out of range exception.

Public Function GetBusinessCalendarPeriod(ByVal dbName As String, ByVal DatePeriod As String, ByVal txtMaxPeriod As String) As DataTable

            Dim retVal As New DataTable("BusinessCalendars")
            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

            'We get the divisions
            For Index As Integer = 0 To MaxPeriod - 1
                ' Get date period
                StoredProcName = dbName & ".dbo.usp_GetDatePeriodFromTo"
                cmd = dbManager.GetStoredProcCommand(StoredProcName)
                dbManager.AddInParameter(cmd, "@YEARMONTH", DbType.String, DtPeriod)
                DtPeriod = IncrementDate(DtPeriod, DateIndex)
                DateIndex = Index + 1
                ' Put the yearmonth, start date ,and end date in a dataset
                tmpds.Tables(0).Rows(Index).Item(0) = dbManager.ExecuteDataSet(cmd)


            Next
            Return retVal

        End Function
0
Jens FiedererTest Developer/ValidatorCommented:
dbManager.ExecuteDataSet(cmd) RETURNS a dataset.

There is NO REASON for you to do tmpds As DataSet = new DataSet()

Even if you create this dataset, it will have no tables in it.  Then if you create a table, it wil have no rows.  Then if you create a row , it will have no items.  If you DID create an item, then you could assign SOMETHING to
      tmpds.Tables(0).Rows(Index).Item(0)
but I don't think a whole dataset would "fit"!

Since you already HAVE a comment that gives you a prepared dataset,
You should do
       Dim tmpds As DataSet = dbManager.ExecuteDataSet(cmd)
where you now do
       tmpds.Tables(0).Rows(Index).Item(0) = dbManager.ExecuteDataSet(cmd)
0
mathieu_cuprykAuthor Commented:
'We get the divisions
            For Index As Integer = 0 To MaxPeriod - 1
                ' Get date period
                StoredProcName = dbName & ".dbo.usp_GetDatePeriodFromTo"
                cmd = dbManager.GetStoredProcCommand(StoredProcName)
                dbManager.AddInParameter(cmd, "@YEARMONTH", DbType.String, DtPeriod)
                DtPeriod = IncrementDate(DtPeriod, DateIndex)
                DateIndex = Index + 1
                ' Put the yearmonth, start date ,and end date in a dataset
                Dim myTable As DataTable = tmpds.Tables(0)
                Dim row As DataRow = myTable.NewRow()
                row.ItemArray = dbManager.ExecuteDataSet(cmd) '[your object array]
                '' or: row[ColumName]=value;
                myTable.Rows.Add(row)

                ' tmpds.Tables("BUSINESS_CALENDAR").Rows(Index)(0) = dbManager.ExecuteDataSet(cmd)


            Next
0
mathieu_cuprykAuthor Commented:
       'System.Data.DataSet' cannot be converted to '1-dimensional on
row.ItemArray = dbManager.ExecuteDataSet(cmd) '[your object array]

0
Jens FiedererTest Developer/ValidatorCommented:
dbManager.ExecuteDataSet(cmd)
RETURNS A DATASET!!!!

Do NOT stick this dataset into a ROW or a CELL or an ARRAY variable.

It WANTS to be put into a variable of type DATASET.

Like tmpds, for instance.
0
mathieu_cuprykAuthor Commented:
For Index As Integer = 0 To MaxPeriod - 1
                ' Get date period
                StoredProcName = dbName & ".dbo.usp_GetDatePeriodFromTo"
                cmd = dbManager.GetStoredProcCommand(StoredProcName)
                dbManager.AddInParameter(cmd, "@YEARMONTH", DbType.String, DtPeriod)
                DtPeriod = IncrementDate(DtPeriod, DateIndex)
                DateIndex = Index + 1
                ' Put the yearmonth, start date ,and end date in a dataset
                tmpds = dbManager.ExecuteDataSet(cmd)
                Dim myTable As DataTable = tmpds.Tables(0)
                Dim row As DataRow = myTable.NewRow()
                ''row.ItemArray =
                '' or: row[ColumName]=value;
                myTable.Rows.Add(row)

            Next
            Return ret
0
Jens FiedererTest Developer/ValidatorCommented:
OK, now what are you trying to do?

It looks like you are trying to add a row to the dataset you just got back.  What do you want to have INSIDE that row?
0
mathieu_cuprykAuthor Commented:
three columns 2004 01     2004 01 25      2004 01 28
I will have at least 13 rows with 3 columns

How should I handle this?

Public Function GetBusinessCalendarPeriod(ByVal dbName As String, ByVal DatePeriod As String, ByVal txtMaxPeriod As String) As DataTable

Dim retVal As New DataTable("BusinessCalendars")

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

'We get the divisions

For Index As Integer = 0 To MaxPeriod - 1

' Get date period

StoredProcName = dbName & ".dbo.usp_GetDatePeriodFromTo"

cmd = dbManager.GetStoredProcCommand(StoredProcName)

dbManager.AddInParameter(cmd, "@YEARMONTH", DbType.String, DtPeriod)

DtPeriod = IncrementDate(DtPeriod, DateIndex)

DateIndex = Index + 1

' Put the yearmonth, start date ,and end date in a dataset

tmpds = dbManager.ExecuteDataSet(cmd)

Dim myTable As DataTable = tmpds.Tables(0)

Dim row As DataRow = myTable.NewRow()

row.ItemArray = '[your object array]   How should I handle this?

'' or: row[ColumName]=value;

myTable.Rows.Add(row)

Next

Return retVal

0
mathieu_cuprykAuthor Commented:
Public Function GetBusinessCalendarPeriod(ByVal dbName As String, ByVal DatePeriod As String, ByVal txtMaxPeriod As String) As DataTable

Dim retVal As New DataTable("BusinessCalendars")

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

'We get the divisions

For Index As Integer = 0 To MaxPeriod - 1

' Get date period

StoredProcName = dbName & ".dbo.usp_GetDatePeriodFromTo"

cmd = dbManager.GetStoredProcCommand(StoredProcName)

dbManager.AddInParameter(cmd, "@YEARMONTH", DbType.String, DtPeriod)

DtPeriod = IncrementDate(DtPeriod, DateIndex)

DateIndex = Index + 1

'Put the yearmonth, start date ,and end date in a dataset

tmpds = dbManager.ExecuteDataSet(cmd)        ' This will put it in a dataset
 
Dim myTable As DataTable = tmpds.Tables(0)   ' This will put it in a table

Dim row As DataRow = myTable.NewRow()        ' Create new row as the index in the for increases
                                             

row.ItemArray =  bManager.ExecuteDataSet(cmd).Tables[0].Rows[Index].ItemArray


myTable.Rows.Add(row)


Next

Return retVal

SOLVED
0
Jens FiedererTest Developer/ValidatorCommented:
With "SOLVED", do you mean that worked for you?

It seems unlikely.

You return retVal, which is a DataTable that has not been touched since it was created.

Maybe THAT is the table in which you should be adding rows?

In that case, you would pull data from tmpds (which you got from the stored procedure) and put it into retVal?

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("BusinessCalendars")
Dim DtPeriod As String = DatePeriod
Dim MaxPeriod As Integer = Int32.Parse(txtMaxPeriod)
Dim StoredProcName As String = dbName & ".dbo.usp_GetDatePeriodFromTo"
Dim DateIndex As Integer

'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.GetStoredProcCommand(StoredProcName)
dbManager.AddInParameter(cmd, "@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(cmd)        ' This will put it in a dataset
row.ItemArray =  tmpds.Tables[0].Rows[Index].ItemArray

Next

Return retVal


maybe?
0
mathieu_cuprykAuthor Commented:
row.ItemArray =  tmpds.Tables[0].Rows[Index].ItemArray
Input array is longer than the number of columns in this table.

Hmmmm,,,
0
mathieu_cuprykAuthor Commented:
For Index As Integer = 0 To MaxPeriod - 1
                ' Get date period
                StoredProcName = dbName & ".dbo.usp_GetDatePeriodFromTo"
                cmd = dbManager.GetStoredProcCommand(StoredProcName)
                dbManager.AddInParameter(cmd, "@YEARMONTH", DbType.String, DtPeriod)
                DtPeriod = IncrementDate(DtPeriod, DateIndex)
                DateIndex = Index + 1
                ' Put the yearmonth, start date ,and end date in a dataset

                Dim myDs As DataSet = dbManager.ExecuteDataSet(cmd)

                Dim myTable As DataTable = myDs.Tables(0)

                Dim myRow As DataRow = myTable.Rows(Index)

                Dim row As DataRow = myTable.NewRow()

                row.ItemArray = myRow.ItemArray
0
Jens FiedererTest Developer/ValidatorCommented:
> Input array is longer than the number of columns in this table.

So you need to set up the columns in the output array before you try to copy.

Your second try,

                Dim myTable As DataTable = myDs.Tables(0)
                Dim myRow As DataRow = myTable.Rows(Index)
                Dim row As DataRow = myTable.NewRow()
                row.ItemArray = myRow.ItemArray
has the problem that the DataTable to which you are adding data is replaced every time you go through the loop.

I THINK this is not what you want (it is hard to tell because you haven't described your goals - maybe, if your English is not good, you could describe in French or German or Spanish?)

More likely you want to do, OUTSIDE the loop
Dim retVal As New DataTable("BusinessCalendars")
retVal.Columns.Add("yearmonth")
retVal.Columns.Add("startdate")
retVal.Columns.Add("enddate")

or something like that.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mathieu_cuprykAuthor Commented:
More likely you want to do, OUTSIDE the loop
Dim retVal As New DataTable("BusinessCalendars")
retVal.Columns.Add("yearmonth")
retVal.Columns.Add("startdate")
retVal.Columns.Add("enddate")


I will put the values into a datatable.

and have it in a retval after I get this thing running.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.