Sunday, January 24, 2010

AutoComplete Textbox

Introduction:

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

Description:
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. Then choose ASP.NET Ajax Enabled Website and change the location to point your http://localhost/AutoComplete folder. Obviously, Default.aspx is added to your solution explorer.

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.


System.Web.Script.Services.ScriptService

[WebMethod]
public string[] GetCountryInfo(string prefixText)
{

int count = 10;
string sql = "Select * from Country Where Country_Name like @prefixText";
SqlDataAdapter da = new SqlDataAdapter(sql,"Your Connection String Comes Here"));
da.SelectCommand.Parameters.Add("@prefixText", SqlDbType.VarChar, 50).Value = prefixText+ "%";
DataTable dt = new DataTable();
da.Fill(dt);
string[] items = new string[dt.Rows.Count];
int i = 0;
foreach (DataRow dr in dt.Rows)
{
items.SetValue(dr["Country_Name"].ToString(),i);
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.



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 country's first letter. See all the countries starts with that letter will appear in the popup.

Thursday, January 21, 2010

SqlCacheDependency Caching in Detail

Caching with filtration:

private void Bind()
{
try
{
DataSet DS_district = new DataSet();
DataTable _table = new DataTable();
if (Cache["CachingName"] == null)
{
DS_district = Common.GetAllDistrict();
SqlCacheDependency dependency = new SqlCacheDependency ("DataBaseName", "TableName");
Cache.Insert("CachingName", DS_district, dependency, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(10));
_table = DS_district.Tables[0];
var query = from _district in _table.AsEnumerable()
where _district.Field("Id").Equals(Convert.ToInt64 (Dropdown2.SelectedValue))
select _district;

DataView _view = query.AsDataView();
Dropdown1.DataSource = _view;
Dropdown1.DataTextField = "Name";
Dropdown1.DataValueField = "Id";
Dropdown1.DataBind();
}
else
{
DS_district = (DataSet)Cache["CachingName"];
_table = DS_district.Tables[0];
var query = from _district in _table.AsEnumerable()
where _district.Field("Id").Equals(Convert.ToInt64 (Dropdown2.SelectedValue))
select _district;

DataView _view = query.AsDataView();
Dropdown1.DataSource = _view;
Dropdown1.DataTextField = "GeographicName";
Dropdown1.DataValueField = "GeographicId";
Dropdown1.DataBind();
}
}

catch (Exception ex)
{
lblError.Text = ex.Message.ToString();
}
}

Thursday, January 7, 2010

Implement Caching

In SQL Server DataBase
:

Run this command:

Exp:
aspnet_regsql.exe -S localhost -U sa -P sa -d NewDataBase -ed
This command Enable your Data Base, now You need to enable using table , for this command is...
aspnet_regsql.exe -S localhost -U sa -P sa -d NewDataBase -t tableName -et
After this go to SQL server , you get a new table "AspNet_SqlCacheTablesForChangeNotification".

coding on web.conf file
system.web
caching
sqlCacheDependency enabled="true"
databases
add name="NewTest" connectionStringName="Connection" pollTime="500"
/databases
/sqlCacheDependency
/caching
/system.web


Coding on .cs file
DataSet geography = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
try
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ConnectionString);
SqlDataAdapter adapter = new SqlDataAdapter("Select * from Users", conn);

adapter.Fill(geography);
SqlCacheDependency dependency = new SqlCacheDependency("NewTest", "Users"); Cache.Insert("Geo", geography, dependency,System.Web.Caching.Cache.NoAbsoluteExpiration,TimeSpan.FromMinutes(20));
GridView1.DataSource = geography;
GridView1.DataBind();
lblMessage.Text = "Categories retrieved from the Data Base";
}
catch { }
}
}
protected void Button1_Click(object sender, EventArgs e)
{
DataSet ds = (DataSet)Cache["Geo"]; lblMessage.Text = "Categories retrieved from the Cached data base";
GridView1.DataSource = ds;
GridView1.DataBind();
}