JQuery 1.6+ attr and prop issue
I used the following code to make sure all radio buttons where unchecked and the current one was set correctly. This worked well before jQuery 1.6.
SetUniqueRadioButton = function(current, id) {
jQuery("input[type=radio]").each(function() {
$(this).attr("checked", false);
});
current.checked = true;
};
});
Since jQuery 1.6, this code did not work and after doing some digging around, I found that you need to use the new method .prop() to get the code functioning correctly again.
SetUniqueRadioButton = function(current, id) {
jQuery("input[type=radio]").each(function() {
$(this).prop("checked", false);
});
current.checked = true;
};
});
Before jQuery 1.6, the .attr() method sometimes took property values into account when retrieving some attributes, which could cause inconsistent behavior. As of jQuery 1.6, the .prop() method provides a way to explicitly retrieve property values, while .attr() only retrieves attributes.
References:
Finding StoreGeneratedPattern value in EdmProperty in T4 Template
If you need to find out if a primary key in your conceptual model which is using the Identity value from the property StoreGeneratedPattern, you can use the following code: (NOTE: This is used within a T4 template)
EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);
string annotationNamespace = "http://schemas.microsoft.com/ado/2009/02/edm/annotation";
foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name)) {
foreach (EdmProperty edmProperty in entity.Properties.Where(p => p.TypeUsage.EdmType is PrimitiveType && p.DeclaringType == entity)) {
MetadataProperty storeGeneratedPatternProperty = null;
edmProperty.MetadataProperties.TryGetValue(annotationNamespace + ":StoreGeneratedPattern", false, out storeGeneratedPatternProperty);
if (storeGeneratedPatternProperty != null && storeGeneratedPatternProperty.Value.ToString() == "Identity") {
//We found an Identity property
}
}
}
Back to Basics – Deleting Cookies using ASP.NET
You cannot instruct ASP.NET to physically remove cookies from the user’s computer so you have to tell the user’s browser to modify the cookie in order to trigger the browser to remove the cookie.
See code example:
string cookieName;
int limit = Request.Cookies.Count;
for (int i=0; i {
cookieName = Request.Cookies[i].Name;
aCookie = new HttpCookie(cookieName);
aCookie.Expires = DateTime.Now.AddDays(-1);
Response.Cookies.Add(aCookie);
}
References:
http://msdn.microsoft.com/en-us/library/ms178194.aspx#Y5776
handling multiple submit buttons in one form (asp.net webforms)
I came across an issue where I had the following setup with an asp.net website:
- I had a master page with one main form element
- I had a submit button in the master page which acts as a search button across the website
- I had a login page that also has a submit button
Naturally when the user presses the enter key, it naturally takes the first submit button on the webpage as being the default submit button. Therefore when a user enters their credentials on the login page, they will always be taken to the search page which is defined by the input button on the master page.
I used jQuery to help solve this issue, you need to do two important things:
1) You need to set a default selector on the input button you want to submit, i.e on the login button I put:
2) Use the following code (Note this code can easily be refactored to be more efficient):
if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
//if the search has text then submit that button first
if ($('.buttondefault').length > 0) {
$('.buttondefault').click();
return false;
}
$('#myMasterButtonClass input[type=submit]').click();
return false;
} else {
return true;
}
});
Off to Wellington
Its been sometime updating this blog due to well being busy (as usual) and being caught up in the 22nd Feb Earthquake in Christchurch. Just a bit of an update of what we are doing and where we are heading.
Firstly we are relocating to Wellington where Tim will spend his time during the day working for NV Interactive as a Senior Developer. He works for NV in the Christchurch office (the temp office at the moment) but looks forward to working in the Welly office.
Over the past several months (and a few sleepness nights) Codelab have helped Coolpak coldstores upgrade their corporate website onto the ASP.NET MVC3/razor and Entity Framework which is working really well.
We setup and ran Medicsafe Online Distributors which is a online shop for purchasing Firstaid Kits and supplies. We have recently handedthis over to Pro+Med which is a firstaid and safety training company. We have also upgraded Pro+Med’s website to MVC3/Razor using the EF framework.
We have helped Verb implement the Unicef Campaign Monitoring which was featured in the Timaru Herald several months back.
We have created a new windows phone app called Prepare Yourself that helps users prepare for natural disasters. This was inspired by the first earthquake in Christchurch in September 2010.
We can now annouce that we are merging the design business Madcow Solutions (ran by my wife) into our company so we will be offering a bunch of new services soon.
Once everything is settled we hope to keep this blog updated with technical posts of problems (and solutions) over a vast range of technologies.
Compiler Error Message: CS1973
I came across this error when I was trying to use the @Render method to render a view while passing through a object that will act as the model for that view.
Compiler Error Message: CS1973: ‘System.Web.Mvc.HtmlHelper’ has no applicable method named ‘Render’ but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax.
The issue was that the compiler could not choose the correct method because my model was “dynamic”.
The correct syntax is:
Disabling Request Validation with ASP.NET MVC
Sometimes when dealing with POSTS and form data, you need to disable Request Validation to allow HTML/scripting data into your methods. Traditionally with Web Forms you use <%@ Page ValidateRequest=”false” %> which turns off Request Validation, just be aware that this doesn’t work with ASP.NET MVC.
You apply the [ValidateInput] attribute to the controller action in order to make this happen.
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create([Bind(Exclude="Id")]Product productToCreate)
{
}
New Windows Phone 7 App coming soon
We have been developing a new Windows Phone 7 application called “Prepare Yourself” which will be in the marketplace very soon. The application helps you track items that you should have in your survival kit in case of a natural disaster. You can view the demo here
Determing SQL Server Table Size
This is a bit of code I found that determines the size used by each table in your database.
Make sure you run DBCC UPDATEUSAGE first to correct any incorrect stats (pages etc) on your tables.
DBCC UPDATEUSAGE (YOUR DATABASE NAME)
--Cursor to get the name of all user tables from the sysobjects listing
DECLARE tableCursor CURSOR
FOR
select [name]
from dbo.sysobjects
where OBJECTPROPERTY(id, N'IsUserTable') = 1
FOR READ ONLY
CREATE TABLE #TempTable
(
tableName varchar(100),
numberofRows varchar(100),
reservedSize varchar(50),
dataSize varchar(50),
indexSize varchar(50),
unusedSize varchar(50)
)
OPEN tableCursor
FETCH NEXT FROM tableCursor INTO @TableName
WHILE (@@Fetch_Status >= 0)
BEGIN
INSERT #TempTable
EXEC sp_spaceused @TableName
FETCH NEXT FROM tableCursor INTO @TableName
END
CLOSE tableCursor
DEALLOCATE tableCursor
--Select all records so we can use the reults, ordered by biggest
SELECT *
FROM #TempTable
ORDER BY CAST(LEFT(dataSize,LEN(dataSize)-3) AS NUMERIC(18,0)) DESC
DROP TABLE #TempTable
References:
You receive a “The incoming tabular data stream (TDS) remote procedure call (RPC) protocol stream is incorrect” exception when using NVarchar parameters with Sqlclient
The problem
I got the following random error:
You receive a “The incoming tabular data stream (TDS) remote procedure call (RPC) protocol stream is incorrect” exception when using NVarchar parameters with Sqlclient
The Solution
Its a known bug with the .Net SqlClient Data Provider. If you have a field in the database of type nvarchar(max) or greater than 4000 characters and if the user enters data into the field greater than 4000 characters via the .Net SqlClient Data Provider you will receive an exception.
To get around this, reduce the field size to less that 4000 characters or change your type to ntext or set the Sqlparamter.size property to -1 to allow the entire data to be saved.
References


