C
C#2y ago
Jimmacle

✅ help with error

not to be sassy, but we can't read your mind
109 Replies
Jimmacle
JimmacleOP2y ago
we need to know the error and more of the code around your problem
🙈
🙈2y ago
No I’ll explain it So it is in a method which edits the database records The only problem is I am struggling it edit the user details As I get this error
Jimmacle
JimmacleOP2y ago
do you understand what the error means?
titan 1-1
titan 1-12y ago
frist name
🙈
🙈2y ago
Nope Chill
Jimmacle
JimmacleOP2y ago
it means either OrderSubmited or Users is null so you should use your debugger and see which one it is
🙈
🙈2y ago
It has to be user cus I when I removed users it worked I will try hard code the values for user to see if it works
titan 1-1
titan 1-12y ago
Is english your second language
Jimmacle
JimmacleOP2y ago
can you avoid commenting unless you have something useful to contribute?
titan 1-1
titan 1-12y ago
he has so many spelling mistakes in his code, that was my first idea
🙈
🙈2y ago
Ya
Jimmacle
JimmacleOP2y ago
spelling mistakes would most likely lead to a compile time error which isn't the case here as long as the spelling is consistent it doesn't really matter
🙈
🙈2y ago
This is when I hard code it
Jimmacle
JimmacleOP2y ago
hard code what?
🙈
🙈2y ago
Instead of getting it from the from I just set the value of user in the controller
Jimmacle
JimmacleOP2y ago
can you use your debugger and check what's actually null? it's faster than guessing
🙈
🙈2y ago
Is that the red one
Jimmacle
JimmacleOP2y ago
$debug
MODiX
MODiX2y ago
Tutorial: Debug C# code and inspect data - Visual Studio (Windows)
Learn features of the Visual Studio debugger and how to start the debugger, step through code, and inspect data in a C# application.
🙈
🙈2y ago
I will try Where do I add brake point Yes it looks like all the user feilds are null as the code stop working there and will not move one from there
Jimmacle
JimmacleOP2y ago
are the user fields null or is the .Users property itself null?
🙈
🙈2y ago
Null means it is empty right but how can it be empty if I hard code it and be like first name = “hi”
Jimmacle
JimmacleOP2y ago
you're skipping a step
🙈
🙈2y ago
All the user feilds ig
Jimmacle
JimmacleOP2y ago
you have an order object, then a user object, then the user's properties one of the first two is null, because you wouldn't get a NRE by assigning a value to a property that is null something higher in the chain has to be
🙈
🙈2y ago
Ummm What User is foreign key
Jimmacle
JimmacleOP2y ago
do you ever actually assign a user object to that property? because if you don't it will be null
🙈
🙈2y ago
I have a foreign userid field in the orders table model. I also have public user users in the order model
Jimmacle
JimmacleOP2y ago
but do you ever give it a value? like Order.Users = new User()
🙈
🙈2y ago
No I don’t think I though changing the foreign key will auto do this
Jimmacle
JimmacleOP2y ago
no
🙈
🙈2y ago
Ok I see
Jimmacle
JimmacleOP2y ago
foreign key just relates entries in your database it doesn't mean it creates one by default
🙈
🙈2y ago
Oh ok I will try add it on the place I make orders Create orders I mean Wait is that the right place how will it know the foreign key is the new user I want to make
Jimmacle
JimmacleOP2y ago
where should the user object be coming from? are you creating a user when you create the order or does the user already exist?
🙈
🙈2y ago
The user already exists When they sign up using asp.net identif Plus I have a class that inherent form that the user class
Jimmacle
JimmacleOP2y ago
so you'll need to look up the user in your database and assign that to your order's user property
🙈
🙈2y ago
Um
Pobiega
Pobiega2y ago
All you've shown is 3 lines of code. We have no idea how OrderSubmitted is created or what properties has values or what those values are, based on what you've shown us.
🙈
🙈2y ago
So ain’t that what the foreign key is for
Jimmacle
JimmacleOP2y ago
no
🙈
🙈2y ago
We found the error
Jimmacle
JimmacleOP2y ago
foreign keys are constraints, in this case you're saying "this column needs to reference a valid ID from the users table" it doesn't do anything beyond that
🙈
🙈2y ago
So how do I find that user and assign it
Jimmacle
JimmacleOP2y ago
if you need to set it to a specific user you have to do that yourself how do you find other things in your database?
🙈
🙈2y ago
Egear loading with foreign keys Is that what you ment
Jimmacle
JimmacleOP2y ago
not really you already know the specific thing you're looking for (a user) so you can query that table directly using the Users collection on your context or whatever it's called for identity probably AspNetUsers?
🙈
🙈2y ago
How
Jimmacle
JimmacleOP2y ago
the same way you query for other things in your database
🙈
🙈2y ago
Ok let me try Got it thank you very very much You are so smart thank you for helping me @Jimmacle i was so sure it was going to work but unfortunately it did not In the same method I added var curuser = _context.User.fristordefault(a=> ordersumited.id) I still get the null error
Pobiega
Pobiega2y ago
use the debugger, check what is null.
🙈
🙈2y ago
@Pobiega the thing that is null is the
Pobiega
Pobiega2y ago
User yes
🙈
🙈2y ago
all the user values one sec ill show you the error loading my pc is very slow there
Pobiega
Pobiega2y ago
thats not what that error means at all cuser is what is null here
🙈
🙈2y ago
ok then i see the problem has to be with my querry Var cuser = _context.User.FirstOrdefault(a => a.id == OrderSumbited.userId) dose anything look wrong there
Pobiega
Pobiega2y ago
you do understand that FirstOrDefault can return null right? if you don't want to allow nulls, use First instead
🙈
🙈2y ago
yes but there is 100% a user how
Pobiega
Pobiega2y ago
then use First
🙈
🙈2y ago
can you explain how it work to me please if you dont mind i will try
Pobiega
Pobiega2y ago
the only difference between First and FirstOrDefault is that that FoD can return null and First can't instead, first throws an exception if the query had no results
🙈
🙈2y ago
ok I see
Pobiega
Pobiega2y ago
if you expect a user to exist 100% of the time, you use First because a user not existing is an exception to your expected state
🙈
🙈2y ago
So this Means there is no user
Pobiega
Pobiega2y ago
bruh why are you reposting that same image change your code, recompile, try again there you go First threw an exception, because there was no results in the query
🙈
🙈2y ago
thats werid becuase a user class exist and if we look at the database
Pobiega
Pobiega2y ago
well, EF doesn't agree
🙈
🙈2y ago
a user dose belong to this order do you want to see the database would do you think i should do
Pobiega
Pobiega2y ago
is your connection string correct? if you try and get all users in the endpoint and just look at the results with the debugger, do you see all the users you expect?
🙈
🙈2y ago
conncetion stirng is right i am new to runing the debug thing
Pobiega
Pobiega2y ago
well, learn how to use it
🙈
🙈2y ago
yup
Pobiega
Pobiega2y ago
its a very important tool that is not optional
🙈
🙈2y ago
yup one sec so do i need to add brake points
Pobiega
Pobiega2y ago
yes, thats how you engage the debugger
🙈
🙈2y ago
one sec trying when i however over a.id nothing happens it dosent show a vlaue like the others i know how to run debug know
Pobiega
Pobiega2y ago
you skipped half of what I said then?
🙈
🙈2y ago
the value of orderSumbited.userId is 512334-etc
Pobiega
Pobiega2y ago
okay.
🙈
🙈2y ago
which matchs order 19 in the order tabel
Pobiega
Pobiega2y ago
and you can manually confirm that it EXACTLY matches the expected id?
🙈
🙈2y ago
ok will try
Pobiega
Pobiega2y ago
wait a second show the query where you are doing the order lookup
🙈
🙈2y ago
ok $rotate actually has nothing to do with orders it just checking if there is a user with that id
Pobiega
Pobiega2y ago
doesnt the order have a foreign key and a navigation property for the user?
🙈
🙈2y ago
yes
Pobiega
Pobiega2y ago
well there you go I don't see a .Include in your query
🙈
🙈2y ago
before you said to hard code it
Pobiega
Pobiega2y ago
no I didn't 1: I'm not Jimmacle 2: You have not shown this code before
🙈
🙈2y ago
it was id == orderSumbited.userId ya oh nvm you never said that why would this not work
Pobiega
Pobiega2y ago
unrelated use .Include on the order query to include the user nav prop
🙈
🙈2y ago
but aint it just like anther variable checking if it matchs ok i will try but i jsut want to know why so i dotn make the same mistkae again
Pobiega
Pobiega2y ago
Well, you seem to not know how EF works at all, or how C# works very well either
🙈
🙈2y ago
ah true i am a beginer
Pobiega
Pobiega2y ago
and its kinda hard to communicate with you too, you seem to misunderstand just about everything I say :p
🙈
🙈2y ago
my bad sorry
Pobiega
Pobiega2y ago
Not saying this to make you feel bad, rather to let you know what to work on.
🙈
🙈2y ago
yup thank you @Pobiega Is that what you ment
Pobiega
Pobiega2y ago
yes
🙈
🙈2y ago
Thanks
Pobiega
Pobiega2y ago
why are you photographing instead of screenshotting?
🙈
🙈2y ago
I have no space on laptop for discord @Pobiega do you know any quick fixes to that or any reason that it is not working
Pobiega
Pobiega2y ago
I'm not reading that lol
Pobiega
Pobiega2y ago
No description
🙈
🙈2y ago
Better
Pobiega
Pobiega2y ago
no its still vertical lol like, are you blind? your pictures are incorrectly rotated no hits as in, you are using First, but your query has no results
🙈
🙈2y ago
Is there anything you can you see wrong with my query Cus when I don’t include users it returns somthing ya but why
Pobiega
Pobiega2y ago
Do something yourself for once, maybe?
🙈
🙈2y ago
ight yup

Did you find this page helpful?