Friday, September 11, 2009

Format Currency Textbox in US$ using AJAX in ASP.Net

The following code is used to format (display the currency value in US$ in textbox) the currency field using AJAX in asp.net

First include the AJAX Toolkit controls in your project and add the below code in your form (ASPX file)

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajax" %>

Next drag and drop the text box, script manager and FilteredTextBoxExtender

Add the below code




Display the currency(US$) format in textbox in code behind

TextBox1.Text = String.Format("{0:C2}", (ds.Tables[0].Rows[0]["Price"]));

The price will display as “$250,200,000.00”

At the time of inserting the Price in DB, remove the $ and , using the below function and insert into table

public string removeFormat(string amount)
{
string replace;
replace = amount.Replace("$", "");
replace = replace.Replace(",", "");
return replace;
}

Decimal price = Convert.ToDecimal(removeFormat(TextBox1.Text));

Read Excel File using DataReader in ASP.Net using C#.Net


The following code is used to read the excel sheet using data reader in asp.net,
            using System.Data.Common;
      public void ReadXLSFile()
      {
        string Field1, Field2;
        Field1 = "";
        Field2 = "";
        string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data  Source=Book1.xls;Extended Properties=""Excel 8.0;HDR=YES;""";
        
 DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb");
        using (DbConnection connection = factory.CreateConnection())
        {
            connection.ConnectionString = connectionString;
            using (DbCommand command = connection.CreateCommand())
            {
                command.CommandText = "SELECT Field1, Field2 FROM [Sheet1$]";
                connection.Open();
                using (DbDataReader dr = command.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        Field1 = dr["Field1"].ToString();
                        Field2 = dr["Field2"].ToString();
                    }
                }
            }
        }
    }

Thursday, September 10, 2009

Autoincrement Filed in Sql table with Initial Charecter

Here is the Function to increment the ID Field in Sql table with Initial Character

The below function will get the Tablename,Column name and Field Character(initial character) as a Parameter and return the Increment value with Field Character. Example to increment UserId, need to pass the table name, field name and character as "UI", then the function will return UI00001
public string Autoincrement(string TName, string CName, string FCode)
        {
            string Id = null;
            string strSql = null;
            string tmp = null;
            sqlconn = sqlConnection();
            strSql = "";
            strSql = "select max(cast(substring(" + CName + ",3,10)as integer))+1 from " + TName + "";
            SqlCommand SqlCmd = new SqlCommand(strSql, sqlconn);
            if (sqlconn.State == ConnectionState.Closed)
                sqlconn.Open();
            if (string.IsNullOrEmpty(SqlCmd.ExecuteScalar().ToString()))
            {
                Id = FCode + "00001";
                if (sqlconn.State == ConnectionState.Open)
                    sqlconn.Close();
                return Id;
            }
            strSql = "";
            strSql = SqlCmd.ExecuteScalar().ToString();
            if (strSql.Length == 1)
                tmp = FCode + "0000";
            else if (strSql.Length == 2)
                tmp = FCode + "000";
            else if (strSql.Length == 3)
                tmp = FCode + "00";
            else if (strSql.Length == 4)
                tmp = FCode + "0";
            else
                tmp = FCode;

            tmp = tmp + strSql;
            Id = tmp;
            if (sqlconn.State == ConnectionState.Open)
                sqlconn.Close();
            return Id;
        }

Call the above function as

string userid = Autoincrement("tblUser","UserId","UI");

Default Button in ASP.Net Form

One can set the default button at the form level meaning of all the buttons in the form the one that is set as default will be triggered whenever user presses Enter key.
To set the default button one has to do the following
<form id="index" runat="server" defaultbutton="btn1">
<asp:Button ID="btn1" runat="server" Text="Button"
    OnClick = "btn1_Click" />
<asp:Button ID="btn2" runat="server" Text="Button"
    OnClick = "btn2_Click" />
<asp:Button ID="btn3" runat="server" Text="Button"
    OnClick = "btn3_Click" />
<asp:TextBox ID="txt1" runat="server">asp:TextBox>
form>
Need to provide the ID of the Button to the defaultbutton property and it is set as default.
If you want to do it from code behind refer below (C#)
this.Form.DefaultButton = "btn1"; 
 To do the same things in Master Form, use the below code in code behind
this.Form.DefaultButton = btn1.UniqueID;
  

Prevent user from navigate to previous page using back button of the browser

Prevent user from navigate to previous page using back button of the browser or the back option in the context menu.
One cannot disable the browser back button functionality only thing that can be done is prevent them.
Below is the JavaScript snippets that needs to be placed in the head section of the page where you don’t want the user to revisit using the back button
 
Suppose there are two pages Page1.aspx and Page2.aspx and Page1.aspx redirects to Page2.aspx
Hence to prevent user from visiting Page1.aspx using Back Button you will need to place the script in the head section of Page1.aspx like in the (in above image).