Get the StudentId Of logged in student

Posted by Jopito under ASP.NET on 12/19/2013 | Points: 10 | Views : 1365 | Status : [Member] | Replies : 7
In my application,to add a new student i have to pass the StudentId of currently logged in Student who is in roles of Students.When i provide the StudentId as query string in my browser,it adds the new student but the problem is when i have to get that StudentId from trhe students Table,the Student Id is not returned back.I have used debugger and i can see the name of the logged user but the id is not returned

Have used this code here but it returns null results.

MembershipUser student = Membership.GetUser(Page.User.Identity.Name);
int StudentId = ManageStudents.GeStudentId((Guid)user.ProviderUserKey);


Here is the Method
public int GetStudent(Guid userId)
{



try
{
return db.Students.FirstOrDefault(x => x.UserId == userId).StudentId;

}
catch (Exception)
{

throw;
}
}

This returns studentId as 0..Someone who can help me solve this?

Mark as answer if satisfied


Responses

Posted by: Bandi on: 12/19/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Hi,
Here you are calling GeStudentId() method
int StudentId = ManageStudents.GeStudentId((Guid)user.ProviderUserKey); 


But you have shared the functionality of method GetStudent()


Try this ?
int StudentId = ManageStudents.GeStudent((Guid)user.ProviderUserKey);


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Jopito, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jopito on: 12/19/2013 [Member] Starter | Points: 25

Up
0
Down
Am getting the error Object reference not set to an instance of an object.
int StudentId = ManageStudents.GeStudentId((Guid)user.ProviderUserKey);


public int GetStudentId(Guid userId)
{



try
{
return db.Students.FirstOrDefault(x => x.UserId == userId).StudentId;

}
catch (Exception)
{

throw;
}
}


Mark as answer if satisfied

Jopito, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: vishalneeraj-24503 on: 12/19/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Why did you pass GUID ti type cast in your code

int StudentId = ManageStudents.GeStudentId((Guid)user.ProviderUserKey);
Instead of Guid,just pass string AS

int StudentId = ManageStudents.GeStudentId((string)user.ProviderUserKey);

Jopito, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 12/19/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Are you getting value into user.ProviderUserKey ?
Debug the code for the values of user.ProviderUserKey

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Jopito, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 12/19/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Refer this link
http://stackoverflow.com/questions/10359108/asp-net-mvc-3-error-object-reference-not-set-to-an-instance-of-an-object

Similar kind of error

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Jopito, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jopito on: 12/19/2013 [Member] Starter | Points: 25

Up
0
Down
Yeah i can get the value but its different from what i think it should be, by using the method below i can get the identity but the problem is when i av to pass the studentId of logged in user
MembershipUser user = Membership.GetUser(Page.User.Identity.Name);,This shows the identity of logged in user but cant pass the id


Mark as answer if satisfied

Jopito, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 12/19/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Can you share the GetUser() functionality?


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Jopito, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response