Monday, February 22, 2010

Several Ways Redirect to Another Page

Hyperlinks

Performs new request on the target page.
Does not pass current page information to the target page.
Requires user initiation.
Redirects to any page, not just pages in the same Web application.
Enables you to share information between pages using query string or session
state.

Cross-page posting
Posts current page information to the target page.
Makes post information available in the target page.
Requires user initiation.
Redirects to any page, not just pages in the same Web application.
Enables the target page to read public properties of the source page if the pages are in the same Web application.
To pass current page information to the target page (as in multi-page forms).
When navigation should be under user control.

Browser redirect
Performs a new HTTP GET request on the target page.
Passes the query string (if any) to the target page. In Internet Explorer, the size of the query string is limited to 2,048 characters.
Provides programmatic and dynamic control over the target URL and query string.
Enables you to redirect to any page, not just pages in the same Web application.
Enables you to share information between source and target pages using session state.
For conditional navigation, when you want to control the target URL and control when navigation takes place. For example, use this option if the application must determine which page to navigate to based on data provided by the user.

Server transfer
Transfers control to a new page that renders in place of the source page.
Redirects only to target pages that are in the same Web application as the source page.
Enables you to read values and public properties from source page.
Does not update browser information with information about the target page. Pressing the refresh or back buttons in the browser can result in unexpected behavior.
For conditional navigation, when you want to control when navigation takes place and you want access to the context of the source page.
Best used in situations where the URL is hidden from the user.

Wednesday, February 10, 2010

Detect Javascript is enabled in ASPX programmatically

if (Session["Flag"] == null)
{
Session["Flag"] = "Checked";
string path = Request.Url + "?temp=1";
Page.ClientScript.RegisterStartupScript(this.GetType(), "redirect",
"window.location.href='" + path + "';", true);
}
if (Request.QueryString["temp"] == null)
Response.Write("JavaScript is not enabled.");
else
Response.Write("JavaScript is enabled.");

Monday, February 1, 2010

Could not find the visual SourceSafe internet web service connection information. SourceSafe web service cannot be accessed.

Could not find the visual SourceSafe internet web service connection information. SourceSafe web service cannot be accessed.


Solution:

Visual Studio -> Tools -> options -> Choose Source Control and Plug-in Selection.

Switch Back to Microsoft Visual SourceSafe instead of Microsoft Visual SourceSafe(Interenet);

Saturday, January 16, 2010

Multi Language Support


Use Global Resources for your page text and controls based on the user’s language. 


Basics
  1. A Resource file(.resx) stores values.
  2. Manually indicate that controls should use resources for their property value
  3. At run-time, the controls property values are derived from the resource file.

Steps

  1. In Solution Explorer, right click the root of your web site , Click Add ASP.NET Folder and then click App_GlobalResources..
             




  1. Right-Click the App_GlobalResources folder, and then click Add New Item
  2. Choose Resource templates , Name box , type MultiLanguage.resx and click Add.
  3. Open the MultiLanguage.resx file
  4. First row under the Name column type Login  and Value Column type Login
       6 . Create for resource file for Chinese and Malay language in the name of  MultiLanguage.zh-SG.resx  and MultiLanguage.ms-MY.resx
       7. Enter the all the corresponding values.







Now you have created three values for the resource. At runtime, ASP.Net will read the value based on what language user selected.

     8. Add Label Control to the page


1.    Default.aspx and switch to design mode, drag label control into the page
2.    Right-Click the Label control , click properties and then click the ellipsis(..)button in the expression box.The Expressions dialog box appears
3. In the Bindable Properties list, click Text.
4. In the Expression Type Lost, Select Resources.
5. Expression Properties, Set ClassKey to MultiLanguage and ResourceKey to Username , click Ok.
6. Assign for all Control.




Chinese Link Button Event



Session["Language"] = "zh-SG";
Response.Redirect("Default.aspx");



Malay Link Button Event



  Session["Language"] = "ms-MY";
  Response.Redirect("Default.aspx");



Add the below function in every page



