Pages

Friday, August 16, 2013

How to Convert String into MD5 Hash in Asp.net

This article will be explain how to convert a string to an MD5 Hash form. To convert string in MD5 hash form in Asp.net you have to use two namespace  'System.Text'  and  'System.Security.Cryptography'. Here is the complete code for convert simple sting in MD5 form.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string val = ConvertToMD5HASH(TextBox1.Text);
        Response.Write(val);
    }

    public string ConvertToMD5HASH(string text)
    {
        MD5 Secur = System.Security.Cryptography.MD5.Create();
        byte[] ByteText = System.Text.Encoding.ASCII.GetBytes(text);
        byte[] MdHash = Secur.ComputeHash(ByteText);

        StringBuilder Str = new StringBuilder();
        for (int i = 0; i < MdHash.Length; i++)
        {
            Str.Append(MdHash[i].ToString("X2"));
        }
        return Str.ToString();
    }


}

Wednesday, July 3, 2013

Dropdownlist Data Binding with Database Table in Asp.net

Introduction
This article will be explain how to bind database table data with Dropdownlist control in Asp.net. There are many way by which you can bind table data with Dropdownlist control.
Firstly go in Dropdownlist properties window and set properties 'True' of AutoPost Back other wise data will not be displayed in Dropdownlist.
Method 1
protected void Page_Load(object sender, EventArgs e)
    {
        SqlDataAdapter da;
        DataSet ds;
        da = new SqlDataAdapter("select * from Table", "Data Source=.;Initial Catalog=san;Integrated Security=True");
        ds = new DataSet();
        da.Fill(ds);
        DropDownList1.DataSource = ds.Tables[0];
        DropDownList1.DataTextField = "name";
        DropDownList1.DataValueField = "id";
        DropDownList1.DataBind();
    }
Method 2
public partial class _Default : System.Web.UI.Page
{
    SqlDataReader dr;
    SqlCommand cmd;
    SqlConnection con;
    protected void Page_Load(object sender, EventArgs e)
    {
        con = new SqlConnection("Data Source=.;Initial Catalog=san;Integrated Security=True");
        cmd = new SqlCommand("select * from Table");
        cmd.Connection = con;
        con.Open();
        dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            DropDownList1.Items.Add(new ListItem(dr.GetString(1), dr.GetString(0)));
        }
    }
}

How to Creat AutoComplete Textbox in Asp.net

Introduction
To create autocomplete textbox you in asp.net firstly you will have to add Ajex control with your visual studio toolbox. After add Ajax control follow this steps.
Step 1
Add ScriptManager and update panel Ajax control to your Default.aspx page.
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
          <ContentTemplate>
          
          </ContentTemplate>
        </asp:UpdatePanel>
 
    </div>
    </form>
</body>
Step 2
Now in this step add a textbox and AutoCompleteExtender in ContentTemplate of update panel. After add both control set textbox id in AutoCompleteExtender TargetControlID as the following.
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:TextBox runat="server" ID="TextBox1"
                style="top: 201px; left: 524px; position: absolute; height: 22px; width: 215px"></asp:TextBox>
            <asp:AutoCompleteExtender ID="TextBox1_AutoCompleteExtender" runat="server"
                DelimiterCharacters="" Enabled="True" ServiceMethod="GetCompletionList"
                ServicePath="" TargetControlID="TextBox1" UseContextKey="True" MinimumPrefixLength="1" CompletionListHighlightedItemCssClass="autocomplete_highlightedListItem"
                 CompletionListItemCssClass="autocomplete_listItem"        CompletionListCssClass="autocomplete_completionListElement">
            </asp:AutoCompleteExtender>
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>
    </form>
</body>
Step 3
Now go on Default.aspx.cs page and write following code.
public partial class AutoComplite : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    [System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
    public static string[] GetCompletionList(string prefixText, int count, string contextKey)
    {
        string s = prefixText + "%";
        List<string> str = new List<string>();
        SqlDataReader dr;
        SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=prabhakar;Integrated Security=True");
        SqlCommand cmd = new SqlCommand("select id from Table where id like '" + s + "'",con);
        con.Open();
        dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        while(dr.Read())
        {
            str.Add(dr["id"].ToString());
        }
        return str.ToArray();
    }
}
Step 4
Finally run your project you will see result.
Step 5
If you want to do this in disconnected mode then use following code.
public partial class AutoComplite : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    [System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
    public static string[] GetCompletionList(string prefixText, int count, string contextKey)
    {
        string s = prefixText + "%";
        List<string> str = new List<string>();
        SqlDataReader dr;
        SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=prabhakar;Integrated Security=True");
        SqlCommand cmd = new SqlCommand("select id from prabh where id like '" + s + "'",con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            str.Add(dr["id"].ToString());
        }
    }

}