Tuesday, February 9, 2010

Individual Field Validation in ASP .NET

When you have text box on your form (or drop down lists that include invalid values) it can make sense to validate the field immediately after the field has been updated rather than waiting until the entire form has been filled out. By doing this the person filling out the form can know immediately that the value that they have entered is invalid allowing them to correct their entry before filling out the rest of the form. In some cases telling them that the first field is invalid straight away may let them know that they are filling out the wrong form before they waste time filling out all of the fields.

We can validate text boxs straight away using the onblur event handler. When we add this event handler to our text boxs and text areas the appropriate validation routine will be called as soon as the person moves on to another field.

Of course we will still need to validate the same field immediately prior to submitting the form just in case someone bypasses accessing the field at all. We can easily add the same validation to both places by placing all of the code for the validation into a function and calling that function from both places.

Here is what the code for a text boxlooks like with the validation processing added:



<asp:TextBox ID="txtEmail" runat="server" onblur="ValidateControl(this)" ></asp:TextBox>



<asp:RequiredFieldValidator ID="reqTxtEmail" runat="server" SetFocusOnError="true"
ControlToValidate="txtEmail" ErrorMessage=" required" EnableClientScript="true" ></asp:RequiredFieldValidator>

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" SetFocusOnError="true" ControlToValidate="txtEmail" ErrorMessage=" invalid"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ></asp:RegularExpressionValidator>



In this example ValidateControl(this) is called OnBlur of the TextBox. This method finds all validator controls for this text box in Page_Validators array, then call ValidatorValidate function of Asp.Net to validate. Javascript to validate is,


function ValidateControl(controltovalidate) {

if (typeof (Page_Validators) != "undefined") {

for (i = 0; i < Page_Validators.length; i++) {

val = Page_Validators[i];

if (val.controltovalidate == controltovalidate.id) {

ValidatorValidate(val);

if (val.isvalid==false) {

return;

}

}

}

}

}

Tuesday, January 20, 2009

Regular Expression for Date

Regular Expression for Date
^([1-9]0[1-9]1[0-2])/([1-9]0[1-9][1-2][0-9]3[01])/([1-9][0-9][0-9][0-9])$
It accepts following format
  • mm/dd/yyyy
  • m/d/yyyy

It accepts following value

  • any value of month in between 01-12 or 1-12
  • any value of day in between 01-31 or 1-31
  • any value of year in between 1000-9999

Monday, October 13, 2008

Redirect Parent page from IFrame

That is a common issue while working with IFrames. One solution to this problem can be Javascript.

Add a Javascript like below:

if (parent.document) { 
parent.document.location.href='http://www.google.com';
 }

In  ASP .NET,  register above javascript statement using 
Page.RegisterStartupScript( ) method


Thursday, September 18, 2008

Date Formats in SQL Server

One of the most frequently asked questions in SQL Server forums is how to format a datetime value or column into a specific date format.  Here's a summary of the different date formats that come standard in SQL Server as part of the CONVERT function.  Following the standard date formats are some extended date formats that are often asked by SQL Server developers.

It is worth to note that the output of these date formats are of VARCHAR data types already and not of DATETIME data type.  With this in mind, any date comparisons performed after the datetime value has been formatted are using the VARCHAR value of the date and time and not its original DATETIME value.

Standard Date Formats

Date Format

Standard

SQL Statement

Sample Output

Mon DD YYYY 1
HH:MIAM (or PM)

Default

SELECT CONVERT(VARCHAR(20), GETDATE(), 100)

Jan 1 2005 1:29PM 1

MM/DD/YY

USA

SELECT CONVERT(VARCHAR(8), GETDATE(), 1) AS [MM/DD/YY]

11/23/98

MM/DD/YYYY

USA

SELECT CONVERT(VARCHAR(10), GETDATE(), 101) AS [MM/DD/YYYY]

11/23/1998

YY.MM.DD

ANSI

SELECT CONVERT(VARCHAR(8), GETDATE(), 2) AS [YY.MM.DD]

72.01.01

YYYY.MM.DD

ANSI

SELECT CONVERT(VARCHAR(10), GETDATE(), 102) AS [YYYY.MM.DD]

1972.01.01

DD/MM/YY

British/French

SELECT CONVERT(VARCHAR(8), GETDATE(), 3) AS [DD/MM/YY]

