Validating FileUpload control for specific file type and required field.

In this article I am going to explain how we can check and validate user for uploading only certain files and making uploading of file to be must for submission of form.

For validating for required field we have to use RequiredFieldValidator and for validating for file types we’ll have to use RegularExpressionValidator. But, before starting we should have brief idea about the controls required.

Before starting about validating FileUpload we must have brief idea about FileUpload control and Validation Controls of ASP.Net

FileUpload Control:

FileUpload control is used for uploading file to the server from the client side.

For detailed reading about FileUpload visit: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.aspx

RequiredFieldValidator:

With this control, the validation fails if the input value does not change from its initial value. By default, the initial value is an empty string (“”).

For more details visit: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.requiredfieldvalidator.aspx

RegularExpressionValidator:

The RegularExpressionValidator control is used to ensure that an input value matches a specified pattern. The validation will not fail if the input control is empty. Use the RequiredFieldValidator control to make the field required.

For more details about RegularExpressionValidator visit following link: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.regularexpressionvalidator.aspx

Validating FileUpload Control in ASP.Net

Validating File Upload Control for certain file types. Here in the example below I am using RegularExpressionValidator to accomplish my task.

<asp:FileUpload ID="FileUpload1" runat="server" />

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Only .doc , .docx or .txt files are allowed." 
ValidationExpression="^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.doc|.docx|.txt)$" ControlToValidate="FileUpload1">*</asp:RegularExpressionValidator>

Here, regular expression for .doc, .docx, .txt files is:

"^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.doc|.docx|.txt)$"

If you want some more or other files type to be uploaded then you can replace or add more extension with the above mentioned extensions. Suppose you want your user to upload only .pdf file then you Regular expression for that will be:

"^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.pdf)$"

But, this does not mean that user will be forced to upload file, this validation is only performed if user tries to upload file. If you want to make uploading of file mandatory then you have to use RequiredFieldValidator.

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="You have not uploaded your file" ControlToValidate="FileUpload1">*</asp:RequiredFieldValidator>

The code above will validate FileUpload1 for required field.

Case statement in SQL Server

The SQL Server CASE statement is a conditional statement that returns a single value based on the evaluation of a statement. CASE expressions can be used in SQL anywhere an expression can be used like SELECT list, WHERE clause, HAVING clause, IN list, DELETE and UPDATE statements. CASE statement can also be nested. This provides a lot of pliability for evaluating multiple expressions.

SQL Case statement can be used in 2 forms:

  1. SQL CASE statement with simple expression to compare and get results.
  2. SQL CASE statement with search or comparison expression to get results.
  1. SQL CASE statement with simple expression to compare and get results

    A simple CASE expression operates by comparing the first expression to the expression in each WHEN clause for equivalency.

    Syntax

    CASE expression
        WHEN expression1 THEN expression1
        [[WHEN expression2 THEN expression2] [...]]
        [ELSE expression N]
    END
    

    Example

    SELECT CASE [Section]
        WHEN 1 THEN 'One'
        WHEN 2 THEN 'Two'
        WHEN 3 THEN 'Three'
        WHEN 4 THEN 'Four'
        WHEN 5 THEN 'Five'
        ELSE 'Greater than Five'
    END as [Sec_Word] FROM [Table_Test]
    
  2. SQL CASE statement with search or comparison expression to get results

    A search or comparison CASE expression allows comparison operators, and the use of AND and/or OR between each Boolean expression.

    Syntax

    CASE
        WHEN Boolean_expression1 THEN expression1
        [[WHEN Boolean_expression2 THEN expression2] [...]]
        [ELSE expression N]
    END
    

    Example

    SELECT CASE
        WHEN [Section] < 5 THEN 'Less than Five'
        WHEN [Section] = 5 THEN 'Equal to Five'
        WHEN [Section] > 5 THEN 'Greater than Five'
    END as [Sec_Word] FROM [Table_Test]
    

Hope you will find this post helpful. For any suggestion or query please feel free to post your valuable comments. Thanks.

Introduction to JOINs in SQL Server

The nature of relational database design shows that we will have related data that is stored in different tables. To retrieve data from two or more tables we use JOINS.

The JOIN clause tells the database how the data in the two tables is related so that it can return a correct representation of the related data.

