Thursday, July 1, 2010

How to get Drop Down List Selected Text in ASP .NET by using jQuery

The Following code sample shows how to get selected text out from ASP .NET Drop Down List.
<script type="text/javascript">

    function GetSelectedText() {
        var ddlID = '#' + '<%= ddlEmployees.ClientID %>';
        var selectedText = $(ddlID + " option:selected").text();
        return selectedText;


<asp:DropDownList ID="ddlEmployees" runat="server">
    <asp:ListItem Text="Employee Name 1" Value="1"></asp:ListItem>
        <asp:ListItem Text="Employee Name 2" Value="2"></asp:ListItem>
        <asp:ListItem Text="Employee Name 2" Value="2"></asp:ListItem>

Tuesday, June 1, 2010

How to Dynamically Select rows in SQL Server

If you want to restrict other developers from using your Stored Procedures that returns a huge amount of data which affects server performance and Application Performance weather it is a Windows or Web Application.

You can use the following idea when you create Stored Procedures:
-- =============================================
-- Author:        Amr Saafan
-- Create date: Jan 5, 2010
-- Description:    Get Employees
-- =============================================
@RowsCount INT = 10
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.

SELECT TOP (@RowsCount) [EmployeeID], [EmployeeName], [EmployeeEmail]
FROM [dbo].[Employee]

As we can see whenever the Stored Procedure called, it will not return more than 10 rows which is a reasonable amount of rows and in the same time if you want less or more you can just pass the number of rows you need @RowsCount = 1 or 1000

Saturday, May 1, 2010

How to convert Data Row to Data Row View

Converting Data Row to Data Row View is not possible, but we can go around this by using DefaultView property of the Data Row Table.

The following code example demonstrate how to get Data Row View out of Data Row:
DataTable dt = new DataTable();

dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Name", typeof(string));

for (int i = 0; i < 10; i++)
 DataRow tempDR = dt.NewRow();
 tempDR["ID"] = (i + 1);
 tempDR["Name"] = "Name " + (i + 1).ToString();

DataRow dr = dt.Rows[1]; 
DataRowView drv = dr.Table.DefaultView[dt.Rows.IndexOf(dr)];

Thursday, April 1, 2010

Prevent Saving Changes That Require Table Re-creation SQL Server 2008

I was modifying an existing table using SQL Server Management Studio 2008 R2, but I got this error message on saving:

Saving changes is not permitted. The changes that you have made require the following tables to be dropped and re-created. You have either made changes to a table that can't be re-created or enabled the option Prevent saving changes that require the table to be re-created.

This error occurs when the Prevent saving changes that require the table re-creation option is enabled. Also it occurs if you made one of these changes to the table:
  • Adding a new column
  • Change column Data type
  • Allow Nulls for a column
  • Change the order of a the table columns

To disable Prevent saving changes that require the table re-creation option follow these steps:
  • Open SQL Server Management Studio 2008
  • Go to Tools menu then Options
  • On Options Tree view, Click Designers
  • Unchecked Prevent saving changes that require the table re-creation option

Monday, March 1, 2010

A potentially dangerous Request.Form value was detected from the client ASP.NET

I got this error message when I try to submit a form with HTML Code, the default settings of the .NET Framework will not allow me to submit HTML Code to prevent cross-site scripting (XSS) attacks.

To avoid this error set Validate Request to false in the Page Directive.

Monday, February 1, 2010

How to remove HTML Tags from string in C#

I will show you three different methods to remove HTML tags from string in C#:

1. By using Regex:
public static string RemoveHTMLTags(string html)
 return Regex.Replace(html, "<.*?>", string.Empty);

2. By using Compiled Regex for better performance:
static Regex htmlRegex = new Regex("<.*?>", RegexOptions.Compiled);
public static string RemoveHTMLTagsCompiled(string html)
 return htmlRegex.Replace(html, string.Empty);

3. By using Char Array for faster performance for several HTML files:
public static string RemoveHTMLTagsCharArray(string html)
 char[] charArray = new char[html.Length];
 int index = 0;
 bool isInside = false;
 for (int i = 0; i < html.Length; i++)
   char left = html[i];

   if (left == '<')
     isInside = true;

   if (left == '>')
     isInside = false;

   if (!isInside)
     charArray[index] = left;

 return new string(charArray, 0, index);

Friday, January 1, 2010

How to Turn on/off tracing for entire ASP.NET Site or Application

Open up your web.config file - then, somewhere in the system.web section, put this :
<trace enabled="true" localonly="false" pageoutput="true"></trace>
Set it to false if you don't want it to show up.