Search This Blog

Friday, July 29, 2011

AsyncFileUpload control OnUploadedComplete event is not firing in asp.net


Problem:
AsyncFileUpload control in updatepanel. The control is not firing OnUploadedComplete server side event when it’s in updatepanel.


Solution:
1) Add the following attributes to the "form" tag of the page..
enctype="multipart/form-data" method="post"

How to avoid copy, paste and backspace error on AsyncFileUpload click on below link

http://challadotnetfaq.blogspot.com/2011/08/how-to-avoid-copy-paste-and-backspace.html


Thursday, July 28, 2011

SQL SERVER – Insert Data From One Table to Another Table – INSERT INTO SELECT – SELECT INTO TABLE




How to insert data from one table to another table efficiently?
How to insert data from one table using where condition to anther table?
How can I stop using cursor to move data from one table to another table?


There are two different ways to implement inserting data from one table to another table. I strongly suggest to use either of the method over cursor. Performance of following two methods is far superior over cursor. I prefer to use Method 1 always as I works in all the case.

Method 1 : INSERT INTO SELECT
This method is used when table is already created in the database earlier and data is to be inserted into this table from another table. If columns listed in insert clause and select clause are same, they are are not required to list them. I always list them for readability and scalability purpose.

GO
----Create TestTable
CREATE TABLE TestTable (FirstName VARCHAR(100), LastName VARCHAR(100))
----INSERT INTO TestTable using SELECT
INSERT INTO TestTable (FirstName, LastName)
SELECT FirstName, LastName
FROM Person.Contact
WHERE EmailPromotion = 2
----Verify that Data in TestTable
SELECT FirstName, LastName
FROM TestTable
----Clean Up Database
DROP TABLE TestTable
GO

Method 2 : SELECT INTO
This method is used when table is not created earlier and needs to be created when data from one table is to be inserted into newly created table from another table. New table is created with same data types as selected columns.



GO
----Create new table and insert into table using SELECT INSERT
SELECT FirstName, LastName
INTO TestTable
FROM Person.Contact
WHERE EmailPromotion = 2
----Verify that Data in TestTable
SELECT FirstName, LastName
FROM TestTable
----Clean Up Database
DROP TABLE TestTable
GO

Procedure or function '' expects parameter '', which was not supplied. exception in sql server while passing parameters from asp.net

Q : Procedure or function '' expects parameter '', which was not supplied.

A:

Below proc throwing the exception if you pass null from ado.net to the stored procedure parameters



alter PROCEDURE [dbo].SP_UpdateHomePageContentDetails
@VideoLink varchar(65) ,
@VideoLinkDescription varchar(65) ,
@P_ReturnValue varchar(50) output
AS
BEGIN



Solution for the above problem is passing null to the parameters as a default value



alter PROCEDURE [dbo].SP_UpdateHomePageContentDetails
@VideoLink varchar(65) = null,
@VideoLinkDescription varchar(65) = null,
@P_ReturnValue varchar(50) output
AS
BEGIN


Wednesday, July 27, 2011

Embedding Youtube videos into asp.net pages



use below code to embed youtube video into your html or .aspx pages


iframe width="219" height="154" src="http://www.youtube.com/embed/HHAx2qIGMl4?controls=0" frameborder="0"
allowfullscreen



or


Below code works fine in .aspx pages


Tuesday, July 26, 2011

Email validation using javascript

function EmailValidate() {

var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
var address = document.getElementById("txtEmail").value;
if (reg.test(address) == false) {

alert('Invalid Email Address');
return false;
}
}


asp:TextBox ID="txtEmail" runat="server">

asp:Button ID="Button1" runat="server" Text="Validate" OnClientClick="return Validate();"

How to allow only numbers into textbox using javascript, asp.net , Allow only numbers validation using javascript

function numbersonly(e) {
var unicode = e.charCode ? e.charCode : e.keyCode
if (unicode != 8) { //if the key isn't the backspace key (which we should allow)
if (unicode < 48 || unicode > 57) //if not a number
return false //disable key press
}
}



asp:TextBox ID="tbxBkgRefNo" runat="server" onkeypress="return numbersonly(event);"

Friday, July 22, 2011

Date difference in Javascript using asp.net and Calculating Duration Between Two Dates in Years, Months and Days

Use below javascript function to

1) validate the date difference in asp.net
2) difference (number of days, months, years) between two dates.


