Posts

Donate

How To Highlight GridView Row On Click In ASP.NET Web Forms

This is based from the article of Vincent Maverick Durano's blog on HIGHLIGHT GRIDVIEW ROW ON CLICK AND RETAIN SELECTED ROW ON POST BACK. I just added some statements on javascript to check if the selected row is less than total gridview row count and server side code for paging. JAVASCRIPT CODE: <script type= "text/javascript" > var prevRowIndex; function ChangeRowColor(row, rowIndex) { var parent = document.getElementById(row); var currentRowIndex = parseInt(rowIndex) + 1; //count number of gridview rows var rowscount = $( "#<%=grdCustomer.ClientID %> tr" ).length; if (prevRowIndex == currentRowIndex) { return ; } else if (prevRowIndex != null ) { parent.rows[prevRowIndex].style.backgroundColor = "#FFFFFF" ; } //check if current index is a number... if (IsNumeric(currentRowIndex)) { //check if row index

Get Absolute Server Path Of Resources In ASP.NET MVC

In a scenario where you want to load images or other resources, Url.Content() does not return the exact resource path like this (D:\Cart\CartProject\Images\Item4.jpg) . Hence, resulting into a null exception or empty resource. Instead of using Url.Content() or ResolveUrl(), i get the server path of the resource using HttpContext of the application. return new ImageResult(HttpContext.Server.MapPath( "~/Images/Item" + id + ".jpg" )); Note: Testing is done locally and not on a virtual directory... Source: Find Absolute Path the the App Data Folder from Controller Cheers!

Display Bitmap Image In ASP.NET MVC View From Database With Image Data Type

I encountered this weird problem rendering bitmap image from a database table that a friend of mine develops in his company. The task is to display the image on the mvc view. I thought it was a gif or jpeg or png format. But to my surprise as I rendered the binary data on webpage, the image was a bitmap type.. The code to show the bitmap image is as follows. That is to render the bitmap image into a jpeg format. C# Code: public ActionResult ShowCategories () { var model = from n in oContext.ApplianceCategories select n; return View (model); } public void ShowImage ( int id) { var image = ( from m in oContext.ApplianceCategories where m.CategoryID == id select m.Picture).FirstOrDefault(); TypeConverter tc = TypeDescriptor.GetConverter( typeof (Bitmap)); Bitmap bitmap1 = (Bitmap)tc.ConvertFrom(image); bitmap1.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); } I got the trick from stack o

ASP.NET MVC generates error = "OutputStream is not available when a custom TextWriter is used"

In a simple application I'm creating, I have a custom ActionResult class that returns an image data. In my view, I simply used: <%= Html.Action( "GetImage" , "ImageCustom" , new {id = item.CategoryID}) %> to call the custom action class ExecuteResult method. This method reads the image byte array and store's it in a stream object. However, the call generates an error stated in the title. Html.Action writes html which implements text writer class. The solution would be using Url.Action() embedded in the image src attribute: <img src= '<%= Url.Action("GetImage","ImageCustom", new {id = item.CategoryID}) %>' alt= "No Photo" width= "100" height= "100" /> Greg

Object doesn't support property or method call in consuming JSON object From ASP.NET MVC In Internet Explorer

Recently, I've followed a simple tutorial regarding consuming JSON object in ASP.NET MVC. The error stated from the title of this post appears in IE9. The original snippet is this: $(document).ready( $.getJSON( 'http://localhost:6222/home/customerjson' , function (item) { $( '#result' ) //show product name in .html( '<p>' + item.CurrentCustomer.CustomerName + '</p>' ); } ) ); The solution is to enclose $.getJSON() in a function() statement based from the modified code below. The popup script just went away. $(document).ready( function (){ $.getJSON( 'http://localhost:6222/home/customerjson' , function (item) { $( '#result' ) //show product name in .html( '<p>' + item.CurrentCustomer.CustomerName + '</p>' ); } )

Eliminate Duplicate Rows In A DataTable In C#

Assuming you have a datatable that has merged contents from different datatables and you want to eliminate the datatable with distinct rows, you can use the statement below. dt.DefaultView.ToTable( true , new string [] { "id" , "name" }); Cheers!

Column 'column_name' Already Belongs To Another DataTable in C#

In a scenario where I added columns using AddRange(), I encountered an error as specified by the title. Here's the C# code: table1.Columns.AddRange( new DataColumn[] { col1, col2, col3 }); table2.Columns.AddRange( new DataColumn[] { col1, col2, col3 }); Where col1, col2, col3 are DataColumn objects. What I did was to initialize columns in the addrange method. But there are other solutions in the forums. table1.Columns.AddRange( new DataColumn[] { new DataColumn( "ID" ), new DataColumn( "WEB ID" ), new DataColumn( "URL" ) }); table2.Columns.AddRange( new DataColumn[] { new DataColumn( "ID" ), new DataColumn( "WEB ID" ), new DataColumn( "URL" ) });

WPF Datagrid (Prevent DataGrid Last Row From Being Sorted On Column Header Clicked)

Image
Here's the WPF version of prevent total rows from being sorted. Images: 1. Form Load, no column header is clicked (unsorted records) 2. Name header is clicked (sorting by column). The names are sorted alphabetically. Below are the methods used: /// <summary> /// column header clicked(sorting) /// </summary> private void dgProducts_Sorting( object sender, DataGridSortingEventArgs e) { DataRowView rv = (DataRowView)dgProducts.Items[dgProducts.Items.Count - 1]; if (rv[0].ToString().Contains( "Total:" )) { dvCopy = dgProducts.Items.SourceCollection as DataView; rv.Delete(); } sorted_aborted = e.Handled; } /// <summary> /// layout is updated /// </summary> private void dgProducts_LayoutUpdated( object sender, EventArgs e) { if (!sorted_aborted) {

Add Checkbox In WPF Datagrid DatagridTemplateColumnHeader (For Checkall)

Image
Hello, Here's how to add checkbox in WPF Datagrid DatagridTemplateColumn to simulate checkall. I have included the TSQL script, XAML and C# codes. Perform these steps below 1. Create an MSSQL database with the following Product Table schema below: Table: Products Fields: -    ProductID (int, autoincrement) -    ProductName (varchar) -    UnitPrice (decimal) -    QuantityPerUnit (varchar) -    Discontinue (bit)  SQL Script: USE [Your_Database_Name] GO /****** Object: Table [dbo].[Products] Script Date: 05/27/2013 14:07:39 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[Products]( [ProductID] [int] NOT NULL , [ProductName] [varchar](50) NULL , [UnitPrice] [decimal](18, 2) NULL , [QuantityPerUnit] [varchar](50) NULL , [Discontinue] [bit] NULL , CONSTRAINT [PK_Products] PRIMARY KEY CLUSTERED ( [productID] ASC ) WITH

Prevent DataGridView Last Row From Being Sorted On Column Click

Image
Hello, You might have a row in the DataGridView typically the last one that computes total and the grid control is unbound to a datasource. And then if a sorting event occurs, you dont' wanna include that row during sort event. So given that your application has a form and a DataGridView control, the code to perform databinding is handled in the Form Load Event. DataGridViewRow dgRowTotalCount; DataTable dataTable; private void Form1_Load ( object sender, EventArgs e) { 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 [] { "2" , 200.52 }); dt.Rows.Add( new object [] { "6" , 500.24 }); dt.Rows.Add( new object [] { "8" , 1000.11 }); dt.Rows.Add( new object [] { "4" , 400.31 }); dt.Rows.Add( new object [] { "5" , 6

Donate