protected override void InitializeCulture()
    {
String str = (String)Session["Language"];
        if (str != null)
        {
            String selectedLanguage = str;
            UICulture = selectedLanguage;
            Culture = selectedLanguage;

            Thread.CurrentThread.CurrentCulture =
                CultureInfo.CreateSpecificCulture(selectedLanguage);
            Thread.CurrentThread.CurrentUICulture = new
                CultureInfo(selectedLanguage);
        }
        else
        {
            base.InitializeCulture();
        }

}




Write the InitializeCulture function in separate file and call that file.

MultiLanguage.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Globalization;
using System.Threading;

///
/// Summary description for MultiLanguage
///
public class MultiLanguage : System.Web.UI.Page
{
      public MultiLanguage()
      {
            //
            // TODO: Add constructor logic here
            //
      }
  
    
    public void fnSupportMultiLanguage()
    {
        String str = (String)Session["Language"];
        if (str != null)
        {
            String selectedLanguage = str;
            UICulture = selectedLanguage;
            Culture = selectedLanguage;

            Thread.CurrentThread.CurrentCulture =
                CultureInfo.CreateSpecificCulture(selectedLanguage);
            Thread.CurrentThread.CurrentUICulture = new
                CultureInfo(selectedLanguage);
        }
        else
        {
            base.InitializeCulture();
        }
     
    }
   
}

In Aspx Page

protected override void InitializeCulture()
    {

        MultiLanguage ml = new MultiLanguage();
        ml.fnSupportMultiLanguage();
    }



 


Tuesday, December 22, 2009

How to add CheckedListBox Items Programmatically in windows application?

C#
         ArrayList US = new ArrayList();
            US.Add(new country("India", "Ind"));
            US.Add(new country("Austria", "Ans"));
            US.Add(new country("Malaysia", "Mal"));

            checkedListBox1.DataSource = US;
            checkedListBox1.DisplayMember = "County";
            checkedListBox1.ValueMember = "SCode";

   public class country
        {
            private string Contry;
            private string code;

            public country(string strLongName, string strShortName)
            {

                this.Contry = strShortName;
                this.code = strLongName;
            }

            public string SCode
            {
                get
                {
                    return Contry;
                }
            }

            public string County
            {

                get
                {
                    return code;
                }
            }

        }
 String str=checkedListBox1.SelectedValue.ToString();

Sunday, December 20, 2009

BusinessLayer and Presentation Layer - C#

Business Layer
using System;
using System.Data;
using System.Data.SqlClient;
using AcPro.DataLayer;

namespace AcPro.BusinessLayer
{
    ///
    /// Summary description for loging.
    ///

    public class login
    {
        public login()
        {
           
        }
        private string P_orgname;
        public string orgname
        {
            set
            {
                P_orgname=value;
            }
            get
            {
                return P_orgname;
            }
        }
        private string P_username;
        public string username
        {
            set
            {
                P_username=value;
            }
            get
            {
                return P_username;
            }
        }
        private string P_pwd;
        public string pwd
        {
            set
            {
                P_pwd=value;
             }
            get
            {
                return P_pwd;
            }
        }
        public DataSet Executesp()
        {
            string strsql="loginsuccess";
            SqlParameter[] prms;
            prms=new SqlParameter[3];
            prms[0]=new SqlParameter("@orgname",orgname);
            prms[1]=new SqlParameter("@username",username);
            prms[2]=new SqlParameter("@pwd",pwd);
            DataSet ds=DataAccess.ExecuteSql(strsql,prms);
            return ds;
        }
    }
}


Presentation Layer -C#


           Login is  Business Layer File Name
            login ln=new login();
            ln.orgname=Ddl_OrgName.SelectedItem.Text.Trim();
            Session["OrgName"]=Ddl_OrgName.SelectedItem.Text.Trim(); // For Display purpose -Muhil
            ln.username=txt_UserName.Text.Trim();
            Session["UserName"]=txt_UserName.Text.Trim(); //For Display purpose -Muhil
            ln.pwd=txt_Password.Text.Trim();
            DataSet ds=ln.Executesp();

DataLayer - C#


