none
Easy LINQ and VB.net Question RRS feed

  • Question

  • I have a simple LINQ and VB.net application that I am building that is a VSTO extention to Excel. I have made a LINQ to SQL connect, retrieved a bunch of data, processed it and now need to INSERT it into a SQL Table. My issue is that I have the processed data an multidementional array that I am trying to write to the SQL database through link. The process should generate 16,800 rows in the database. The VB.net code I have written is only giving me the last record when the insert is done to SQL. I think my problem is that I am reusing the same variable, but I am not sure how to make this work better. Any help would be greatly appreciated.

        Dim deplist As New List(Of WellDepletion)
        Dim dep As New WellDepletion
    
        For a = 1 To UBound(out, 1) 'each well
          For b = 1 To UBound(out, 2) 'each monthly step
            dep.WellNo = a
            dep.Mnth = b
            dep.Dep = out(a, b)
            deplist.Add(dep)
          Next b
        Next a
    
        db.WellDepletions.InsertAllOnSubmit(deplist)
        db.SubmitChanges()
    


    Heath
    Friday, August 5, 2011 3:55 PM

Answers

  • Hi heath140;

    You need to create a new instance of WellDepletion for each entry into the db. I have modified you code below, try it this way.

    Dim deplist As New List(Of WellDepletion)    
    Dim dep As WellDepletion          
                            
    For a = 1 To UBound(out, 1) 'each well     
     For b = 1 To UBound(out, 2) 'each monthly step
      dep = New WellDepletion
      dep.WellNo = a               
      dep.Mnth = b                
      dep.Dep = out(a, b)             
      deplist.Add(dep)              
     Next b                    
    Next a                     
                            
    db.WellDepletions.InsertAllOnSubmit(deplist)  
    db.SubmitChanges()
    

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by heath140 Monday, August 8, 2011 2:11 PM
    Friday, August 5, 2011 8:57 PM