19/02/72

DD/MM/YYYY

British/French

SELECT CONVERT(VARCHAR(10), GETDATE(), 103) AS [DD/MM/YYYY]

19/02/1972

DD.MM.YY

German

SELECT CONVERT(VARCHAR(8), GETDATE(), 4) AS [DD.MM.YY]

25.12.05

DD.MM.YYYY

German

SELECT CONVERT(VARCHAR(10), GETDATE(), 104) AS [DD.MM.YYYY]

25.12.2005

DD-MM-YY

Italian

SELECT CONVERT(VARCHAR(8), GETDATE(), 5) AS [DD-MM-YY]

24-01-98

DD-MM-YYYY

Italian

SELECT CONVERT(VARCHAR(10), GETDATE(), 105) AS [DD-MM-YYYY]

24-01-1998

DD Mon YY 1

-

SELECT CONVERT(VARCHAR(9), GETDATE(), 6) AS [DD MON YY]

04 Jul 06 1

DD Mon YYYY 1

-

SELECT CONVERT(VARCHAR(11), GETDATE(), 106) AS [DD MON YYYY]

04 Jul 2006 1

Mon DD, YY 1

-

SELECT CONVERT(VARCHAR(10), GETDATE(), 7) AS [Mon DD, YY]

Jan 24, 98 1

Mon DD, YYYY 1

-

SELECT CONVERT(VARCHAR(12), GETDATE(), 107) AS [Mon DD, YYYY]

Jan 24, 1998 1

HH:MM:SS

-

SELECT CONVERT(VARCHAR(8), GETDATE(), 108)

03:24:53

Mon DD YYYY HH:MI:SS:MMMAM (or PM) 1

Default +
milliseconds

SELECT CONVERT(VARCHAR(26), GETDATE(), 109)

Apr 28 2006 12:32:29:253PM 1

MM-DD-YY

USA

SELECT CONVERT(VARCHAR(8), GETDATE(), 10) AS [MM-DD-YY]

01-01-06

MM-DD-YYYY

USA

SELECT CONVERT(VARCHAR(10), GETDATE(), 110) AS [MM-DD-YYYY]

01-01-2006

YY/MM/DD

-

SELECT CONVERT(VARCHAR(8), GETDATE(), 11) AS [YY/MM/DD]

98/11/23

YYYY/MM/DD

-

SELECT CONVERT(VARCHAR(10), GETDATE(), 111) AS [YYYY/MM/DD]

1998/11/23

YYMMDD

ISO

SELECT CONVERT(VARCHAR(6), GETDATE(), 12) AS [YYMMDD]

980124

YYYYMMDD

ISO

SELECT CONVERT(VARCHAR(8), GETDATE(), 112) AS [YYYYMMDD]

19980124

DD Mon YYYY HH:MM:SS:MMM(24h) 1

Europe default + milliseconds

SELECT CONVERT(VARCHAR(24), GETDATE(), 113)

28 Apr 2006 00:34:55:190 1

HH:MI:SS:MMM(24H)

-

SELECT CONVERT(VARCHAR(12), GETDATE(), 114) AS [HH:MI:SS:MMM(24H)]

11:34:23:013

YYYY-MM-DD HH:MI:SS(24h)

ODBC Canonical

SELECT CONVERT(VARCHAR(19), GETDATE(), 120)

1972-01-01 13:42:24

YYYY-MM-DD HH:MI:SS.MMM(24h)

ODBC Canonical
(with milliseconds)

SELECT CONVERT(VARCHAR(23), GETDATE(), 121)

1972-02-19 06:35:24.489

YYYY-MM-DDTHH:MM:SS:MMM

ISO8601

SELECT CONVERT(VARCHAR(23), GETDATE(), 126)

1998-11-23T11:25:43:250

DD Mon YYYY HH:MI:SS:MMMAM 1

Kuwaiti

SELECT CONVERT(VARCHAR(26), GETDATE(), 130)

28 Apr 2006 12:39:32:429AM 1

DD/MM/YYYY HH:MI:SS:MMMAM

Kuwaiti

SELECT CONVERT(VARCHAR(25), GETDATE(), 131)

28/04/2006 12:39:32:429AM


Here are some more date formats that does not come standard in SQL Server as part of the CONVERT function.

