Error open DataReader Solution: For anyone experiencing an error with their DataReader solution already being open the problem is that "userManager.Users" is not asynchronous. One solution to this is to extend it with the following: "userManager.Users.ToListAsync()" be sure to #include Microsoft.EntityFrameworkCore; in order to access this extension method. CODE FROM VIDEO WITH ERROR //foreach (var user in userManager.Users) //{ // if (await userManager.IsInRoleAsync(user, role.Name)) // { // model.Users.Add(user.UserName); // } //} NEW CODE WITH EXTENSION //foreach (var user in await userManager.Users.ToListAsync()) //{ // if (await userManager.IsInRoleAsync(user, role.Name)) // { // model.Users.Add(user.UserName); // } //} A proposed solution for some is to add MultipleActiveResultSets=True; to their connection string however, if you're using MySQL Server instead of SQLServer this will not be available.
Dugz: Good job! Saved me a lot of time. As I am using .NET CORE 3.1, I now tend to read all the comments to identify potential problems BEFORE implementing the code he used for .NET CORE 2.2
you are brilliant Venkat you have explained two concepts in one shot. Complete CRUD along with Identity. wonderful. wonderful. wonderful tutorial. thanks from the bottom of my hear
8:37 Why not just use var users = await _userManager.GetUsersInRoleAsync(role.Name); instead of retrieving all users and check them one by one if they are in the role? Or was GetUsersInRoleAsync not available when the video was created?
foreach(var user in userManager.User) throws exception like DataReader connection already opened so kindly use foreach(var user in userManager.User.ToList()). Thank for great tutorial Venkat
Thank you so much for this, I was having the exception, the DataReader connection already opened and I didn't know how to solve it your comment helped me. Keep posting these comments it really helps new developers. Shukriya
14:11 you don't have to instantiate the list in the constructor in latest version of c#. You can easily do that in the property itself. public List Users { get; set; } = new List();
Admin role can't delete. Only admin can add or remove other roles. How admin set specific prevelig to other roles in asp.net core ? In this project admin can perform crud operation while other users can't. How to implement this?
When you add the disabled attribute to an HTML input element, it will prevent the input from being edited by the user. However, it will also exclude the input from being submitted with the form data when the form is posted. If you want to display the RoleId without allowing the user to edit it, you can use a readonly input instead of a disabled input.
Done follow this tutorial in one week, best approach to learn this is you should read the tutorial on the kudvenkat tutorial website before u watch the video. so then u know the content of video, read is not enough must watch the video too. good luck.
Really appreciate the way you have explained the .net core. Is this project available on GIT HUB? If yes please paste the project URL if you can. Thanks again.
Hello Karan - Very sorry, unfortunately at the moment, I do not have the code on Github. I will get it out there soon. In the mean time, please check out the following link where you can find the source code in text format Part by Part in logical sequence. Instead of typing everything by hand, you can copy and paste the code. Hope this saves at least sometime for you. www.pragimtech.com/courses/asp-net-core-mvc-tutorial-for-beginners/
Thanks for the excellent tutorials. Please make a video on action based permission like add, edit, delete and view. Assign permission to user. Use permission instead of role or use both.
Thanks for the great videos. Does anyone know why we show the role id to the user instead of just having it as a hidden input field to pass to the server?
The best asp.net core tutorial out there! You're also the best educator imo. I guess next step is combing all these cool steps into complete asp.net project do it please :D
for some reason the tag helpers are not working for editrole! whenever i click on the edit button it doesnt throw an exception and the id is also not attached to the Url! can anyone help me with this?
Hello, very good course. I have a problem." NotSupportedException: Store does not implement IUserRoleStore" the error ocurs in " if (await userManager.IsInRoleAsync(user, role.Name))". use NetCore 5. I would know how to solve this problem. Saludos desde Argentina.
Can you share why we are not consistent with the original convention to use Interface repository, the naming convention of Index , Class under "Model" directory of the Table we are trying to create with [Required] [Display(Name = "City")] [MaxLength(50, ErrorMessage = "Field cannot be more than 50 Characters")] for example ?
Even though text box ID is disabled for not allowing user manipulation, while updating the model ID is not taken from text box. Its actually taking from URL parameter. You can prove this by giving a different name RoleId instead of Id for EditRoleViewModel. The value will be NULL, because the control is disabled. Instead of disabled, use readonly to resolve this issue.
Why are you getting in 4:30 the browser error while iam getting "Sorry, the resource you requested couldn't be found" the error we did handle in previous videos?
One thing I noticed, because I'm not using bootstrap. Every time when I try to Edit the role name, my roleId is null, I can't understand how that in your video is working and in my project isn't....
So I did find out why. This happens because of the "disabled" attribute from html. Try to use type=hidden, when your not using bootstrap. In bootstrap it's using a lot of javascript, so be carefull. Quote from w3schools: The disabled attribute can be set to keep a user from using the element until some other condition has been met (like selecting a checkbox, etc.). Then, a JavaScript could remove the disabled value, and make the element usable. Good luck and have fun. I hope it helps.
When i try to implement this lecture i get this error ""there is already an open datareader associated with this command which must be closed first" On the line where we assign users in User field of editroleviewmodel Why is that
Unable to Load Users for Edit. Getting Error - There is already an open DataReader associated with this Command which must be closed first. at line "if (await userMgr.IsInRoleAsync(user, role.Name))" - Help is welcomed
thanks for this, I couldn't figure out why the model parameter for EditRole Post method doesn't receive the Id value... the readonly seems to be the correct keyword that works. Don't know why.. maybe different .net core version? I'm doing this on 3.0
You can find the code in text format on our blog at the following link. For every video we have the source code. Hope this helps you save some time. All the best with everything. csharp-video-tutorials.blogspot.com/2019/01/aspnet-core-tutorial-for-beginners.html
Hello i Have error with instrucution IsInRoleAsync foreach (var user in users) { if (await userManager.IsInRoleAsync(user, role.Name)) { model.Users.Add(user.UserName); }; } InvalidOperationException: There is already an open DataReader associated with this Connection which must be closed first. Does anyone have an idea?
I have found out a solution for this. foreach (var user in _userManager.Users.ToList()) { var isRolePresent = _userManager.IsInRoleAsync(user, role.Name).Result; if (isRolePresent) { model.UserNames.Add(user.UserName); } } here when you using IQuerable it keeps the connection open but IsInRoleAsync is trying to close the connection once it checks the users. therefore this issue comes up. If you follow the way I have done then you will not come up with issue.
Hello David - The source code is not on GitHub, but you can download it from the following page. Hope this helps. www.pragimtech.com/courses/asp-net-core-mvc-tutorial-for-beginners/
Hi Guys here is issue instead of disabled use readonly because if you use disable it didn't find the control and the id value could not post back to controller
Hello Ram, I am facing issue with non-posting of Role Id from View to Controller, it is null. I would like to know what you wish to highlight above by saying use readonly instead of disabled. Can you elaborate which piece of code I need to update to make it working? I face this same issue in begnining example of this .Net core series example as well. Thanks in advance
one place I found so many unnecessary request are made to the database to fetch the user. var model = new EditRoleViewModel { Id = role.Id, RoleName = role.Name }; // Retrieve all the Users foreach (var user in userManager.Users) { // If the user is in this role, add the username to // Users property of EditRoleViewModel. This model // object is then passed to the view for display if (await userManager.IsInRoleAsync(user, role.Name)) { model.Users.Add(user.UserName); } } this foreach loop can be written making one request to the data base var Model = new EditRoleViewModel { Id = identityRole.Id, RoleName = identityRole.Name, };
Error open DataReader Solution:
For anyone experiencing an error with their DataReader solution already being open the problem is that "userManager.Users" is not asynchronous. One solution to this is to extend it with the following: "userManager.Users.ToListAsync()" be sure to #include Microsoft.EntityFrameworkCore; in order to access this extension method.
CODE FROM VIDEO WITH ERROR
//foreach (var user in userManager.Users)
//{
// if (await userManager.IsInRoleAsync(user, role.Name))
// {
// model.Users.Add(user.UserName);
// }
//}
NEW CODE WITH EXTENSION
//foreach (var user in await userManager.Users.ToListAsync())
//{
// if (await userManager.IsInRoleAsync(user, role.Name))
// {
// model.Users.Add(user.UserName);
// }
//}
A proposed solution for some is to add MultipleActiveResultSets=True; to their connection string however, if you're using MySQL Server instead of SQLServer this will not be available.
nice solution, thank you so much
Thank you.
thx!!
Thanks.
thanks, i was stuck, but now i'm ok))
You have reached beyond the pinnacle of tutorial arts. I, my dear friend, remain in awe.
Dugz: Good job!
Saved me a lot of time.
As I am using .NET CORE 3.1, I now tend to read all the comments to identify potential problems BEFORE implementing the code he used for .NET CORE 2.2
you are brilliant Venkat you have explained two concepts in one shot. Complete CRUD along with Identity. wonderful. wonderful. wonderful tutorial. thanks from the bottom of my hear
8:37 Why not just use
var users = await _userManager.GetUsersInRoleAsync(role.Name);
instead of retrieving all users and check them one by one if they are in the role?
Or was GetUsersInRoleAsync not available when the video was created?
Good i think your method is best practice
Thank you, i was looking for this solution the whole time!
Excellent!
This way prevents an error on this line -
foreach (var user in userManager.Users)
@@hshlom userManager.Users.ToList()
foreach(var user in userManager.User) throws exception like DataReader connection already opened so kindly use foreach(var user in userManager.User.ToList()). Thank for great tutorial Venkat
Thank you so much for this, I was having the exception, the DataReader connection already opened and I didn't know how to solve it your comment helped me. Keep posting these comments it really helps new developers. Shukriya
14:11
you don't have to instantiate the list in the constructor in latest version of c#. You can easily do that in the property itself.
public List Users { get; set; } = new List();
or even better, simply public List Users { get; set; } = new();
@@eawig getting all users on a same role is making alot of issues for me. dont know y
It's been a week, we haven't seen any new video uploaded. WE MISSED YOU VENKAT.
Thank u sir, I'm following this series and learning asp.net core everyday.
thank you, it's not just a tutorial. it's complete learning of .net core. and is it possible to cover signalr?
Please make tutorials on Docker and microservices in Asp.net Core.
Admin role can't delete. Only admin can add or remove other roles. How admin set specific prevelig to other roles in asp.net core ? In this project admin can perform crud operation while other users can't. How to implement this?
there is an arror at
When you add the disabled attribute to an HTML input element, it will prevent the input from being edited by the user. However, it will also exclude the input from being submitted with the form data when the form is posted. If you want to display the RoleId without allowing the user to edit it, you can use a readonly input instead of a disabled input.
Done follow this tutorial in one week, best approach to learn this is you should read the tutorial on the kudvenkat tutorial website before u watch the video. so then u know the content of video, read is not enough must watch the video too. good luck.
Thankyou for this awsome tutorial very helpful as always !
when you will finish asp.net core tutorials?
Really appreciate the way you have explained the .net core. Is this project available on GIT HUB? If yes please paste the project URL if you can.
Thanks again.
Hello Karan - Very sorry, unfortunately at the moment, I do not have the code on Github. I will get it out there soon. In the mean time, please check out the following link where you can find the source code in text format Part by Part in logical sequence. Instead of typing everything by hand, you can copy and paste the code. Hope this saves at least sometime for you.
www.pragimtech.com/courses/asp-net-core-mvc-tutorial-for-beginners/
Stupendous tutorial, you make it look so easy with a subject I had difficulties with. Many Thanks and keep them coming.
Such a great video. I have been being a great fan of you. Sir
many thanks keep them coming!
Thanks for the excellent tutorials.
Please make a video on action based permission like add, edit, delete and view. Assign permission to user. Use permission instead of role or use both.
Thanks for the great videos. Does anyone know why we show the role id to the user instead of just having it as a hidden input field to pass to the server?
Great video really appreciate the help! Currently implementing your tutorials in my major project. Thank you :)
The best asp.net core tutorial out there! You're also the best educator imo. I guess next step is combing all these cool steps into complete asp.net project do it please :D
Great tutorials as usual! Where can I find the source code for this course to download?
for some reason the tag helpers are not working for editrole! whenever i click on the edit button it doesnt throw an exception and the id is also not attached to the Url! can anyone help me with this?
Hello, very good course. I have a problem." NotSupportedException: Store does not implement IUserRoleStore" the error ocurs in " if (await userManager.IsInRoleAsync(user, role.Name))". use NetCore 5. I would know how to solve this problem. Saludos desde Argentina.
Can you share why we are not consistent with the original convention to use Interface repository, the naming convention of Index , Class under "Model" directory of the Table we are trying to create with [Required]
[Display(Name = "City")]
[MaxLength(50, ErrorMessage = "Field cannot be more than 50 Characters")] for example ?
Even though text box ID is disabled for not allowing user manipulation, while updating the model ID is not taken from text box. Its actually taking from URL parameter. You can prove this by giving a different name RoleId instead of Id for EditRoleViewModel. The value will be NULL, because the control is disabled. Instead of disabled, use readonly to resolve this issue.
exactly
god of teaching
Why are you getting in 4:30 the browser error while iam getting "Sorry, the resource you requested couldn't be found" the error we did handle in previous videos?
What will be next sir, what are you going to cover in next series ?
Great explanation !!!
thanks, very good video u made!
you're the best
Hello venkut I applied autocomple jquery code and I was using WYSIWYG editor and now copy paste is not working
One thing I noticed, because I'm not using bootstrap. Every time when I try to Edit the role name, my roleId is null, I can't understand how that in your video is working and in my project isn't....
So I did find out why. This happens because of the "disabled" attribute from html. Try to use type=hidden, when your not using bootstrap. In bootstrap it's using a lot of javascript, so be carefull.
Quote from w3schools: The disabled attribute can be set to keep a user from using the element until some other condition has been met (like selecting a checkbox, etc.). Then, a JavaScript could remove the disabled value, and make the element usable.
Good luck and have fun. I hope it helps.
Use the attribute readonly instead of disabled.
When i try to implement this lecture i get this error
""there is already an open datareader associated with this command which must be closed first"
On the line where we assign users in User field of editroleviewmodel
Why is that
Modify the connection string to support multiple resultsets. Add MultipleActiveResultSets=true to connection string.
hi sir, please make tutorials on CRUD using jquery dataTable asp.net core , please sir, thank you
Unable to Load Users for Edit. Getting Error - There is already an open DataReader associated with this Command which must be closed first. at line "if (await userMgr.IsInRoleAsync(user, role.Name))" - Help is welcomed
Look at
@
Murat Kazanova's comment. Fixes the issue.
Thank so much
Because the id field is disabled it passes a null value when clicking on update.
thanks for this, I couldn't figure out why the model parameter for EditRole Post method doesn't receive the Id value... the readonly seems to be the correct keyword that works. Don't know why.. maybe different .net core version? I'm doing this on 3.0
@@jgsk78 I was using 3.1 and also used a readonly field
I am getting a null expectation error while using addtoroleasyn or any roleasyn method. Can any body help
I'm getting error
CS0246 C# The type or namespace name 'ApplicationUser' could not be found
while adding it to constructor injection
Hey, something similar happened to me. Did you solve it?
@@dacevedo19 yes for me it was bcoz I was injecting IdentityUser class ...which we extended through ApplicationUser class refer to Part : 77
@@NimbuYT Yeah i fixed that now! Thank you brother! Great memory btw haha
sir please make videos on jquery ajax call for this project
Please add full, top-to-bottom view of the complete class, controller action, view, model
You can find the code in text format on our blog at the following link. For every video we have the source code. Hope this helps you save some time. All the best with everything.
csharp-video-tutorials.blogspot.com/2019/01/aspnet-core-tutorial-for-beginners.html
What is the music at the end of each video?
I want it
I wonder How it works for everybody else without having actionmethod for Update Button.
Hello i Have error with instrucution IsInRoleAsync
foreach (var user in users)
{
if (await userManager.IsInRoleAsync(user, role.Name)) {
model.Users.Add(user.UserName);
};
}
InvalidOperationException: There is already an open DataReader associated with this Connection which must be closed first.
Does anyone have an idea?
i am also facing same issue
I have found out a solution for this.
foreach (var user in _userManager.Users.ToList())
{
var isRolePresent = _userManager.IsInRoleAsync(user, role.Name).Result;
if (isRolePresent)
{
model.UserNames.Add(user.UserName);
}
}
here when you using IQuerable it keeps the connection open but IsInRoleAsync is trying to close the connection once it checks the users. therefore this issue comes up. If you follow the way I have done then you will not come up with issue.
You need to .ToList( ) in the foreach loop
foreach (var user in _userManager.Users.ToList())
@@ghaziali3504 Thanks a lot
@@allalmohammed-amen6193 you're most welcome
Thanks for your support
How can we use ajax form in .net core?
your blog is not working for this tutorial. please fix it
Do you have this project on github?
Hello David - The source code is not on GitHub, but you can download it from the following page. Hope this helps.
www.pragimtech.com/courses/asp-net-core-mvc-tutorial-for-beginners/
why use
public EditRoleViewModel()
{
Users = new List();
}
Hi Guys here is issue instead of disabled use readonly because if you use disable it didn't find the control and the id value could not post back to controller
Hello Ram, I am facing issue with non-posting of Role Id from View to Controller, it is null. I would like to know what you wish to highlight above by saying use readonly instead of disabled. Can you elaborate which piece of code I need to update to make it working?
I face this same issue in begnining example of this .Net core series example as well.
Thanks in advance
The views always shoot down so low towards the end towards these types of series.
one place I found so many unnecessary request are made to the database to fetch the user.
var model = new EditRoleViewModel
{
Id = role.Id,
RoleName = role.Name
};
// Retrieve all the Users
foreach (var user in userManager.Users)
{
// If the user is in this role, add the username to
// Users property of EditRoleViewModel. This model
// object is then passed to the view for display
if (await userManager.IsInRoleAsync(user, role.Name))
{
model.Users.Add(user.UserName);
}
}
this foreach loop can be written making one request to the data base
var Model = new EditRoleViewModel
{
Id = identityRole.Id,
RoleName = identityRole.Name,
};
IList users = await userManager.GetUsersInRoleAsync(identityRole.Name);
foreach(var user in users)
{
Model.Users.Add(user.UserName);
}
Thank you. I needed that.
disabled make id not posted so I think we should use readonly
I have the same problem, thanks Mahmoud!