Monday, May 30, 2011

Insert date time from other culture to SQL Server us_english

Insert date time from other culture to SQL Server us_english
1) To know which language is in use. @@Language
     select

2) To know all languages
     sp_helplanguage
3) For set new language :
    SET

4) For set new dateformat.
    SET dateformat dmy

 Now set the format and

dateTime.ToString("MM/dd/yyyy", dateTimeFormat);
DateTimeFormatInfo dateTimeFormat = (new CultureInfo("??-??")).DateTimeFormat;
LANGUAGE us_english
http://www.sql-server-helper.com/tips/date-formats.aspx

2Nd:-
Set DD MM YYYY from C#
 string dateFormat = "dd-MM-yyyy";
 string Dtfrm = dateFrom.ToString(dateFormat);
Use in inline query with convert function 
 "date_from >= CONVERT(DATETIME,'" + Dtfrm + "',105) "

For example you can check following function in SQL and give correct result.
105

 select CONVERT(DATETIME,'18.08.2010',105)
 select CONVERT(DATETIME,'18-08-2010',105)
 select CONVERT(DATETIME,'18/08/2010',105)




Thanks
Mahesh

Passing multiple parameters to predicates

Step 1) Create a sample Class let say MyClass:
public class MyClass{
public int Age { get; set; }
public string TelephoneNumber { get; set; }
public string Name { get; set; }
}

Step 2) Create List of class and add some dummy values in list



 objMyClass = new MyClass();
objMyClass.Age = 39;
objMyClass.Name = "mahesh";
objMyClass.TelephoneNumber = "9876561364";
objListOfMyClass.Add(objMyClass);
objMyClass = new MyClass();";"";
objMyClass = new MyClass();
objMyClass = "nabhay";
objMyClass.Age = 22;
objMyClass.Name = "rakshit";
objMyClass.TelephoneNumber = ""
objListOfMyClass.Add(objMyClass);


objMyClass =
objMyClass.Age = 18;
objMyClass.Name =
objMyClass.TelephoneNumber =
objListOfMyClass.Add(objMyClass);

Step 3)
Create new class in which we will create property to set the number of arguments and value, In real project we will only using the set attribute of property.

public class MyListMatcher{
public int Age { get; set; }
public string TelephoneNumber { get; set; }
public string Name { get; set; }
 public bool Predicate(MyClass item)return ((item.Age == Age) && (item.TelephoneNumber == TelephoneNumber) && (item.Name == Name));}

Step 4) Now we have List and Predicated implemented logic so we have to just use it


objMyListMathcer.Name = "rakshit";
objMyListMathcer.Age = 18;
objMyListMathcer.TelephoneNumber = "987654321";
MyListMatcher objMyListMathcer = new MyListMatcher();
List<MyClass> objSelecteRes = objListOfMyClass.FindAll(objMyListMathcer.Predicate).ToList();

Thanks
Mahesh K. Sharms

Add a CalendarExtender to a GridView for month yearsetting (OnClientHidden OnClientShown)

If we apply modification to calendar control then need to set property for its method and we need to create behaviour id for each calendar. using thesse settings in desing time it is easy but if we add these controls into gridview / datalist etc than we have to struggle with which calendar invoked the request.

For this we have to follwoing steps
step 1)
Add name space
using AjaxControlToolkit;
Step 2) In row databound event  find the control and set its behaviour id wiht row index so each control has its own behaviour ID.
 CalendarExtender AjaxCalendarTextBoxInvalidDate_To = (CalendarExtender)e.Row.FindControl("AjaxCalendarTextBoxInvalidDate_To");
AjaxCalendarTextBoxInvalidDate_To.BehaviorID = "calendar1" + e.Row.RowIndex;

Step 3)
Use the follwing script. In which we declared calendarID as global and set its id ones in function and used in all three function.

<script language="javascript" type="text/javascript">
  var calendarID = '';
        function onCalendarShown(sender,e) {
calendarID = sender._id;
            var cal = $find(sender._id);
            //Setting the default mode to month
            cal._switchMode("months", true);
           
            //Iterate every month Item and attach click event to it
            if (cal._monthsBody) {
                for (var i = 0; i < cal._monthsBody.rows.length; i++) {
                    var row = cal._monthsBody.rows[i];
                    for (var j = 0; j < row.cells.length; j++) {
                        Sys.UI.DomEvent.addHandler(row.cells[j].firstChild, "click", call);
                    }
                }
            }
        }
       
        function onCalendarHidden(sender,e)
        {
      
        calendarID = sender._id;
           var cal = $find(sender._id);
            //Iterate every month Item and remove click event from it
              if (cal._monthsBody) {
                for (var i = 0; i < cal._monthsBody.rows.length; i++) {
                    var row = cal._monthsBody.rows[i];
                    for (var j = 0; j < row.cells.length; j++) {
                        Sys.UI.DomEvent.removeHandler(row.cells[j].firstChild,"click",call);
                    }
                }
            }
        }
       
        function call(eventElement)
        {
            var target = eventElement.target;
            switch (target.mode) {
            case "month":
                var cal = $find(calendarID);
                cal._visibleDate = target.date;
                cal.set_selectedDate(target.date);
                cal._switchMonth(target.date);
                cal._blur.post(true);
                cal.raiseDateSelectionChanged();
                break;
            }
            calendarID  = '';
        }

    </script>

4) If you need to show calendare only for specific row/condition than u can declare ajax calendar in side the label and hide / show label on condition


 <asp:Label Visible="false" ID="LableAjaxxToolKitCalendarContainer" runat="server" >
                                <ajax:CalendarExtender   ID="AjaxCalendarTextBoxInvalidDate_To" runat="server"
                                    Animated="False" FirstDayOfWeek="Monday" Format="MMM - yyyy" PopupButtonID="ImageAjaxCalendarTextBoxInvalidDate_To"
                                    TargetControlID="TextBoxInvalidDate_To" >
                                  
                                </ajax:CalendarExtender></asp:Label>

Thanks