Friday, April 8, 2011

Fetch data from database with Autocomplete Exender Textbox in AJAX

 This article explains to fill an AutoComplete Textbox from the data fetched from database.

Everyone knows about the AutoComplete Textbox. It is an ASP.NET AJAX extender that can be attached to any TextBox control. When the user types some letters in the Textbox, a popup panel will come to action and displayed the related words.So that the user can choose exact word from the popup panel. Here I tried to explain how this AutoComplete fetches data from the database through a Webservice.

Open Microsoft Visual Studio, click on New Website. Give website name as AutocompleteExenderSample.

Now drag and drop a Textbox from your Toolbox. Then drag and drop a ScriptManager and AutoCompleteExtender to your Default.aspx page. Then add a webservice to your project as WebService.asmx. First thing you have to do is to add the ScriptService reference to the webserive as follows.

/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
public class WebService : System.Web.Services.WebService {

    SqlConnection cn = new SqlConnection("Data Source=Local;Database=Northwind;trusted_connection=true");
    public WebService () {

        //Uncomment the following line if using designed components

    public string[] GetEmployeeName(string prefixText)
        //int count = 10;
        string sql;
        sql = "Select FirstName from Employees Where FirstName like '" + prefixText + "' + '%'";
        SqlDataAdapter da = new SqlDataAdapter(sql, cn);
        //da.SelectCommand.Parameters.Add("@prefixText", SqlDbType.VarChar, 50).Value = prefixText + "%";
        DataTable dt = new DataTable();
        string[] items = new string[dt.Rows.Count];
        int i = 0;
        foreach (DataRow dr in dt.Rows)
            items.SetValue(dr[0].ToString(), i);
        return items;

The above webmethod takes prefixText as argument, sends it to the query to fetch only the related words that starts with the prefixText values. Then it returns the result as an array of strings.

Next, in the Default.aspx page, set the AutoCompleteExtender’s TargetControlID property to the TextBox Id. Now you can see a new Extenders Tab is added in the Textbox’s Property window. Set ServicePath as WebService.asmx, ServiceMethod as
GetEmployeeName and MinimimPrefixLength as 1.
<asp:ScriptManager ID="ScriptManager1" runat="server">

    <asp:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server"
    MinimumPrefixLength="1" ServiceMethod="GetEmployeeName"
    ServicePath="~/WebService.asmx" TargetControlID="TextBox1"></asp:AutoCompleteExtender>
        <asp:TextBox ID="TextBox1" runat="server" Width="213px"></asp:TextBox>

Now, it’s time to run the project. Select the Default.aspx and click on View in Browser. You can see the excellent application starts to run. Type your Employee Name first letter. See all the Employee list starts with that letter will appear in the popup.

Tuesday, April 5, 2011

How to Encrypt and decrypt xml files in C#

In this post we are going to encrypt and decrypt xml files using RijndaelManaged class.
this class will be in System.Security.Cryptography namespace.
The System.Security.Cryptography namespace provides cryptographic services, including secure encoding and decoding of data.

    public static void EncryptAndSerialize(Object obj)
        UnicodeEncoding aUE = new UnicodeEncoding();
        byte[] key = aUE.GetBytes("password");
        RijndaelManaged RMCrypto = new RijndaelManaged();
        using (FileStream fs = File.Open(@"D:\Sample.xml", FileMode.Create))
        using (CryptoStream cs = new CryptoStream(fs, RMCrypto.CreateEncryptor(key,   key), CryptoStreamMode.Write))
                XmlSerializer xmlser = new XmlSerializer(obj.GetType());
                xmlser.Serialize(cs, obj);

public static DataSet DecryptAndDeserialize(string filename)
        DataSet ds = new DataSet();
        FileStream aFileStream = new FileStream(filename, FileMode.Open);
        StreamReader aStreamReader = new StreamReader(aFileStream);
        UnicodeEncoding aUE = new UnicodeEncoding();
        byte[] key = aUE.GetBytes("password");
        RijndaelManaged RMCrypto = new RijndaelManaged();
        CryptoStream aCryptoStream = new CryptoStream(aFileStream, RMCrypto.CreateDecryptor(key, key),     CryptoStreamMode.Read);

        //Restore the data set to memory.
       return ds;

For more info Refer following links