109 Replies
we need to know the error and more of the code around your problem
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
do you understand what the error means?
frist name
Nope
Chill
it means either
OrderSubmited
or Users
is null
so you should use your debugger and see which one it isIt 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
Is english your second language
can you avoid commenting unless you have something useful to contribute?
he has so many spelling mistakes in his code, that was my first idea
Ya
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
This is when I hard code it
hard code what?
Instead of getting it from the from
I just set the value of user in the controller
can you use your debugger and check what's actually null?
it's faster than guessing
Is that the red one
$debug
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.
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
are the user fields null or is the
.Users
property itself null?Null means it is empty right but how can it be empty if I hard code it and be like first name = “hi”
you're skipping a step
All the user feilds ig
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
Ummm
What
User is foreign key
do you ever actually assign a user object to that property?
because if you don't it will be null
I have a foreign userid field in the orders table model. I also have public user users in the order model
but do you ever give it a value?
like
Order.Users = new User()
No I don’t think I though changing the foreign key will auto do this
no
Ok I see
foreign key just relates entries in your database
it doesn't mean it creates one by default
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
where should the user object be coming from?
are you creating a user when you create the order or does the user already exist?
The user already exists
When they sign up using asp.net identif
Plus I have a class that inherent form that the user class
so you'll need to look up the user in your database and assign that to your order's user property
Um
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.So ain’t that what the foreign key is for
no
We found the error
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
So how do I find that user and assign it
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?
Egear loading with foreign keys
Is that what you ment
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?
How
the same way you query for other things in your database
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
use the debugger, check what is null.
@Pobiega the thing that is null is the
User
yesall the user values
one sec ill show you the error
loading
my pc is very slow
there
thats not what that error means at all
cuser
is what is null hereok 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
you do understand that
FirstOrDefault
can return null right?
if you don't want to allow nulls, use First
insteadyes but there is 100% a user
how
then use
First
can you explain how it work to me please if you dont mind i will try
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 resultsok I see
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 stateSo this Means there is no user
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 querythats werid becuase a user class exist
and if we look at the database
well, EF doesn't agree
a user dose belong to this order
do you want to see the database
would do you think i should do
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?
conncetion stirng is right
i am new to runing the debug thing
well, learn how to use it
yup
its a very important tool that is not optional
yup one sec so do i need to add brake points
yes, thats how you engage the debugger
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
you skipped half of what I said then?
the value of orderSumbited.userId is
512334-etc
okay.
which matchs order 19 in the order tabel
and you can manually confirm that it EXACTLY matches the expected id?
ok will try
wait a second
show the query
where you are doing the order lookup
ok
$rotate
actually has nothing to do with orders
it just checking if there is a user with that id
doesnt the order have a foreign key and a navigation property for the user?
yes
well there you go
I don't see a
.Include
in your querybefore you said to hard code it
no I didn't
1: I'm not Jimmacle
2: You have not shown this code before
it was id == orderSumbited.userId
ya oh nvm you never said that
why would this not work
unrelated
use
.Include
on the order query to include the user nav propbut 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
Well, you seem to not know how EF works at all, or how C# works very well either
ah true i am a beginer
and its kinda hard to communicate with you too, you seem to misunderstand just about everything I say :p
my bad
sorry
Not saying this to make you feel bad, rather to let you know what to work on.
yup
thank you
@Pobiega
Is that what you ment
yes
Thanks
why are you photographing instead of screenshotting?
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
I'm not reading that lol

Better
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 resultsIs there anything you can you see wrong with my query
Cus when I don’t include users it returns somthing
ya
but why
Do something yourself for once, maybe?
ight
yup