using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace AcPro.DataLayer
{
    ///
    /// Summary description for DataAccess.
    ///

   
    public class DataAccess
    {
        protected static SqlConnection Conn;
        protected static SqlTransaction sqlTrans;
        public DataAccess()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        public static void OpenConnection()
        {
            string strConnectionString=ConfigurationSettings.AppSettings["ConnectionString"];
            Conn=new SqlConnection();
            if(Conn.State==ConnectionState.Open)
            {
                Conn.Close();
            }
            else
            {
                Conn.ConnectionString=strConnectionString;
                Conn.Open();
            }
        }
        public static void CloseConnection()
        {
            if(Conn!=null)
            {
                if(Conn.State==ConnectionState.Open)
                {
                    Conn.Close();
                }
                Conn=null;
            }
        }
        public static void BeginTransaction()
        {
            OpenConnection();

            //Begin the transaction
            sqlTrans=Conn.BeginTransaction();
        }

        ///
        /// Commits the Transaction
        ///

        public static void CommitTransaction()
        {
            sqlTrans.Commit();
            sqlTrans.Dispose();
            CloseConnection();
        }

        ///
        /// Rolls Back the Transaction
        ///

        public static void RollBackTransaction()
        {
            sqlTrans.Rollback();
            sqlTrans.Dispose();
            CloseConnection();
        }
        public static int ExecuteNonQuery(string CommandText,SqlParameter[] Params)
        {
            int intRecordsAffected;
            OpenConnection();
            SqlCommand cmdObj=new SqlCommand();
            sqlTrans=Conn.BeginTransaction();
            cmdObj.CommandType=CommandType.StoredProcedure;
            cmdObj.Connection=Conn;
            cmdObj.CommandText=CommandText;
            cmdObj.Transaction=sqlTrans;
            if (Params.Length>0)
            {
                for (int i=0;i
                {
                    cmdObj.Parameters.Add(Params[i]);
                }
            }           
                try
                {
                    intRecordsAffected=cmdObj.ExecuteNonQuery();
                    sqlTrans.Commit();                   
               
                }
                catch(SqlException e)
                {
                    sqlTrans.Rollback();
                    throw e;
                }
           
            cmdObj.Dispose();
            CloseConnection();
            return intRecordsAffected;
        }
        public static int ExecutiveSclar(string CommandText, SqlParameter[] Params)
        {
            OpenConnection();
            SqlCommand cmdObj=new SqlCommand(CommandText,Conn);
            cmdObj.CommandType=CommandType.StoredProcedure;
            if(Params.Length>0)
            {
                for(int i=0;i
                {
                    cmdObj.Parameters.Add(Params[i]);
                }
            }
            int HeadPk=Convert.ToInt32(cmdObj.ExecuteScalar());
            cmdObj.Dispose();
            CloseConnection();
            return HeadPk;
        }
        ///
        ///  return values in dataset
        ///

        ///


        ///
        ///
        public static DataSet ExecuteSql(string CommandText, SqlParameter[] Params)
        {
            OpenConnection();
            SqlCommand cmdObj=new SqlCommand(CommandText,Conn);
            cmdObj.CommandType=CommandType.StoredProcedure;
            if(Params.Length>0)
            {
                for(int i=0;i
                {
                    cmdObj.Parameters.Add(Params[i]);
                }
            }
            SqlDataAdapter mySDA=new SqlDataAdapter(cmdObj);
            DataSet myDS=new DataSet();
            mySDA.Fill(myDS);
            //cmdObj.Dispose();
            CloseConnection();
            return myDS;

        }
        public static DataTable populateledger(String CommandText,SqlParameter[] Params)
              {
                 
                  OpenConnection();
                  SqlCommand cmdObj=new SqlCommand(CommandText,Conn);
                  cmdObj.CommandType=CommandType.StoredProcedure;      
                  if (Params.Length>0)
                  {
                      for (int i=0;i
                      {
                          cmdObj.Parameters.Add(Params[i]);
                      }
                  }
                  SqlDataAdapter mySDA=new SqlDataAdapter(cmdObj);
                  DataTable myDT=new DataTable();
                    myDT.Clear();
                  mySDA.Fill(myDT);   
                  mySDA.Dispose();
                 // cmdObj.Dispose();
            CloseConnection();
                  return myDT;
              }
        public static DataTable ExecuteTable(string strsql)
        {OpenConnection();
            DataTable dt=new DataTable();
            try
            {
                SqlDataAdapter da=new SqlDataAdapter(strsql,Conn);
                da.Fill(dt);
            }
            catch(Exception e)
            {
                throw e;
            }
            return dt;
        }
    }
}