Posts

Donate

Failed to load resource: the server responded with a status of 404 (WebResource.axd)

I downloaded a GridView custom control with cool features on search and filtering that was developed on Visual Studio 2010 and ASP.NET 4.0. After playing around with the control, I decided to migrate the files to Visual Studio 2012 ASP.NET 4.5. Upon running the ASP.NET Project, the resource to be embedded on the GridView control in which case a JavaScript file was not recognized/found. And thus, returned a 404 status. After doing some research, I came up with the solution below. Steps to fix the issue: 1. Set Build Action of the Resource/JavaScript file to Embedded Resource . In my project, the file to be embedded is EnhancedGridView.js 2. Add WebResourceAttribute to the GridView custom control to embedded a JavaScript file as Resource in an assembly. Make sure that the namespace of the file is correct. 1 2 3 4 5 [assembly: WebResource("GridViewFilter.EnhancedGridView.js", "text/javascript")] public partial class EnhancedGridView : GridView { //.....

Remove Formatting Of Last Selected Text In A WPF RichTextBox Control

Image
Here's how to remove the formatting of last selected text in a WPF RichTextBox control from it's SelectionChanged event. C# Code 1 2 3 4 5 6 private void rtbEditor_SelectionChanged( object sender, RoutedEventArgs e) { //Clear previous selections. TextRange textRange = new TextRange(rtbEditor.Document.ContentStart, rtbEditor.Document.ContentEnd); textRange.ClearAllProperties(); } Output

Create XmlElement Using XmlSerializer In C#.NET

Image
Given the XML data below, the product properties are generated below the products node. What I want is to structure the product properties inside an element Product while serializing. <?xml version="1.0" encoding="utf-8"?> <Products xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd= "http://www.w3.org/2001/XMLSchema" > <Code>12345</Code> <Name>Samsung Galaxy</Name> <Model>Galaxy</Model> <Manufacturer>Samsung Ltd</Manufacturer> <OperatingSystem>Jelly Bean</OperatingSystem> <Distributor>Junrex</Distributor> <Version>7.0</Version> </Products> To achieve that, I have modified the model classes by separating the properties into another class and in the Products class I created a property of type List with XML Attribute Product . public class Products { [XmlElement("Product")] publ

Parse Html Table Using HTML Agility Pack In C#

Below is a simple code to parse a table using HTML Agility Pack. Make sure to add the Html Agility Pack package from Nuget and reference that library in the namespace of your program. 1 using HtmlAgilityPack; Parse Table 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 HtmlWeb web = new HtmlWeb(); HtmlDocument doc = web.Load( "http://your_sample_url" ); // Get all tables in the document HtmlNodeCollection tables = doc.DocumentNode.SelectNodes( "//table" ); // Iterate all rows in the first table HtmlNodeCollection rows = tables[0].SelectNodes( "tr" ); for ( int i = 0; i <= rows.Count - 1; i++) { // Iterate all columns in this row HtmlNodeCollection cols = rows[i].SelectNodes( "td" ); if (cols != null ) { for ( int j = 0; j <= cols.Count - 1; j++) { // Get the value of the column and print it string value = cols[j].InnerText; Console.Write

Removing Hyperlink Or LinkButton In A GridViewColumn Based From A Condition Or Value

Image
There was a question raised from the forum on how to remove a hyperlink control in a GridViewColumn if a given condition is met. In the example below, the hyperlink(LinkButton) is disabled if Units In Stock is zero (0). A solution is to disable the LinkButton in the RowDataBoundEvent . 1 2 3 4 5 6 7 8 9 10 11 protected void gvUnitSummary_RowDataBound( object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { if (Convert.ToInt32(e.Row.Cells[3].Text) == 0) { LinkButton lnkUnitID = (LinkButton)e.Row.FindControl( "lnkProducts" ); lnkUnitID.Enabled = false ; } } } Another solution is to change the column from BoundField to TemplateField. The TemplateField contains a LinkButton and a Label control. Set the Visibility property of the controls accordingly such as LinkButton will be shown if Units In Stock is greater than 0. Otherwise, show the Label control instead. 1 2

Copy Single Element Of An array To Another Array Without Using Loop In C# And VB.NET

Simple solution is to use Array.Copy() method. C# Code 1 2 3 int [] sourceArray = {1,2,3,4,5,6,7,8,9,10}; int [] destinationArray = new int [1]; Array.Copy(sourceArray,3,destinationArray,0,1); VB.NET Code 1 2 3 Dim sourceArray As Integer () = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} Dim destinationArray As Integer () = New Integer (1) {} Array.Copy(sourceArray, 3, destinationArray, 0, 1)

WPF DataGrid Column Resize Event

Windows Forms DataGridView has a built-in event when the DataGridViewCell is resized but missing in WPF. To add an event for the DataGridCell when it is resized, a solution is to add an EventSetter to the DataGridCell style and then set the values of Handler and Event respectively. 1 2 3 4 5 <DataGrid.CellStyle> <Style TargetType= "DataGridCell" > <EventSetter Event= "SizeChanged" Handler= "Cell_SizedChanged" /> </Style> </DataGrid.CellStyle> In your code behind, add definition to the Handler based from the defined style. 1 2 3 4 5 private void Cell_SizedChanged( object sender, SizeChangedEventArgs e) { DataGridCell cell = (DataGridCell)sender; //TODO: Add your code here.... }

WebBrowser Click Html Element With data-id Attribute

Here's how you click an <li> element with data-id attribute. Given that the element's behavior includes navigating to another page. VB.NET 1 2 3 4 5 6 7 8 9 10 11 12 If webBrowser1.Document IsNot Nothing Then Dim elems As HtmlElementCollection = webBrowser1.Document.GetElementsByTagName( "li" ) For Each elem As HtmlElement In elems Dim nameStr As String = elem.GetAttribute( "data-id" ) If ((nameStr IsNot Nothing ) And (nameStr.Length <> 0)) Then If nameStr = "5" Then elem.InvokeMember( "click" ) End If End If Next End If C# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 if (webBrowser1.Document != null ) { HtmlElementCollection elems = webBrowser1.Document.GetElementsByTagName( "li" ); foreach (HtmlElement elem in elems) { String nameStr = elem.GetAttribute( "data-id" );

Get Latest Order For Each Customer Using C# And LINQ To SQL

Given the task that my client requires me to retrieve all customers and their recent orders made for their report, the solution for this using LINQ is to join two tables namely Customers and Orders . Then the script retrieves the latest order code in the subquery with the condition that it should match with the customer id in the outer query. The subquery also applies arranging the orders made according to recent date and then select the latest using FirstOrDefault() which is the equivalent to Top 1. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 var results = ( from order in _context.Orders join cus in _context.Customers on order.CustomerID equals cus.CustomerID where (order.OrderID == ( from subOrder in _context.Orders where subOrder.CustomerID == order.CustomerID orderby subOrder.OrderDate descending select subOrder.OrderID).FirstOrDefault())

Apply LINQ Grouping And Sum Aggregate To A Datatable Object (C#)

Image
Good day! Here's the C# version of this post. Apply LINQ Grouping and Sum aggregate to a Datatable Object (VB.NET) Code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 DataTable dt = new DataTable( "tblEntTable" ); dt.Columns.Add( "ID" , typeof ( string )); dt.Columns.Add( "amount" , typeof ( decimal )); dt.Rows.Add( new object [] { "1" , 100.51 }); dt.Rows.Add( new object [] { "1" , 200.52 }); dt.Rows.Add( new object [] { "2" , 500.24 }); dt.Rows.Add( new object [] { "2" , 400.31 }); dt.Rows.Add( new object [] { "3" , 600.88 }); dt.Rows.Add( new object [] { "3" , 700.11 }); var results = ( from orders in dt.AsEnumerable() group orders by orders.Field< string >( "ID" ) into g select new { ID = g.Key,

Donate