Wednesday, January 25, 2012

How to Check/Get Session value in javascript

      We can access Session variable in Aspx page by using the following code block:
You need to follow the following steps:
1. You need to create one function in codebehind to check whether session value exists or not
      Example: CheckSessionExists() in TestPage.aspx.cs
2. You need to include the following javascript block inside the form/body tag.
3. You need to check whether Session exists or not by calling your codebehind function("Example:       CheckSessionExists() ")


TestPage.aspx:
==========
Please include the following javascript block inside the form tag

  <script language="javascript" type="text/javascript">

                <% if(CheckSessionExists()) { %>
                         var usrid =  "<%= Session["userid"].ToString() %>";
                      alert(usrid);

                    <% } %>
   </script>


TestPage.aspx.cs:
=============


        public bool CheckSessionExists()
        {

            if (Session["userid"] != null)
            {
                return true;
            }

            return false;
        }

Output:
=====




Common SQL Connections in App.Config file

Common SQL Connections in App.Config file
------------------------------------------------------
Both App.Config and Web.Config File:-
<configuration>
<connectionStrings>
    <add name="TestConnectionString" connectionString="Data Source=192.168.0.7;Initial                     Catalog=Test;User Id=sa; pwd=test@123" providerName="System.Data.SqlClient"/>
</connectionStrings>
<appSettings>
    <add key="DBString" value="Data Source=192.168.0.7; Initial Catalog=TEST; User Id=sa; pwd=test@123"/>
</appSettings>
</configuration>


In File(For App.Config)
``````````
SqlConnection sqlcon = new SqlConnection(ConfigurationSettings.AppSettings["DBString"])

In File(For Web.Config)
````````
SqlConnection sqlCon = new SqlConnection(ConfigurationManager.AppSettings["DBString"])

How to get TimeZones in C#

You can retrieve Timezones by using the following code blocks:

Getting Time Zone:

Page_Load:
=======
if (!IsPostBack)
{
   drpDwnTimeZone.DataSource = GetTimeZones();
   drpDwnTimeZone.DataTextField = "Name";
   drpDwnTimeZone.DataValueField = "ID";
   drpDwnTimeZone.DataBind();
   ShowCustomerProfile();
}
public Collection<MyStruct> GetTimeZones()
{
   var myClass = new Collection<MyStruct>();
   foreach (var timeZoneInfo in TimeZoneInfo.GetSystemTimeZones())
   {
      myClass.Add(new MyStruct { Name = timeZoneInfo.DisplayName, ID = timeZoneInfo.Id });
   }
   return myClass;
}
public struct MyStruct
{
   public string Name { get; set; }
   public string ID { get; set; }
Here "drpDwnTimeZone" is dropdownlist control


Author: ArunKumar 

Tuesday, January 24, 2012

How to Reset/Remove read only property values in C#


How to remove query string from the url.
Example: In this example Query String value is setting to the null then it is removed from the query string:
            PropertyInfo isreadonly = typeof(System.Collections.Specialized.NameValueCollection).GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic);
            // make collection editable
            if (HttpContext.Current.Request != null)
            {
                // remove
                isreadonly.SetValue(HttpContext.Current.Request.QueryString, false,             null);
                HttpContext.Current.Request.QueryString.Set(keyname,null);
                HttpContext.Current.Request.QueryString.Remove(keyname);
            }

Note: PropertyInfo class will exist under “System.Reflection” namespace

Could not load file or assembly 'AjaxControlToolkit' or one of its dependencies. The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))


Solution:This error occurred due to the improper shut down of the PC. The solution for this issue is to replace AjaxControlToolkit.dll in the Libraries folder of the IUSCRM project into the local AjaxControlToolKit.dll.
But in Net they posted lot of solutions, but that doesn’t work for me, but the above said solution if fine.

How to use Optional Parameter in C#


We can use optional parameter by the following  way:
Solution:
Syntax :
=====
We need to set "string Id=""" called as optional parameter
Here "string Id=" is the variable declaration
all the optional parameters should declare after required parameters

 example:
=============
private void GetSelectedItems(Label lbltest,string Id="")
{
    
}

//calling function using optional parameter
          GetSelectedItems(mylable,"lbltest");

//calling function without using optional parameter
 GetSelectedItems(mylable);

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


When we are entering  script tag(ex: <html>,<script>) values in the text box
A potentially dangerous Request.Form value was detected from the client (ctl00$ContentPlaceHolder1$txtRemarks="").
Solution: need to set  “requestValidationMode=2.0” which is exist under “httpRuntime”  under   <system.web> root node

Example:
           <system.web>

 <pages validateRequest="false">

    </pages>

//if your using .NET 4.0 set the following attribute
                <httpRuntime requestValidationMode="2.0"/>
            </system.web>

How to apply normal asp.net control style to Devexpress Controls


By default, most editors from the ASPxGridView and Editor Library are rendered using a 
specifically designed custom layout implemented with the help of standard HTML elements, such as HTML TABLEs, DIVs, SPANs, etc. This allows an editor's appearance to be fully customized by applying a specific style to each composite element of the editor.


 To apply normal asp.net control style to Devexpress control you need to set property called "Native=true" to the devexpress control


In some cases, the application logic might require representing editors as native HTML INPUT elements of the corresponding types. This demo illustrates how our editors (such as the ASPxTextBox, ASPxMemo, ASPxListBox, ASPxComboBox and ASPxButton) support native rendering. In order to use an editor in native mode, set the Native property to true. In native mode, an editor's render size may be significantly reduced, which improves the editor's overall performance.

Only the controls that can be rendered into a standard HTML element provide the Native property. These controls are:
  1. ASPxButton is rendered as <input type="button"/> or <input type="submit"/> (see input Object).
  2. ASPxTextBox is rendered as <input type="text"/> or <input type="password"/>.
  3. ASPxCheckBox is rendered as <input type="checkbox"/>.
  4. ASPxComboBox and ASPxListBox are rendered as <select /> (see select Object).
  5. ASPxMemo is rendered as <textarea />  
Example: 

 To apply normal asp.net control style to Devexpress Radio button list  need to use "Native=true" to the devexpress control

 <dxe:ASPxRadioButton runat="server" Native="true"></dxe:ASPxRadioButton>

 


Demo App:


    http://demos.devexpress.com/ASPxEditorsDemos/Features/NativeMode.aspx