Solved

row.ItemArray = myRow.ItemArray not set to an instance of an object.

Posted on 2007-04-04
2
549 Views
Last Modified: 2012-05-05
Object reference not set to an instance of an object.

 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)
                ' Put the yearmonth, start date ,and end date in a dataset
                myDs = dbManager.ExecuteDataSet(cmd)

                ' check myTable first
                Dim myRow As DataRow

                Dim myTable As DataTable = myDs.Tables(0)

                If myTable.Rows.Count >= 1 Then
                    myRow = myTable.Rows(Index)
                End If

                Dim row As DataRow = myTable.NewRow()

                row.ItemArray = myRow.ItemArray

                myTable.Rows.Add(row)  


            Next
           
0
Comment
Question by:mathieu_cupryk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 10

Accepted Solution

by:
Si_Hibbard earned 500 total points
ID: 18850568
This looks wrong to me - first why is the query in the loop, and then you are doing an assignment
row.ItemArray = myRow.ItemArray
even if you have never set myRow to anything

The error is telling you that you are trying to assign something to an object that has no value (has not been initialised) - eg -> "myRow.ItemArray" does not have a value

maybe you want something like the following - i cannot test it but it makes more sense from what i see:::

 ' Get date period

StoredProcName = dbName & ".dbo.usp_GetDatePeriodFromTo"
cmd = dbManager.GetStoredProcCommand(StoredProcName)
dbManager.AddInParameter(cmd, "@YEARMONTH", DbType.String, DtPeriod)


DtPeriod = IncrementDate(DtPeriod, DateIndex)
' Put the yearmonth, start date ,and end date in a dataset
myDs = dbManager.ExecuteDataSet(cmd)

Dim myTable As DataTable = myDs.Tables(0)

'loop
For Index As Integer = 0 To MaxPeriod - 1
   
                ' check myTable first
                Dim myRow As DataRow

                If myTable.Rows.Count >= 1 Then
                    myRow = myTable.Rows(Index)          
                    Dim row As DataRow = myTable.NewRow()
                    row.ItemArray = myRow.ItemArray
                    myTable.Rows.Add(row)  
               End If

            Next
0
 

Author Comment

by:mathieu_cupryk
ID: 18851016
Good job!!!
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Sort GridView by ID Descending 1 26
asp web application 3 43
C# Service FileSystemwatcher isse 9 47
How to escape alias names for Oracle query 1 25
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

730 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