130 Replies
What's the signature of
FindByNoIzin
?Severity Code Description Project File Line Suppression State
Error CS0161 'PPController.FindByNoIzin(string)': not all code paths return a value
The signature for
FindByNoIzin
that's called on _repository.PP?Yes
what does it return?
I want to get data status and uraian by find data by NoIzin. If the data null return not found.
Also one question, can we connect 2 databases on 1 project? I want to access 2 different database on my web api project.
it seems like
FindByNoIzin
is returning an IEnumerable<Object>
If you can change it's return type, then change it to the type you want.So the problem is this return?
how about insert select ?
Yup, because it's probably returning an
IEnumerable<Object>
instead of IEnumerable<T>
where T has the properties you need
Select can be called on any IEnumerable
Okay thank you i will change it ๐
Can you help me answer this?
yeah, you can connect to multiple databases
Thank you ๐
Hey Kouhai, i change it but still error, did i do something wrong?
the same error as before
object doesn't contain definition of status
?Yes it has same error
I'm stuck at this
Can you paste the signature and return value type for
FindByNoIzin
?Well the problem here seems to be she creates a new object of type object but that object can never have a property p.Status
where should that come from?
an assignment is needed and it can't come from p
Look at this example to understand how you should use .Select:
I think they said they changed FindByIzin to return an enumerable<T> where T has these properties.....
still an object of type object can't have a Status property
p.Status can't exist like that
why would it be of type object if it doesn't return object?
sorry, doesn't return IEnumerable<object>
look at her first screenshot
even the IDE yells at her, that this can't be done like that ๐
p is of type object and object contains no property status
it needs to be assigned if it should exist
ik it's of type object, but they said they changed the return type...
Donโt yell at me like IDE๐
Thank you i will try to understand that
But why that works on getalldata?
what do you mean it works on getalldata
If it's still failing then FindByIzin is still returning an enumerable of objects
can you show the code of
FindByIzin
Hey sorry for late replay, i was away from my laptop. So my goal is actually want to make this. We searcg data by "NoIzin" then it get that data.
and this is my finbyizin
Not sure why
Aren't there two FindByNoIzin methods? One in the controller and one called on PP?
No, only one FindByNoIzin method in controller, so the goal for FindByNoIzin to call KdStatus and Uraian (Active n valid is data from Uraian colomn from table RefStatus)
So PP is the controller and FindByNoIzin is a recursively calling itself?
PP is the model and FindByNoIzin is in PPController.cs
NoIzin itself is from table PP
Okay, what I'm seeing is you have method in your controller the returns an IActionResult.
And you also call FindByIzin on your model, can you show the code for FindByIzin in PP?
this one in my PPRepository
Yes in my controller return IActionResult
Is PP an IEnumerable?
I think it's not, i'm not using the automapper
What classe/interfaces does it inherent/implement?
yes it does
Do you mean it implements IEnumerable?
It would still be better if you could show the class declaration ๐
I'm sorry honestly i'm abit confused cause i have so many classes PP๐
I mean which one do you want me to show you?
The one that's returned by
FindByNoIzin
ah this one?
Yeah,
PP
used by that repositoryBut i still have same error
Also response to Braniac5, im using same code for GetAllPP like this and it's work๐
what does GetAllPP return?
Does it return
PP
too?Yes GetAllPP return PP:)
Alright, again can you post the class deceleration for PP? ๐
this is my PP it's pretty long can't get all in a frame
No problem, can you also post the declaration for BaseEntity?
something is wrong here ๐
LINQ's
Select
can only be called on IEnumerable
FindByNoIzin
returns PP
instance which doesn't implement IEnumerable
.
Do you have a Select
method in PP?ah i have select method actually๐
but it's default. when my select on findByNoIzin error i tried to press Crtl+ and it gave me this one (pic. PP class)
Perfect, I assume you wanted to call LINQ's
Select
, obviously the extension method Select
can't be called on PP, so VS suggested this default implementation.
I'm not sure why you want to call select though.
When you call FindByNoIzin, you return the first result or null.
You can just remove Select
Actually i don't want to call select๐
okay i will remove it ๐
This what i mean
Remove the call to
select
too.
It's not needed, LINQ's Select
is used to map a collection of valueslike this one?
But how to get Status and Kdstatus only from FindByNoIzin?
I mean i want to map collection
But FindByNoIzin is not returning a collection, it's returning a single object
Ah i see, i was confused there
You match a condition first, and then call
FirstOrDefault
.
FirstOrDefault
returns the first object it finds in the collection, if it doesn't find anything, it returns default
for that object type.
default
for classes is nullThen what it should be?
Btw thank you so much Kouhai for helping me patiently
match a condition first? i have this one before
Do many PPs have the same NoIzin?
1 PP have 1 NoIzin same as Id
Okay, then FindByNoIzin is behaving correctly
It's returning a single object if it finds it
Ah maybe to map value i should use automapper then?
no use select
I assume you want to map PP because you don't want all it's properties to be serialized?
Yes, i only need 2 properties only since PP has so many properties.
Then just do
Ah thank you, i never thought of it๐ญ
๐
no problem
also this another problem not all path return a value?
You aren't returning in this branch
You're also not returning if result == null
So both result should be result == null?
sorry i wrong read
When your method has a return type, all your code paths just needs to have a return statement or throw an exception
Yes i realized i didn't return result == null๐
Noted
Kou the data is empty๐
Can you put a breakpoint on
return Ok(....)
and see if it's hit?I did return ok (result), it got all data, it wont call the if method after
it was return ok();
Oh, that's normal
return ok() would just return with status code
200
and no data.yes right Kou
so kou i did some modification, like this. and it gave me error like this, i think it i didn't call Uraian? Uraian is in table RefStatusIzin
this RefStatusIzin
RefStatusIzin
is not populated, are you using any ORM like EntityFramework, Dapper.. etc?I believe i'm using EntityFramework
Alright, what's the code for
FindByCondition
?this one
You need to call
Include
iirc by default EF won't load other tables.Loading Related Entities - EF6
Loading Related Entities in Entity Framework 6
I called include too, Kou
This is my RefStatusIzin
and this is for PP
Sorry sorry
The configurations looks fine to me.
I'm not sure where
Include
is called thoughSorry i think i didn't call include, i mean i don't use include, it's only configuration and models
What's the class deceleration for
RefStatusIzin
?RefStatusIzin is table where there's only description for status. So in table PP there's KdStatus, if kdStatus = IdRefStatus we get description for KdStatus, ex. IdRefStatus 1 = Active, 2 = Super Active. If KdStatus = IdRef = 2, Status = Active
This data from GetAllData, like this one
Alright,
FindByCondition is basically just a
WHERE
sql statement, it doesn't have any JOIN
.
So EF is not loading the table for RefStatusIzin
How does the code for GetAllPP()
look like?Yes you're right, that's what i want to find out how to make FindByCondition could do JOIN
This is my GetAllPP
Can you also show the code for FindAll?
This Kou
You can just call
in FindByInzin, because after AsNoTracking in FindByCondition you shouldn't do any other queries.
Kou it said not valid in the given context
Okey noted, so we need to write include too
Sorry I forgot
()
after Set<PP>
๐
OMG Kuo, thank you so much it works, ๐ญ
glad it's
why my GetDataById and FindPPByNoIzin have same route so system confused who to call so it show error return?
Are these two methods in a controller?
Yes in same controller
Can you show the attributes on both the controller and methods?
this is GetPPById
FindByNoIzin
And the class' attributes?
this is the class
Oh, I meant the controller class' attributes
this one?
Yeah, basically both methods resolve to this route
api/PP/{}
, you need to change the route of one method like this
[HttpGet("id/{Id}")]
This would change it's route to
api/PP/id/{}
and the other method can be called with the default route api/PP/{}
of course this means if the API consumer
makes a call like this
api/PP/id
without adding the id it would resolve to the method handling this route api/PP/{}
Thank you so much Kou, i will take that in my note. It's working now
no worries
โ
This post has been marked as answered!