Friday, December 30, 2011

Select record in gridview

After heavy usage of different ways of selecting record in a gridview i have realized to post them in the blog

1) Use of DataKeyName and CommandField

Insert a command field in grid, for simplicity I have shown a select button

<asp:CommandField ShowSelectButton="True">
<ItemStyle Font-Size="Small" Width="5%" />
asp:CommandField>

and provide datakeyname in definition of grid as

<asp:GridView ID="grdCustomers" runat="server" Width="90%" DataKeyNames="pk_Id"
AutoGenerateColumns="False" AllowSorting="True">

It will provide access to following values on SelectedIndexChanged of grid or other events

grdCustomers.SelectedDataKey.Value
grdCustomers.SelectedRow
Easy First One!

2) Using a TemplateField

Insert a template field in appropriate position among gridview columns and add a Link Button like below

<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Select" CommandArgument=>asp:LinkButton>

Notice the attributes in it, 'CommandName' which is any unique name and 'CommandArgument' where PK_ID is a column name in datasource, you all know about eval dont you?

Now in code behind add a RowCommand event for gridview where you may use the following code

If e.CommandName = "Select" Then
Dim pk_id As String = e.CommandArgument
End If

To select gridview row in this case use this line:
Dim Dim row As GridViewRow = CType(CType(e.CommandSource, LinkButton).NamingContainer, GridViewRow)

Use it and enjoy your experience with ASP.NET gridview!

Friday, September 16, 2011

Save image from URL

Soon as I needed to save image from different URLs again, I recalled how many times i have previously done this, So this time pasting it on the blog so it will help not only me but some viewers too


'MyImage is System.Drawing.Image type variable
  MyImage = GetImage(ImageURL)
  If Not IsNothing(MyImage) Then
       ImagePath = RetreivedImagesPath + ValidFileName(PartNo) + ".jpg"
       MyImage.Save(ImagePath)
  Else
       ImagePath = "File Not Found"
  End If


Definition of GetImage() Function in VB


'Function to return an image taking URL as input
 Function GetImage(ByVal URL As String) As System.Drawing.Image
        Dim Request As HttpWebRequest
        Dim Response As HttpWebResponse
        Try
            Request = WebRequest.Create(URL)
            Response = CType(Request.GetResponse, System.Net.WebResponse)
            If Request.HaveResponse Then
                If Response.StatusCode = Net.HttpStatusCode.OK Then
                    GetImage = System.Drawing.Image.FromStream(Response.GetResponseStream)
                End If
            End If
            Thread.Sleep(5000)
        Catch ex As Exception
            GetImage = Nothing
        End Try
    End Function

Function for Valid File Name



Below is a VB function to return a valid file name,


  Protected Function ValidFileName(ByVal FileName As String) As String
        Dim rgPattern as String = "[\\\/:\*\?""'<>|]"
        Dim objRegEx As New Regex(rgPattern)
        Return objRegEx.Replace(FileName, "-")
  End Function

Tuesday, August 30, 2011

Unexpected error 0x8ffe2740 occurred

When using IIS 5.1 I got following error starting a website
"Unexpected error 0x8ffe2740 occurred"

This alert may occur due to a port conflict on the system. Port 80 is default port for HTTP communication. And there may be another application other than IIS using port 80 on same IP address.

Resolution is to either change default IP address on the IIS or to exit the other application using port 80. Applications like Skype may be a culprit as it uses port 80 and 443 as alternatives by default.

Sharing it as it might help someone else in future.
Regards,

Wednesday, August 24, 2011

Unable to attach to application 'WebDev.WebServer.EXE'

Some times in VS 2008 an error is observed before debugging an application:

Unable to attach to application 'WebDev.WebServer.EXE' (PID: 4548) using 'AYAZ-PC'. A debugger is already attached.

Do you want to continue anyway?

A quick resolution is to press CTRL+ALT+DEL to open task manager, In processes tab end WebDev.WebServer.EXE

BENEFITS OF DOTNET Framework 4.0

Hi,


Following is a quick list for new features in dotnet framework 4.0


BENEFITS OF DOTNET FRAMEWORK 4.0

1. Parallel Computing
The new programming model in framework 4.0 enables developers to write efficient, fine-grained, and scalable parallel code in a natural idiom without having to work directly with threads or the thread pool. The new Parallel and Task classes, and other related types, support this new model.