Extended Date Formats

Date Format

SQL Statement

Sample Output

YY-MM-DD

SELECT SUBSTRING(CONVERT(VARCHAR(10), GETDATE(), 120), 3, 8) AS [YY-MM-DD]

99-01-24

YYYY-MM-DD

SELECT CONVERT(VARCHAR(10), GETDATE(), 120) AS [YYYY-MM-DD]

1999-01-24

MM/YY

SELECT RIGHT(CONVERT(VARCHAR(8), GETDATE(), 3), 5) AS [MM/YY]
SELECT SUBSTRING(CONVERT(VARCHAR(8), GETDATE(), 3), 4, 5) AS [MM/YY]

08/99

MM/YYYY

SELECT RIGHT(CONVERT(VARCHAR(10), GETDATE(), 103), 7) AS [MM/YYYY]

12/2005

YY/MM

SELECT CONVERT(VARCHAR(5), GETDATE(), 11) AS [YY/MM]

99/08

YYYY/MM

SELECT CONVERT(VARCHAR(7), GETDATE(), 111) AS [YYYY/MM]

2005/12

Month DD, YYYY 1

SELECT DATENAME(MM, GETDATE()) + RIGHT(CONVERT(VARCHAR(12), GETDATE(), 107), 9) AS [Month DD, YYYY]

July 04, 2006 1

Mon YYYY 1

SELECT SUBSTRING(CONVERT(VARCHAR(11), GETDATE(), 113), 4, 8) AS [Mon YYYY]

Apr 2006 1

Month YYYY 1

SELECT DATENAME(MM, GETDATE()) + ' ' + CAST(YEAR(GETDATE()) AS VARCHAR(4)) AS [Month YYYY]

February 2006 1

DD Month 1

SELECT CAST(DAY(GETDATE()) AS VARCHAR(2)) + ' ' + DATENAME(MM, GETDATE()) AS [DD Month]

11 September 1

Month DD 1

SELECT DATENAME(MM, GETDATE()) + ' ' + CAST(DAY(GETDATE()) AS VARCHAR(2)) AS [Month DD]

September 11 1

DD Month YY 1

SELECT CAST(DAY(GETDATE()) AS VARCHAR(2)) + ' ' + DATENAME(MM, GETDATE()) + ' ' + RIGHT(CAST(YEAR(GETDATE()) AS VARCHAR(4)), 2) AS [DD Month YY]

19 February 72 1

DD Month YYYY 1

SELECT CAST(DAY(GETDATE()) AS VARCHAR(2)) + ' ' + DATENAME(MM, GETDATE()) + ' ' + CAST(YEAR(GETDATE()) AS VARCHAR(4)) AS [DD Month YYYY]

11 September 2002 1

MM-YY

SELECT RIGHT(CONVERT(VARCHAR(8), GETDATE(), 5), 5) AS [MM-YY]
SELECT SUBSTRING(CONVERT(VARCHAR(8), GETDATE(), 5), 4, 5) AS [MM-YY]

12/92

MM-YYYY

SELECT RIGHT(CONVERT(VARCHAR(10), GETDATE(), 105), 7) AS [MM-YYYY]

05-2006

YY-MM

SELECT RIGHT(CONVERT(VARCHAR(7), GETDATE(), 120), 5) AS [YY-MM]
SELECT SUBSTRING(CONVERT(VARCHAR(10), GETDATE(), 120), 3, 5) AS [YY-MM]

92/12

YYYY-MM

SELECT CONVERT(VARCHAR(7), GETDATE(), 120) AS [YYYY-MM]

2006-05

MMDDYY

SELECT REPLACE(CONVERT(VARCHAR(10), GETDATE(), 1), '/', '') AS [MMDDYY]

122506

MMDDYYYY

SELECT REPLACE(CONVERT(VARCHAR(10), GETDATE(), 101), '/', '') AS [MMDDYYYY]

12252006

DDMMYY

SELECT REPLACE(CONVERT(VARCHAR(10), GETDATE(), 3), '/', '') AS [DDMMYY]

240702

DDMMYYYY

SELECT REPLACE(CONVERT(VARCHAR(10), GETDATE(), 103), '/', '') AS [DDMMYYYY]

24072002

Mon-YY 1

