Solved

Object reference not set to an instance of an object.

Posted on 2007-04-03
27
917 Views
Last Modified: 2010-05-18
       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
0
Comment
Question by:mathieu_cupryk
  • 16
  • 8
  • 3
27 Comments
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 18843588
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
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 18843635
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
 

Author Comment

by:mathieu_cupryk
ID: 18843683
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
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 18844416
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
 

Author Comment

by:mathieu_cupryk
ID: 18844886
How will I loop through the dataset and store each row that executes.
0
 
LVL 21

Expert Comment

by:theGhost_k8
ID: 18844963
Dim tmpds As DataSet
I am not able to see NEW DATASET(...) for tmpds
0
 
LVL 21

Expert Comment

by:theGhost_k8
ID: 18844972
tmpds.Tables(0).
this should show value nothing check in debug mode
0
 

Author Comment

by:mathieu_cupryk
ID: 18845003
Now I am very confused. What exactly do you want me to try. I appreciate any help.
0
 
LVL 21

Expert Comment

by:theGhost_k8
ID: 18845319
 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
 

Author Comment

by:mathieu_cupryk
ID: 18845497
tmpds.Tables(0).Rows(Index).Item(0) = dbManager.ExecuteDataSet(cmd)
                DtPeriod = IncrementDate(DtPeriod, Index)
what should this be?
0
 

Author Comment

by:mathieu_cupryk
ID: 18845526
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
 

Author Comment

by:mathieu_cupryk
ID: 18845559
Still getting an error {"Cannot find table 0."}
0
 

Author Comment

by:mathieu_cupryk
ID: 18845571
tmpds.Tables(0).Rows(Index).Item(0) = dbManager.ExecuteDataSet(cmd)
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:mathieu_cupryk
ID: 18845613
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
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 18845876
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
 

Author Comment

by:mathieu_cupryk
ID: 18845881
'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
 

Author Comment

by:mathieu_cupryk
ID: 18845897
       'System.Data.DataSet' cannot be converted to '1-dimensional on
row.ItemArray = dbManager.ExecuteDataSet(cmd) '[your object array]

0
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 18845960
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
 

Author Comment

by:mathieu_cupryk
ID: 18845976
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
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 18846026
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
 

Author Comment

by:mathieu_cupryk
ID: 18846074
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
 

Author Comment

by:mathieu_cupryk
ID: 18846625
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
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 18846790
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
 

Author Comment

by:mathieu_cupryk
ID: 18847836
row.ItemArray =  tmpds.Tables[0].Rows[Index].ItemArray
Input array is longer than the number of columns in this table.

Hmmmm,,,
0
 

Author Comment

by:mathieu_cupryk
ID: 18847850
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
 
LVL 23

Accepted Solution

by:
Jens Fiederer earned 500 total points
ID: 18849891
> 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
 

Author Comment

by:mathieu_cupryk
ID: 18849965
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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Using MS Code on my Mac 6 45
Duplicate a row 2 33
Default parameter problem in C# 3 27
XML & .net 5 20
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now