[Serializable] DB.NET

.NET development and more (or less)

/// <summary>
/// Daniel Balla's Weblog - Various posts 
/// on .NET Development, ASP.NET, Agile, 
/// TDD, Architecture, SQL and others.
/// </summary>
[Serializable]
class Blog<T> where T : ASP.NET, Ajax, WCF, WF, new()
{
}

Friday, March 23, 2007

Row level validation in a gridview

A couple days ago a friend asked me how can you make the ASP.NET validators validate the contents of a gridview at row level, so that each row works independently. For example, in the grid below use a requiredfieldvalidator on each row so that the Save button would validate the description textbox on the same row, but wouldn’t validate any of the other rows’ descriptions.



In this case, clicking the save button in the “Dairy products” row would cause the validation to suppress the postback, but clicking the save button of any other row wouldn’t.
The solution is simple: use the validation group bound to a unique row identifier. In our example (which displays the contents of the Categories table of the Northwind database) the binding would look like:

(asp:TextBox Width="300px" ID="txtDescription" runat="server" Text='(%# Bind("Description") %)' /)
(asp:RequiredFieldValidator ID="reqDescription" runat="server" ControlToValidate="txtDescription" ErrorMessage="*" ValidationGroup='(%# Bind("CategoryID") %)' /)
(asp:Button ID="butSave" runat="server" Text="Save" ValidationGroup='(%# Bind("CategoryID") %)' /)

The parenthesis ( ) should be replaced with less-greater than signs < >, they are just a workaround the poor blogger’s editor.

Download sample

Labels:

Wednesday, November 22, 2006

Script controls may not be registered before PreRender

This is neither a very common scenario nor a major finding, just a small reminder/warning to spare you a little debugging for one of those things that are fairly obvious yet could take some time to see.
While helping a friend figure out why his ASP.NET no longer works after "atlasising" it I came across this issue:
The page was throwing the following exception:

Script controls may not be registered before PreRender.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: Script controls may not be registered before PreRender.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[InvalidOperationException: Script controls may not be registered before PreRender.]
Microsoft.Web.UI.ScriptControlManager.RegisterScriptControl(TScriptControl scriptControl) +144
Microsoft.Web.UI.UpdateProgress.OnPreRender(EventArgs e) +140
System.Web.UI.Control.PreRenderRecursiveInternal() +77
System.Web.UI.Control.PreRenderRecursiveInternal() +161
System.Web.UI.Control.PreRenderRecursiveInternal() +161
System.Web.UI.Control.PreRenderRecursiveInternal() +161
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1360



As the page worked before it had the atlas references and an update panel added (and out of laziness) instead of properly reading the source code I proffered to google the error and the only relevant result was http://forums.asp.net/thread/1471314.aspx where the suggestion was that an older version of the DLL might be cached somewhere. Here is where I went wrong and spent a lot of time clearing and rebuilding. On a second look, I found that the page's OnPreRender method was overridden and it didn't call the base OnPreRender method therefore loosing some handlers. Obviously this mistake didn't do much difference before atlas came into play but it did afterwards because the Atlas ScriptControlManager's RegisterScriptControl method doesn't register the control but after prerender (by checking a flag it sets on prerender)

public void RegisterScriptControl(TScriptControl scriptControl) where TScriptControl: Control, IScriptControl
{
if (scriptControl == null)
{
throw new ArgumentNullException("scriptControl");
}
if (!this._pagePreRenderRaised)
{
throw new InvalidOperationException(AtlasWeb.ScriptControlManager_RegisterScriptControlTooEarly);
}
this.ScriptControls.Add(scriptControl);
}


public void OnPagePreRender(object sender, EventArgs e)
{
this._pagePreRenderRaised = true;
}

Labels:

Tuesday, November 21, 2006

Unable to preview web service

Seldom, but still a possible scenario: when trying to preview the generated service description for an asmx web service (both .NET 1.1 and .NET 2.0) you might get an error that is not very well described:

Line 1334: OperationBinding FindHttpBinding(string verb) {
Line 1335: foreach (ServiceDescription description in serviceDescriptions) {
Line 1336: foreach (Binding binding in description.Bindings) {
Line 1337: HttpBinding httpBinding = (HttpBinding)binding.Extensions.Find(typeof(HttpBinding));



The real issue is that in your web application’s web.config file in the pages element you probably have the autoEventWireup attribute set to false
<pages … autoEventWireup="false"…

If you want to trace the problem, you can open the DefaultWsdlHelpGenerator.aspx file (located under %windir\Microsoft.NET\Framework\[version]\CONFIG\DefaultWsdlHelpGenerator.aspx) and you would notice that the error is that the serviceDescriptions member is used without being initialized (therefore the null object reference exception). Looking up in the page you notice that the serviceDescriptions member is initialized
(as:
serviceDescriptions = (ServiceDescriptionCollection) Context.Items["wsdlsWithPost"];
or
serviceDescriptions = (ServiceDescriptionCollection) Context.Items["wsdls"];)
in the Page_Load method. However, because your settings disable the autoeventwireup that prevents the Page_Load methods from being called (it is not explicitly wired to the page load event so it relies entirely on the autoeventwireup feature).

Surely this isn’t a big finding and it wouldn’t save you more than 5 minutes of debugging but it is worth mentioning because it is a “red wiring” in the framework.

Labels:

Tuesday, August 15, 2006

Stress testing web services with WAST through SOAP

Stress testing webservices: I just found this great article on how to use Ms WAST with SOAP. In this manner, this looks like a decent tool to stress test web services (if you don't have VSTF). If anyone reading this has a better idea please do leave a comment.

Labels:


Gixen - Free Auction Sniper