SELECT REPLACE(RIGHT(CONVERT(VARCHAR(9), GETDATE(), 6), 6), ' ', '-') AS [Mon-YY]

Sep-02 1

Mon-YYYY 1

SELECT REPLACE(RIGHT(CONVERT(VARCHAR(11), GETDATE(), 106), 8), ' ', '-') AS [Mon-YYYY]

Sep-2002 1

DD-Mon-YY 1

SELECT REPLACE(CONVERT(VARCHAR(9), GETDATE(), 6), ' ', '-') AS [DD-Mon-YY]

25-Dec-05 1

DD-Mon-YYYY 1

SELECT REPLACE(CONVERT(VARCHAR(11), GETDATE(), 106), ' ', '-') AS [DD-Mon-YYYY]

25-Dec-2005 1

1 To make the month name in upper case, simply use the UPPER string function.

Thursday, March 13, 2008

Handling OnSelectedIndexChange event of DropDownList in Data Repeater

Few days ago, I faced a problem while registering OnSelectedIndexChanged event of a DropDownList (which is Embedded in a Data Repeater). Problem is, when I change Selected Index of that Drop Down List, it fires the event but do not run the Method registered to that event. the Scenario is described below.

<asp:Repeater ID="PrescriptionRepeater" runat="server">
<ItemTemplate>
<asp:DropDownList runat="server" ID="ddlStatus" OnSelectedIndexChange="ddlStatus_SelectedIndexChanged" autopostback="true">
<asp:ListItem Text="Prescribed"></asp:ListItem>
<asp:ListItem Text="Cancelled"></asp:ListItem>
<asp:ListItem Text="Lost"></asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:Repeater>


I have Placed a DropDownList(id="ddlStatus") in a DataRepeater and set
OnSelectedIndexChange="
ddlStatus_SelectedIndexChanged" autopostback="true". Now when I run the Page and change selected index of drop down list, it posts back but does not run the method ddlStatus_SelectedIndexChanged .


Then I have tried to register the Event by Hard coding.
ddlStatus.SelectedIndexChanged += new EventHandler(
ddlStatus_SelectedIndexChanged);
But still there is no change. Finally, my senior coleage find out a solution

That is Page.Request.Params.Get("__EVENTTARGET") . This method returns name of the control that caused PostBack. Then what we did was, find that control and call a method for further processing

if (Page.IsPostBack)
{
DropDownList ddlStatus = (DropDownList)Page.FindControl(Page.Request.Params["__EVENTTARGET"]);
if (ddlStatus != null)
{
DDLStateSelectedIndexChangedEvent(ddlStatus);
}
}


*I have not checked yet what to do If you have more than one dropdownlist
causing Postback in a single row of DataRepeater.
*Tested on Visual Studio 2005 and .Net 2.0
Want to know details? click below
Determining the Control that Caused a PostBack


Tuesday, February 12, 2008

iTank - Vision Control Robot

Introduction:

A robot is an electro-mechanical device that can perform autonomous or preprogrammed tasks. A robot may act under the direct control of a human (e.g. the robotic arm of the space shuttle) or autonomously under the control of a programmed computer.

Nowadays robots are widely used as surveillance purpose in modern world.

To keep track with the modern world we have stepped to design and develop a small robot that is able to track a specified colored object around through image processing. We named it Intelligent Tank (iTank). It is artificially intelligent, it can track any user specified colored object. Again we call it a tank; it has a Laser light to point the object. We used Laser light as a model of a gun (it was not possible for us to install a real firing device like gun on it). Whenever iTank detects a user specified object, Laser light points to that object as if it is trying to shoot the object.

Anatomy:

iTank has three sections. The software, the circuits and the mechanical devices. Mechanical section consists of stepper motors attached with wheels and laser gun. iTank is based on a wooden structure. Circuit section has two parts, one is logic circuit and another is motor driver circuit. The software section is implemented solely in java with JMF (Java Media Framework) and parallel port control API.

Difficulties we faced:

In Bangladesh, main barrier in developing robot is scarcity of hardware. We had to depend on Dholai Khaal for motors, wheels and gears, Stadium Market for ICs and Internet (google search) for references. Problem was, what we learn from Internet is not always available in the local market and what we get from here is not on the Internet. It’s very tough to make a good combination of three Ws (What we know, What we have and What we want). Making a good workable physical structure was another issue. We had to change iTank’s structural design 4 times, which eventually increased our project cost.

