C
C#โ€ข2y ago
daisy

Can someone help me i got this error? [Answered]

Error on status and ref status
130 Replies
canton7
canton7โ€ข2y ago
What's the signature of FindByNoIzin?
daisy
daisyโ€ข2y ago
Severity Code Description Project File Line Suppression State Error CS0161 'PPController.FindByNoIzin(string)': not all code paths return a value
Kouhai
Kouhaiโ€ข2y ago
The signature for FindByNoIzin that's called on _repository.PP?
daisy
daisyโ€ข2y ago
Yes
Kouhai
Kouhaiโ€ข2y ago
what does it return?
daisy
daisyโ€ข2y ago
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.
Kouhai
Kouhaiโ€ข2y ago
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.
daisy
daisyโ€ข2y ago
So the problem is this return? how about insert select ?
Kouhai
Kouhaiโ€ข2y ago
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
daisy
daisyโ€ข2y ago
Okay thank you i will change it ๐Ÿ™‚ Can you help me answer this?
Kouhai
Kouhaiโ€ข2y ago
yeah, you can connect to multiple databases
daisy
daisyโ€ข2y ago
Thank you ๐Ÿ™‚
daisy
daisyโ€ข2y ago
Hey Kouhai, i change it but still error, did i do something wrong? crying
Kouhai
Kouhaiโ€ข2y ago
the same error as before object doesn't contain definition of status?
daisy
daisyโ€ข2y ago
Yes it has same error I'm stuck at this
Kouhai
Kouhaiโ€ข2y ago
Can you paste the signature and return value type for FindByNoIzin?
Brainiac V
Brainiac Vโ€ข2y ago
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:
List<Employee> selectMethod = Employee.GetEmployees()
.Select(emp => new Employee()
{
FirstName = emp.FirstName,
LastName = emp.LastName,
Salary = emp.Salary
}).ToList();
List<Employee> selectMethod = Employee.GetEmployees()
.Select(emp => new Employee()
{
FirstName = emp.FirstName,
LastName = emp.LastName,
Salary = emp.Salary
}).ToList();
Kouhai
Kouhaiโ€ข2y ago
I think they said they changed FindByIzin to return an enumerable<T> where T has these properties.....
Brainiac V
Brainiac Vโ€ข2y ago
still an object of type object can't have a Status property p.Status can't exist like that
Kouhai
Kouhaiโ€ข2y ago
why would it be of type object if it doesn't return object? sorry, doesn't return IEnumerable<object>
Brainiac V
Brainiac Vโ€ข2y ago
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
Kouhai
Kouhaiโ€ข2y ago
ik it's of type object, but they said they changed the return type...
daisy
daisyโ€ข2y ago
Donโ€™t yell at me like IDE๐Ÿ˜… Thank you i will try to understand that But why that works on getalldata?
Kouhai
Kouhaiโ€ข2y ago
what do you mean it works on getalldata If it's still failing then FindByIzin is still returning an enumerable of objects
Tvde1
Tvde1โ€ข2y ago
can you show the code of FindByIzin
daisy
daisyโ€ข2y ago
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.
daisy
daisyโ€ข2y ago
and this is my finbyizin
[HttpGet("{NoIzin}")]
public IActionResult FindByNoIzin(string NoIzin)
{
try
{
var result = _repository.PP.FindByNoIzin(NoIzin)
.Select(p => new
{
p.Status,
Uraian = p.RefStatusIzin.Uraian
});
if (result != null)
{
if (result.Status == null)
{
Console.WriteLine("Data not found");
}
else
{
_logger.LogInfo($"Returned data with NoIzin: {NoIzin}");
return Ok(result);
Console.WriteLine("Status: Valid");
}
}
}
catch (Exception ex)
{
_logger.LogError($"Something went wrong inside GetSampleById action: {ex.Message}");
return StatusCode(500, "Internal server error");
}
}
[HttpGet("{NoIzin}")]
public IActionResult FindByNoIzin(string NoIzin)
{
try
{
var result = _repository.PP.FindByNoIzin(NoIzin)
.Select(p => new
{
p.Status,
Uraian = p.RefStatusIzin.Uraian
});
if (result != null)
{
if (result.Status == null)
{
Console.WriteLine("Data not found");
}
else
{
_logger.LogInfo($"Returned data with NoIzin: {NoIzin}");
return Ok(result);
Console.WriteLine("Status: Valid");
}
}
}
catch (Exception ex)
{
_logger.LogError($"Something went wrong inside GetSampleById action: {ex.Message}");
return StatusCode(500, "Internal server error");
}
}
Not sure why
Kouhai
Kouhaiโ€ข2y ago
Aren't there two FindByNoIzin methods? One in the controller and one called on PP?
daisy
daisyโ€ข2y ago
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)
Kouhai
Kouhaiโ€ข2y ago
So PP is the controller and FindByNoIzin is a recursively calling itself?
daisy
daisyโ€ข2y ago
PP is the model and FindByNoIzin is in PPController.cs NoIzin itself is from table PP
Kouhai
Kouhaiโ€ข2y ago
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?
daisy
daisyโ€ข2y ago
this one in my PPRepository
daisy
daisyโ€ข2y ago
Yes in my controller return IActionResult
Kouhai
Kouhaiโ€ข2y ago
Is PP an IEnumerable?
daisy
daisyโ€ข2y ago
I think it's not, i'm not using the automapper
Kouhai
Kouhaiโ€ข2y ago
What classe/interfaces does it inherent/implement?
daisy
daisyโ€ข2y ago
yes it does
Kouhai
Kouhaiโ€ข2y ago
Do you mean it implements IEnumerable? It would still be better if you could show the class declaration ๐Ÿ˜…
daisy
daisyโ€ข2y ago
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?
Kouhai
Kouhaiโ€ข2y ago
The one that's returned by FindByNoIzin
daisy
daisyโ€ข2y ago
ah this one?
Kouhai
Kouhaiโ€ข2y ago
Yeah, PP used by that repository
daisy
daisyโ€ข2y ago
But i still have same error
daisy
daisyโ€ข2y ago
Also response to Braniac5, im using same code for GetAllPP like this and it's work๐Ÿ˜•
Kouhai
Kouhaiโ€ข2y ago
what does GetAllPP return? Does it return PP too?
daisy
daisyโ€ข2y ago
Yes GetAllPP return PP:)
Kouhai
Kouhaiโ€ข2y ago
Alright, again can you post the class deceleration for PP? ๐Ÿ˜…
daisy
daisyโ€ข2y ago
this is my PP it's pretty long can't get all in a frame
Kouhai
Kouhaiโ€ข2y ago
No problem, can you also post the declaration for BaseEntity?
daisy
daisyโ€ข2y ago
Kouhai
Kouhaiโ€ข2y ago
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?
daisy
daisyโ€ข2y ago
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)
Kouhai
Kouhaiโ€ข2y ago
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
daisy
daisyโ€ข2y ago
Actually i don't want to call select๐Ÿ˜… okay i will remove it ๐Ÿ™‚
daisy
daisyโ€ข2y ago
daisy
daisyโ€ข2y ago
This what i mean
Kouhai
Kouhaiโ€ข2y ago
Remove the call to select too. It's not needed, LINQ's Select is used to map a collection of values
daisy
daisyโ€ข2y ago
like this one?
daisy
daisyโ€ข2y ago
But how to get Status and Kdstatus only from FindByNoIzin? I mean i want to map collection
Kouhai
Kouhaiโ€ข2y ago
But FindByNoIzin is not returning a collection, it's returning a single object
daisy
daisyโ€ข2y ago
Ah i see, i was confused there
Kouhai
Kouhaiโ€ข2y ago
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 null
daisy
daisyโ€ข2y ago
Then what it should be? Btw thank you so much Kouhai for helping me patiently
daisy
daisyโ€ข2y ago
match a condition first? i have this one before
Kouhai
Kouhaiโ€ข2y ago
Do many PPs have the same NoIzin?
daisy
daisyโ€ข2y ago
1 PP have 1 NoIzin same as Id
Kouhai
Kouhaiโ€ข2y ago
Okay, then FindByNoIzin is behaving correctly It's returning a single object if it finds it
daisy
daisyโ€ข2y ago
Ah maybe to map value i should use automapper then? no use select
Kouhai
Kouhaiโ€ข2y ago
I assume you want to map PP because you don't want all it's properties to be serialized?
daisy
daisyโ€ข2y ago
Yes, i only need 2 properties only since PP has so many properties.
Kouhai
Kouhaiโ€ข2y ago
Then just do
return Ok(new { result.Status,
Uraian = result.RefStatusIzin.Uraian });
return Ok(new { result.Status,
Uraian = result.RefStatusIzin.Uraian });
daisy
daisyโ€ข2y ago
Ah thank you, i never thought of it๐Ÿ˜ญ
Kouhai
Kouhaiโ€ข2y ago
๐Ÿ˜… no problem
daisy
daisyโ€ข2y ago
also this another problem not all path return a value?
Kouhai
Kouhaiโ€ข2y ago
You aren't returning in this branch
if (result.Status == null)
{ Console.WriteLine("Data not found");
}
if (result.Status == null)
{ Console.WriteLine("Data not found");
}
You're also not returning if result == null
daisy
daisyโ€ข2y ago
So both result should be result == null? sorry i wrong read
Kouhai
Kouhaiโ€ข2y ago
When your method has a return type, all your code paths just needs to have a return statement or throw an exception
daisy
daisyโ€ข2y ago
Yes i realized i didn't return result == null๐Ÿ˜… Noted
Kouhai
Kouhaiโ€ข2y ago
blobthumbsup
daisy
daisyโ€ข2y ago
Kou the data is empty๐Ÿ˜‚
Kouhai
Kouhaiโ€ข2y ago
Can you put a breakpoint on return Ok(....) and see if it's hit?
daisy
daisyโ€ข2y ago
I did return ok (result), it got all data, it wont call the if method after it was return ok();
Kouhai
Kouhaiโ€ข2y ago
Oh, that's normal return ok() would just return with status code 200 and no data.
daisy
daisyโ€ข2y ago
yes right Kou
daisy
daisyโ€ข2y ago
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
daisy
daisyโ€ข2y ago
this RefStatusIzin
Kouhai
Kouhaiโ€ข2y ago
RefStatusIzin is not populated, are you using any ORM like EntityFramework, Dapper.. etc?
daisy
daisyโ€ข2y ago
I believe i'm using EntityFramework
Kouhai
Kouhaiโ€ข2y ago
Alright, what's the code for FindByCondition?
daisy
daisyโ€ข2y ago
this one
Kouhai
Kouhaiโ€ข2y ago
You need to call Include iirc by default EF won't load other tables.
daisy
daisyโ€ข2y ago
I called include too, Kou
daisy
daisyโ€ข2y ago
This is my RefStatusIzin
daisy
daisyโ€ข2y ago
and this is for PP
daisy
daisyโ€ข2y ago
Sorry sorry
Kouhai
Kouhaiโ€ข2y ago
The configurations looks fine to me. I'm not sure where Include is called though
daisy
daisyโ€ข2y ago
Sorry i think i didn't call include, i mean i don't use include, it's only configuration and models
Kouhai
Kouhaiโ€ข2y ago
What's the class deceleration for RefStatusIzin?
daisy
daisyโ€ข2y ago
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
daisy
daisyโ€ข2y ago
daisy
daisyโ€ข2y ago
This data from GetAllData, like this one
Kouhai
Kouhaiโ€ข2y ago
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?
daisy
daisyโ€ข2y ago
Yes you're right, that's what i want to find out how to make FindByCondition could do JOIN
daisy
daisyโ€ข2y ago
This is my GetAllPP
Kouhai
Kouhaiโ€ข2y ago
Can you also show the code for FindAll?
daisy
daisyโ€ข2y ago
daisy
daisyโ€ข2y ago
This Kou
Kouhai
Kouhaiโ€ข2y ago
You can just call
RepositoryContext.Set<PP>.Include(e => e.RefStatusIzin).Where(e => e.NoIzin.Equals(noIzin)).AsNoTracking().FirstOrDefault();
RepositoryContext.Set<PP>.Include(e => e.RefStatusIzin).Where(e => e.NoIzin.Equals(noIzin)).AsNoTracking().FirstOrDefault();
in FindByInzin, because after AsNoTracking in FindByCondition you shouldn't do any other queries.
daisy
daisyโ€ข2y ago
Kou it said not valid in the given context
daisy
daisyโ€ข2y ago
Okey noted, so we need to write include too
Kouhai
Kouhaiโ€ข2y ago
Sorry I forgot () after Set<PP> ๐Ÿ˜…
daisy
daisyโ€ข2y ago
OMG Kuo, thank you so much it works, ๐Ÿ˜ญ catlovecatlove
Kouhai
Kouhaiโ€ข2y ago
glad it's blobthumbsup
daisy
daisyโ€ข2y ago
catlovecatlove why my GetDataById and FindPPByNoIzin have same route so system confused who to call so it show error return?
Kouhai
Kouhaiโ€ข2y ago
Are these two methods in a controller?
daisy
daisyโ€ข2y ago
Yes in same controller
Kouhai
Kouhaiโ€ข2y ago
Can you show the attributes on both the controller and methods? catthinking
daisy
daisyโ€ข2y ago
this is GetPPById
daisy
daisyโ€ข2y ago
FindByNoIzin
Kouhai
Kouhaiโ€ข2y ago
And the class' attributes?
daisy
daisyโ€ข2y ago
this is the class
Kouhai
Kouhaiโ€ข2y ago
Oh, I meant the controller class' attributes
daisy
daisyโ€ข2y ago
this one?
Kouhai
Kouhaiโ€ข2y ago
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/{}
daisy
daisyโ€ข2y ago
Thank you so much Kou, i will take that in my note. It's working now catlove
Kouhai
Kouhaiโ€ข2y ago
no worries smileybolb
Accord
Accordโ€ข2y ago
โœ… This post has been marked as answered!
Want results from more Discord servers?
Add your server
More Posts