function validate(ArrivalDate, DepartureDate )
{
var ArrivalDtValue = "15/02/2011"; // ArrivalDate
var DepartureDtValue = "16/02/2011"; // DepartureDate

var currentTime = new Date();
var month = currentTime.getMonth();
var day = currentTime.getDate();
var year = currentTime.getFullYear();
var currentDt = day + "/" + month + "/" + year;

var ArrDt = getDateObject(ArrivalDtValue, "/");
var DeparDt = getDateObject(DepartureDtValue, "/");
var CurDt = new Date();

//Total time for one day
var one_day = 1000 * 60 * 60 * 24;
//Here we need to split the inputed dates to convert them into standard format
//for furter execution
var x = ArrivalDtValue.split("/");
var y = DepartureDtValue.split("/");
//date format(Fullyear,month,date)
var ArrivalDate = new Date(x[2], (x[1] - 1), x[0]);
var DepartureDate = new Date(y[2], (y[1] - 1), y[0]);
var CurrentDate = new Date(year, month, day);
var month1 = x[1] - 1;
var month2 = y[1] - 1;

//Calculate difference between the two dates, and convert to days
var ArrivaldiffDays = Math.abs((CurrentDate.getTime() - ArrivalDate.getTime()) / (one_day));
var DeparturediffDays = Math.abs((CurrentDate.getTime() - DepartureDate.getTime()) / (one_day));


if (ArrDt > DeparDt) {
        alert("Departure date should not be less than Arrival date");
        return false;
}
else if (ArrivalDtValue == DepartureDtValue) {
      alert("Arrival date and Departure date should not be same");
      return false;
}
else if (ArrDt < CurrentDate)
{
      alert("Arrival date should not be less than current date");
      return false;
}
else if (DeparDt < CurrentDate)
{
     alert("Departure date should not be less than current date");
     return false;
}
else
{
     return true;
}
}


function getDateObject(dateString, dateSeperator) {
//This function return a date object after accepting
//a date string ans dateseparator as arguments
var curValue = dateString;
var sepChar = dateSeperator;
var curPos = 0;
var cDate, cMonth, cYear;

//extract day portion
curPos = dateString.indexOf(sepChar);
cDate = dateString.substring(0, curPos);

//extract month portion
endPos = dateString.indexOf(sepChar, curPos + 1);
cMonth = dateString.substring(curPos + 1, endPos);

//extract year portion
curPos = endPos;
endPos = curPos + 5;
cYear = curValue.substring(curPos + 1, endPos);

//Create Date Object
dtObject = new Date(cYear, cMonth - 1, cDate);
//alert(dtObject);
return dtObject;
}





...............................................................................................................
For the date validation (dd/mm/yyyy format) you can refer below site

http://challadotnetfaq.blogspot.com/2011/07/date-validation-using-javascript-in.html



Date validation using Javascript in asp.net

function validate() {
var oInput1 = document.getElementById("datepicker_Control");

if (isValidDate(oInput1))
{
//proceed code
}
else
{
// alert message
}

}



function isValidDate(field) {
var checkstr = "0123456789";
var DateField = field;
var Datevalue = "";
var DateTemp = "";
var seperator = ",";
var day;
var month;
var year;
var leap = 0;
var err = 0;
var i;
err = 0;
DateValue = DateField;
/* Delete all chars except 0..9 */
for (i = 0; i < DateValue.length; i++) { if (checkstr.indexOf(DateValue.substr(i, 1)) >= 0) {
DateTemp = DateTemp + DateValue.substr(i, 1);
}
}
DateValue = DateTemp;
/* Always change date to 8 digits - string*/
/* if year is entered as 2-digit / always assume 20xx */
if (DateValue.length == 6) {
DateValue = DateValue.substr(0, 4) + '20' + DateValue.substr(4, 2);
}
if (DateValue.length != 8) {
err = 19;
}
/* year is wrong if year = 0000 */
year = DateValue.substr(4, 4);
if (year == 0) {
err = 20;
}
/* Validation of month*/
month = DateValue.substr(2, 2);
if ((month < 1) || (month > 12)) {
err = 21;
}
/* Validation of day*/
day = DateValue.substr(0, 2);
if (day < 1) { err = 22; } /* Validation leap-year / february / day */ if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) { leap = 1; } if ((month == 2) && (leap == 1) && (day > 29)) {
err = 23;
}
if ((month == 2) && (leap != 1) && (day > 28)) {
err = 24;
}
/* Validation of other months */
if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {
err = 25;
}
if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) {
err = 26;
}
/* if 00 ist entered, no error, deleting the entry */
if ((day == 0) && (month == 0) && (year == 00)) {
err = 0; day = ""; month = ""; year = ""; seperator = "";
}
/* if no error, write the completed date to Input-Field (e.g. 13.12.2001) */
if (err == 0) {
DateField.value = day + seperator + month + seperator + year;
}
/* Error-message if err != 0 */
else {
return false;
}
return true;
}

Monday, July 18, 2011

Split function in sql server 2005/2008

 
GO

CREATE FUNCTION dbo.fnSplit(
    @sInputList VARCHAR(8000) -- List of delimited items
  , @sDelimiter VARCHAR(8000) = ',' -- delimiter that separates items
) RETURNS @List TABLE (item VARCHAR(8000))

BEGIN
DECLARE @sItem VARCHAR(8000)
WHILE CHARINDEX(@sDelimiter,@sInputList,0) <> 0
 BEGIN
 SELECT
  @sItem=RTRIM(LTRIM(SUBSTRING(@sInputList,1,CHARINDEX(@sDelimiter,@sInputList,0)-1))),
  @sInputList=RTRIM(LTRIM(SUBSTRING(@sInputList,CHARINDEX(@sDelimiter,@sInputList,0)+LEN(@sDelimiter),LEN(@sInputList))))

 IF LEN(@sItem) > 0
  INSERT INTO @List SELECT @sItem
 END

IF LEN(@sInputList) > 0
 INSERT INTO @List SELECT @sInputList -- Put the last item in
RETURN
END

GO

Example :

SELECT * from fnSplit('1,2,3,4,5',',')

Popular Posts