C
C#โ€ข3y ago
esther

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

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