JunglePay - Mobile Payments and more | Need Help? Get support from txtNationSD.
Saturday, January 19, 2013
Thursday, January 17, 2013
ASP.NET Session.Abandon() doesn't work - not Logged out
Issue:
Calling Session.Abandon() does not clear the full session and invalidate the login. User is not logged out by this.
Details:
Session.Abandon() Clears the current session of the user. This does not specifically ensures to clear all Session. For that you need to specifically call the Session.Clear() method. But many users expect to logout the user after these methods are called, which doesn't work well. Also this server side method doesn't clear the cached session on the client browser.
Solution:
If you want to invalidate the user login, the best solution is to use the ASP.NET LoginStatus control.
<asp:LoginStatus id="LoginStatus1" runat="server" LogoutAction="RedirectToLoginPage" />
Make sure to set the LogoutAction property to "RedirectToLoginPage". This will invalidate the user's session and Log him out.
Calling Session.Abandon() does not clear the full session and invalidate the login. User is not logged out by this.
Details:
Session.Abandon() Clears the current session of the user. This does not specifically ensures to clear all Session. For that you need to specifically call the Session.Clear() method. But many users expect to logout the user after these methods are called, which doesn't work well. Also this server side method doesn't clear the cached session on the client browser.
Solution:
If you want to invalidate the user login, the best solution is to use the ASP.NET LoginStatus control.
<asp:LoginStatus id="LoginStatus1" runat="server" LogoutAction="RedirectToLoginPage" />
Make sure to set the LogoutAction property to "RedirectToLoginPage". This will invalidate the user's session and Log him out.
Tuesday, January 8, 2013
Unable to update the EntitySet - because it has a DefiningQuery and no element exists in the element to support the current operation.
Error:
Unable to update the EntitySet 'Classroom' because it has a DefiningQuery and no <InsertFunction> element exists in the <ModificationFunctionMapping> element to support the current operation.
This error is raised while using Entity Framework.
While trying to save and persist db changes it raise this error. This error happens only in particular save and not every other places. The save operation raising this error is most probably an insert or a delete operation; like or example in my case:
db.Classrooms.Add(classroom);
db.SaveChanges();
Cause:
Eventhought the error message is a bit cryptic, the cause of the issue is simpler. The reason for this error is because the Primary key for the table is not set.
Solution:
Set the Primary key for the table and update the Entity Model (edmx file). This should clear the "Unable to update the EntitySet - because it has a DefiningQuery and no element exists in the element to support the current operation." exception.
Unable to update the EntitySet 'Classroom' because it has a DefiningQuery and no <InsertFunction> element exists in the <ModificationFunctionMapping> element to support the current operation.
This error is raised while using Entity Framework.
While trying to save and persist db changes it raise this error. This error happens only in particular save and not every other places. The save operation raising this error is most probably an insert or a delete operation; like or example in my case:
db.Classrooms.Add(classroom);
db.SaveChanges();
Cause:
Eventhought the error message is a bit cryptic, the cause of the issue is simpler. The reason for this error is because the Primary key for the table is not set.
Solution:
Set the Primary key for the table and update the Entity Model (edmx file). This should clear the "Unable to update the EntitySet - because it has a DefiningQuery and no element exists in the element to support the current operation." exception.
LINQ : How to check if a record exists in the database
Issue: How to check if a record exists in the database using LINQ
// Solution 1: Select . Don't use SingleOrDefault because if there are more than one record returned it throws exception.
var res = (from cls in db.Classrooms
where cls.Name == "10A"
select cls).SingleOrDefault();
if(res != null)
{
//record exists
}
// Solution 2: Select Count if you don't want to load the entity. This is an efficient method but not the most efficient. The LINQ expression translates to Count(*) in SQL.
int res = (from cls in db.Classrooms
where cls.Name == "10A"
select cls).count();
if(res > 0)
{
//record exists
}
// Solution 3:
Classroom cls = db.Classrooms.FirstOrDefault(cls => cls.Name == "10A");
if(cls != null) {
// record exists
}
else {
// record does not exist
}
// Solution 4: This is the most effective solution
// If you only want to know if such a Record exists:
// This is the most efficient as it generates an IF EXISTS SQL query
return db.Classrooms.Any(cls => cls.Name == "10A"); // true if exists
// Similar expression is:
return db.Classrooms.Where(cls => cls.Name == "10A").Any();
// Solution 5:
// If you want a count of how many such Record exists:
return db.Classrooms.Count(cls => cls.Name == "10A");
// Solution 6:
// If you want an enumeration (IEnumerable<Record>) of all such Record:
return db.Classrooms.Where(cls => cls.Name == "10A");
// Solution 1: Select . Don't use SingleOrDefault because if there are more than one record returned it throws exception.
where cls.Name == "10A"
select cls).SingleOrDefault();
if(res != null)
{
//record exists
}
// Solution 2: Select Count if you don't want to load the entity. This is an efficient method but not the most efficient. The LINQ expression translates to Count(*) in SQL.
int res = (from cls in db.Classrooms
where cls.Name == "10A"
select cls).count();
if(res > 0)
{
//record exists
}
// Solution 3:
Classroom cls = db.Classrooms.FirstOrDefault(cls => cls.Name == "10A");
if(cls != null) {
// record exists
}
else {
// record does not exist
}
// Solution 4: This is the most effective solution
// If you only want to know if such a Record exists:
// This is the most efficient as it generates an IF EXISTS SQL query
return db.Classrooms.Any(cls => cls.Name == "10A"); // true if exists
// Similar expression is:
return db.Classrooms.Where(cls => cls.Name == "10A").Any();
// Solution 5:
// If you want a count of how many such Record exists:
return db.Classrooms.Count(cls => cls.Name == "10A");
// Solution 6:
// If you want an enumeration (IEnumerable<Record>) of all such Record:
return db.Classrooms.Where(cls => cls.Name == "10A");
Tuesday, January 1, 2013
Open URL in Browser Programatically -IE/FF/Chrome [Windows Forms]
Problem:
I want to open a URL in desktop web browser programatically.
I want to open it in different browsers of my choice.
Solution:
Here are some code blocks to open a url in the browsers of our choice.
Below code will open a specific url in your default browser configured:
System.Diagnostics.Process.Start("http://www.habeeb.in");
To open the url in Internet Explorer, use the code:
System.Diagnostics.Process.Start("iexplore.exe", "http://www.habeeb.in");
To open the url in firefox, use the code:
System.Diagnostics.Process.Start("firefox.exe", "http://www.habeeb.in");
To open the url in Chrome, use the code:
System.Diagnostics.Process.Start("chrome.exe", "http://www.habeeb.in");
These solutions are based on Windows Desktops Winforms Applications.
I want to open a URL in desktop web browser programatically.
I want to open it in different browsers of my choice.
Solution:
Here are some code blocks to open a url in the browsers of our choice.
Below code will open a specific url in your default browser configured:
System.Diagnostics.Process.Start("http://www.habeeb.in");
To open the url in Internet Explorer, use the code:
System.Diagnostics.Process.Start("iexplore.exe", "http://www.habeeb.in");
To open the url in firefox, use the code:
System.Diagnostics.Process.Start("firefox.exe", "http://www.habeeb.in");
To open the url in Chrome, use the code:
System.Diagnostics.Process.Start("chrome.exe", "http://www.habeeb.in");
These solutions are based on Windows Desktops Winforms Applications.
Wednesday, December 5, 2012
"Add Web Reference" missing in Visual Studio 2010/2012 - Solution
Issue:
Where is the Add Web Reference option in Visual Studio 2010 and 2012.
Did microsoft remove the option to add web service and retained option only to use WCF references (Service Reference) ?
Everytime Microsoft comes up with a new version of a product, some of the options are either renamed or rearranged.
The same happened with Visual Studio 2010.
The option to add web reference is still available but is misplaced.
Solution:
To access the option follow the steps below:
1) Right Click the Project -> Select "Add Service Reference". You get the "Add Service Reference" window.
2) On its left bottom there is an "Advanced..." button. It will take you to the "Service Reference Settings" window.
3) Bottom left of this window there is the old "Add Web Reference..." button dumped in a corner.
Click to get the "Add Web Reference window".
Screenshots:
Where is the Add Web Reference option in Visual Studio 2010 and 2012.
Did microsoft remove the option to add web service and retained option only to use WCF references (Service Reference) ?
Everytime Microsoft comes up with a new version of a product, some of the options are either renamed or rearranged.
The same happened with Visual Studio 2010.
The option to add web reference is still available but is misplaced.
Solution:
To access the option follow the steps below:
1) Right Click the Project -> Select "Add Service Reference". You get the "Add Service Reference" window.
2) On its left bottom there is an "Advanced..." button. It will take you to the "Service Reference Settings" window.
3) Bottom left of this window there is the old "Add Web Reference..." button dumped in a corner.
Click to get the "Add Web Reference window".
Screenshots:
Monday, December 3, 2012
How to declare local variable in ASP.NET MVC Razor?
Issue:
How to declare local variable in ASP.NET MVC Razor?
This is a very common scenario that newbies in ASP.NET MVC Razor view comes across.
It would be confusing when even with the "@" sign before the declaration it doesn't work.
Solution:
The solution as simple as placing the whole declation statement inside curly braces.
Even multiple declarations can be placed inside a curly braces block.
The declaration and usage would be as below.
@{int count = 1;}
@foreach (var step in level.steps)
{
<div>
<span class="title">@step.Name</span>
<span class="meaning">@step.Description</span>
</div>
}
How to declare local variable in ASP.NET MVC Razor?
This is a very common scenario that newbies in ASP.NET MVC Razor view comes across.
It would be confusing when even with the "@" sign before the declaration it doesn't work.
Solution:
The solution as simple as placing the whole declation statement inside curly braces.
Even multiple declarations can be placed inside a curly braces block.
The declaration and usage would be as below.
@{int count = 1;}
@foreach (var step in level.steps)
{
<div>
<span class="title">@step.Name</span>
<span class="meaning">@step.Description</span>
</div>
}
Subscribe to:
Comments (Atom)