2. SEO Improvements
Dotnet framework 4.0 has some new features for SEO optimization which are mentioned here:
a. Page.MetaKeywords and Page.MetaDescription properties
These properties are useful when using masterpages as section ends up being in masterpages. These properties are now set in aspx pages like page.Title and have their values automatically rendered by the control within master page.
Meta Keywords and description can also be set in @Page directive
b. New URL routing support for ASP.NET forms
With Dotnet framework 4.0 URL routing one can use routing to define URLs that are semantically meaningful to users and that can help with SEO. Links will no longer refer to physical file like
http://www.mysite.com/products.aspx?category=software Can be changed to http://www.mysite.com/products/software
c. Response.RedirectPermanent() as replacement of Response.Redirect()


3. Enhancements in ADO.NET
Entity Framework decreases the amount of coding and maintenance required for data-driven applications by enabling you to program against data models defined in terms of entities and relationships. Entity Framework has got some useful features involve in framework 4.0


4. JQuery Included with Webforms
On creation of new website script folder is created with following files
a. jQuery-1.4.1.js
b. jQuery-14.1.min.js
c. jQuery-1.4.1-vsdoc.js – IntelliSense documentation file


5. ScriptManager Explicit Scripts
By taking advantage of the new ScriptManager.AjaxFrameworkMode property, one can control exactly which components of the ASP.NET Ajax Library are loaded and load only the components of the ASP.NET Ajax Library that are needed.


6. New property Control.ViewStateMode
This property is used to disable view state for all controls on a page except those for which you explicitly enable view state.


7. Support for recently introduced browsers and devices


8. Enhancements to data controls
a. Persisting row collection in data controls
A new attribute ‘EnablePersistedSelection’ is added in data controls which is used for selecting rows depending on row datakey instead of row index for better behaviour during paging
b. RenderTable property to generate cleaner HTML
In previous versions of framework most data controls were rendered within table thus imposing styling and other restrictions now controls like FormView and Login has got RenderTable property which is used to hide this table around objects


9. Enhanced support for web standards
Earlier versions of ASP.NET controls sometimes render markup that does not conform to HTML, XHTML, or accessibility standards. ASP.NET 4 eliminates most of these exceptions.


10. New Controls
a. ASP.NET Chart Control
This control enables to create applications that have simple, intuitive charts for complex statistical or financial analysis
b. QueryExtender Control as replacement of where clause
This is a linq based control and can be added to EntityDataSource or LinqDataSource controls in order to filter the data returned by these controls. It has following filter options:
i. Search Expression
ii. RangeExpression
iii. PropertyExpression
iv. OrderByExpression
v. CustomExpression


11. Control Improvements
a. Menu Control
In ASP.NET 4, menu control now renders HTML using semantic markup that consists of an unordered list and list elements instead of rendering in table as in previous versions. In addition to rendering improvements, keyboard navigation of the menu has been improved using focus management. And arrow keys now can be used to navigate


12. JavaScript IntelliSense Enhancements
IntelliSense now recognizes objects that have been dynamically generated by methods such as registerNamespace() etc


13. Application Domain Resource Monitoring
In .NET Framework 4.0, one can get processor usage and memory usage estimates per application domain by using AppDomain class


14. Session State Compression
ASP.NET 4 introduces a new compression for OutProc and SQLServer session modes. When the compressionEnabled configuration option is set to true, ASP.NET compresses (and decompresses) serialized session state by using the .NET Framework System.IO.Compression.GZipStream class.


15. New Caching API
The new System.Runtime.Caching.dll assembly contains a new caching API in the System.Runtime.Caching namespace which is mainly used for non web applications


16. Setting client id
Creating the id attribute for elements that are rendered in controls is now handled with new ClientIDMode property. This property lets you specify more precisely that how this id should be generated rather than auto generation as happened in previous versions.


17. Changes in CAS
Code access security in earlier frameworks is declared difficult to use effectively, and the new CAS security model in the .NET Framework 4.0 with Level 2 transparency completely replaces it.

Sunday, July 24, 2011

'specified service has been marked for deletion error'

If a service has been uninstantiated from services.msc than it first needs to close services.msc window before installing it again.

Most peoples have been observed restarting system to fix this and as it is a common situation for services development, hopefully this post will save a few reboots.