-3

I'm working on a Sindows Forms application to help keep inventory of some scanners. I'm using Linq2Sql, each table has an id column. On my repair history form. I'm trying to use the serial number from the inventory table so it goes to the database and looks up the sID from the table and it returns the correct value, but when I go to send all the entered data to the history table it gets a null reference exception.

Dim db As New DataClasses1DataContext
Dim rep As Scanner_Repair_History

Dim scan = (From Scanner_Inventory In db.Scanner_Inventories Where scannerid.Text = Scanner_Inventory.SN Select Scanner_Inventory.SID).FirstOrDefault

rep.SID = scan
rep.Date_Broken = datebroke.Value
rep.Description = description.Text
rep.Send_Date = senddate.Text
rep.Recieve_Date = recievedate.Text
rep.Cost = cost.Text
rep.PlantID = plantid.Text
rep.BID = brokenid.Text
rep.RMAnumber = rmanum.Text

db.Scanner_Repair_Histories.InsertOnSubmit(rep)
db.SubmitChanges()
GSerg
  • 71,102
  • 17
  • 141
  • 299
Johnathan
  • 1
  • 1
  • 2
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Bugs Nov 25 '16 at 16:36

2 Answers2

0

is that me but you didn't instanciate your "rep" variable

0

You don't have a defined object for placement with a 'new' keyword but I am also curious if it is a system.type.

Update based on Jinx88909 You may be returning an entire POCO Object that may be null and have a null property. You can adjust this most times by doing a null condition if you are using .NET 4.5 and up. '?.' operator.

Dim db As New DataClasses1DataContext
'I need to be a new object and not instantiated as Nothing
Dim rep As New Scanner_Repair_History

'You have the potential for a nothing value here as 'FirstOrDefault' includes a potential Nothing'. 
'I would get the entire object and then just a property of it after the fact
Dim scan = (From Scanner_Inventory In db.Scanner_Inventories Where scannerid?.Text = Scanner_Inventory?.SN Select Scanner_Inventory).FirstOrDefault?.Sid 

If scan IsNot Nothing Then
  rep.SID = scan 'Could you maybe want scan.Id or something similar?
  rep.Date_Broken = datebroke.Value
  rep.Description = description.Text
  rep.Send_Date = senddate.Text
  rep.Recieve_Date = recievedate.Text
  rep.Cost = cost.Text
  rep.PlantID = plantid.Text
  rep.BID = brokenid.Text
  rep.RMAnumber = rmanum.Text

  db.Scanner_Repair_Histories.InsertOnSubmit(rep)
  db.SubmitChanges()
Else
  Console.WriteLine("I got nothing for you with the inputs you put in!")
End If
djangojazz
  • 12,570
  • 9
  • 48
  • 82
  • using the isnot nothing just jumps down to the inserts into the table. from what i can tell when i start the program 'rep' is pointing to the 'Scanner_Repair_History' table like its suppose to, but when it jumps down past the query 'rep' changes to 'nothing'. im not entirely sure what the cause is. and 'scan' does return the correct id for the scaner SN – Johnathan Nov 25 '16 at 19:32