Before entering into JOINs concept in SQL Server first design two tables in database, which I will be using to give you a better understanding of JOINs.

Table1Table2

In this blog I am going to discuss about two JOIN type:

  1. INNER JOIN
  2. OUTER JOIN

INNER JOIN

The INNER JOIN keyword return rows when there is at least one match in both tables.

inner-join

Example of Inner Join in SQL Server

SELECT * FROM Table1 t1
INNER JOIN Table2 t2
ON t1.ID = t2.ID

Output of Inner Join in SQL Server
Inner-Join

OUTER JOIN

There are three types of OUTER JOIN’s in SQL Server

  • LEFT OUTER JOIN
  • RIGHT OUTER JOIN
  • FULL OUTER JOIN

LEFT OUTER JOIN

LEFT OUTER JOIN in SQL Server returns all the rows from the left table in conjunction with the matching rows from the right table. If there are no columns matching in the right table, it returns NULL values.

left-join

Example of Left Outer Join in SQL Server

SELECT * FROM Table1 t1
LEFT OUTER JOIN Table2 t2
ON t1.ID = t2.ID

Output of Left Outer Join in SQL Server
Left-Outer-Join

RIGHT OUTER JOIN

RIGHT OUTER JOIN returns all the rows from the right table in conjunction with the matching rows from the left table. If there are no columns matching in the left table, it returns NULL values.

right-join

Example of Right Outer Join in SQL Server

SELECT * FROM Table1 t1
RIGHT OUTER JOIN Table2 t2
ON t1.ID = t2.ID

Output of Right Outer Join in SQL Server
RIGHT-OUTER-JOIN

FULL OUTER JOIN

FULL OUTER JOIN returns rows from either table when the conditions are met and returns null value when there is no match.

outer-join

Example of Full Outer Join in SQL Server

SELECT * FROM Table1 t1
FULL OUTER JOIN Table2 t2
ON t1.ID = t2.ID

Output of Full Outer Join in SQL Server
FULL-OUTER-JOIN

Find child control of a control in WPF

Sometime we want to get reference of child control of container control of specified type. Example we want to find CheckBox control inside ListViewItem or we want to find another container Panel inside a Grid.

Here is a method through which we can find child control inside a parent control. The method below will return the first matching control of the specified type (T), if control of specified type is not found in the provided parent control it will return null.

/// <summary>
/// Method to get child control of specified type
/// </summary>
/// <typeparam name="T">Type of child control queried</typeparam>
/// <param name="parent">Reference of parent control in which child control resides</param>
/// <returns>Returns reference of child control of specified type (T) if found, otherwise it will return null.</returns>

private static T FindVisualChild<T>(DependencyObject parent)
where T : DependencyObject
{
     for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
     {
          DependencyObject child = VisualTreeHelper.GetChild(parent, i);

          if (child != null && child is T)
               return (T)child;
          else
          {
               T childOfChild = FindVisualChild<T>(child);
               if (childOfChild != null)
                    return childOfChild;
          }
     }
return null;
}

To find parent control of a control in WPF, refer to Getting Parent Control of a Contol in WPF through Code or at Run-time.

Hope you will find it helpful, if you have any doubt please feel free to ask.

Getting parent control of a control in WPF through code or at runtime

I was working on a WPF project where I came across a problem where I need to refer to the parent container/control of specific type of control/item in WPF. So I did some RnD and search on the net for the problem and finally I came across a solution which I want to share.

Suppose we want to get ListViewItem on which a Button control is placed or we want to get the reference of that ListView in WPF.

Here is a method through which we can find parent of a control.

/// <summary>
/// Finds a parent of a given control/item on the visual tree.
/// </summary>
/// <typeparam name="T">Type of Parent</typeparam>
/// <param name="child">Child whose parent is queried</param>
/// <returns>Returns the first parent item that matched the type (T), if no match found then it will return null</returns>

public static T TryFindParent<T>(this DependencyObject child)
where T : DependencyObject
{
     DependencyObject parentObject = VisualTreeHelper.GetParent(child);
     if (parentObject == null) return null;

     T parent = parentObject as T;
     if (parent != null)
          return parent;
     else
          return TryFindParent<T>(parentObject);
}

To find child control of a control, refer to Find Child Control of a Control in WPF.

Hope you will find this blog helpful.