Future Direction:

At present communication between iTank and computer is done by wires. But wireless communication will be best one to use it in practical purposes. iTank’s intelligence is not enough to serve practical purposes. Better AI capabilities like human tracker/bomb detector can be implemented in future.

We are:

Foyzul Hassan – shakil_oxygen@yahoo.com
Mohammad Mahedi Hasan – mahedi_018bd@yahoo.com
Goutom Kumar – apu2000bd@yahoo.com &
Md. Khairul Anam – mdkhairulanam@gmail.com

CSE-3 , MIST

Tuesday, February 5, 2008

"নেই কাজ তো ভেরেন্ডা ভাজ" - আসুন আমরা ভেরেন্ডা ("বায়ো ডিজেল") ভাজি

"নেই কাজ তো ভেরেন্ডা ভাজ" - এমন কথা ঠাট্টা করে আমরা অনেক বলেছি। কিন্তু এখন সময় এসেছে ভেরেন্ডা ভাজার।

সম্প্রতি "বায়ো ডিজেল" এর জয়জয়কার চারদিকে। সেই দিন ইটিভি তে এটা নিয়ে একটা আলোচনা অনুষ্ঠান দেখলাম। আমাদের দেশে গ্রামে বাড়ির আশে পাশে ভেরেন্ডা গাছ দেখা যায়। এই গাছের ফল ভেরেন্ডা হলো "বায়ো ডিজেল" এর কাচামাল।

"বায়ো ডিজেল" কি?
এটা উদ্ভিদের বীজ থেকে উৎপন্ন এক ধরনের জ্বালানী যা ডিজেল ইন্জিনে ব্যবহার করা যায়। এর ব্যবহার বৃদ্ধি করতে পারলে খনি থেকে প্রাপ্ত ডিজেলের উপর চাপ কমানো যাবে।
"ভেরেন্ডা " কি?
ভেরেন্ডা গাছের ইংরেজি নাম Jatropha (যেট্রোফা)"। এটা যেকোনো পতিত জমি তে চাষ করা যায়, এমন কি মরু ভুমি তেও চাষ করা যায়। প্রথম ২বছর কোনো ফল দেয়না। পরবর্তী ৫০বছর পর্যন্ত ফল দেয় এবং প্রতি বছর ফলন পুর্ববর্তী বছরের চেয়ে বেশি হয়। এই গাছের ফল থেকে যে বীজ পাওয়া যায় তা থেকে বিশেষ উপায়ে (কম খরচে) যে তেল পাওয়া যায় তে আমরা ডিজেল এর বিকল্প হিসেবে ব্যবহার করতে পারি। এছাড়া তেল তৈরী করার সময় যে খৈল পাওয়া যায় তা সার হিসেবে ব্যবহার করা যায়, আর সাবানের জন্য গ্লিসারিন ও পাওয়া যায়।

বিশ্বের অন্যান্য দেশ কি ভাবছে?
সব দেশের খবর জানিনা। শুধু এতটুকু জানি, ইন্ডিয়া ইতিমধ্যে ১০লাখ হেক্টর জমিতে এর চাষ শুরু করেছে।

বাংলাদেশ কি ভাবছে?
সবার খবর জানিনা। শুধু এতটুকু জানি, একজন প্রবাসী বাঙালি (ইটিভি তে উনি সাক্ষাতকারে বলেছেন) চেষ্টা চালাছেন কিছু করার জন্য। আমরা আমাদের পতিত জমি যেমন, রাস্তার পাশে, সুন্দরবন, কক্সবাজার সৈকতের পাশে এর ব্যপক চাষ করতে পারি।
এতে করে আমাদের দেশের ডিজেল চাহিদাতো মিটবেই, সেই সাথে বিপুল বৈদেশিক মুদ্রা সাশ্রয় হবে

বায়োফুয়েলকে জ্বালানি হিসেবে উৎসাহিত করবো আমি, কিন্তু খাদ্যশস্য উপেক্ষা করে জ্বালানি উৎপাদনকে সমর্থন করিনা ।

কিছু ছবি সংযুক্ত করলাম....




bdnews24.com | Bangla Version

The Daily Star