facade pattern

Facade pattern is one of the Structural Pattern.
Main purpose of the facade pattern is to encapsulate whole subsystem under one class or in simple words one class represents the whole subsystem.
Client only talks to the facade class and not to other classes which are inside the system.
This pattern make complex code very easy.



Following diagram shows how the facade pattern works.
Main class only talks to the facade class which in turns talks to other class in the subsystem.
Example:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
facade fd = new facade();
fd.methodfacade();
Console.ReadLine();
}
}
class subclass1
{
public void method1()
{
Console.WriteLine("Subclass1");
}
}

class subclass2
{
public void method2()
{
Console.WriteLine("Subclass2");
}
}

class subclass3
{
public void method3()
{
Console.WriteLine("Subclass3");
}
}

class facade
{
private subclass1 one = new subclass1();
private subclass2 two = new subclass2();
private subclass3 three = new subclass3();
public void methodfacade()
{
one.method1();
two.method2();
three.method3();
}
}
}

Using AutoCompleteExtender of ajax in asp.net or how to use autocomplete property of ajax toolkit ?

In this tutorial i will show how to use AutoCompleteExtender of Ajax tool kit in asp.net

Steps are as follows:

  1. Add scriptmanger.
  2. Add AutocompleteExtender.
     <asp:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server"
TargetControlID="TextBox1" ServiceMethod="GetCompletionList"
UseContextKey="True" MinimumPrefixLength="1">
asp:AutoCompleteExtender>

this will have TargetControlID as id of textbox.
MinimumPrefixlength is length of the text in textbox after which autoCompleteExtender
start showing suggestions.

  3. Add one textbox.

<asp:TextBox ID="TextBox1" runat="server">asp:TextBox>

4. Now go to design mode & Click on Add AutoComplete Page Method.




5. After this you will see that some code is added to ur code behind file ,
just replace it with this code
(here i have bounded the colum from database for which i want suggestions ,
you should change this with your database teble & column)
(Here i have used Name Column of CountryRegion of Adventureworks Database )
[System.Web.Services.WebMethodAttribute(),
System.Web.Script.Services.ScriptMethodAttribute()]
public
static string[] GetCompletionList
(string prefixText, int count, string contextKey)
{
SqlConnection conn;
SqlCommand cmd;
string cmdString =
"Select Name from Person.CountryRegion WHERE Name LIKE '" +
prefixText + "%'";
conn = new
SqlConnection(@"Data Source=VRAKSH-VAIBHAVS;Initial Catalog=AdventureWorks;
User ID=sa;Password=data#123"
);
// Put this string on one line in your code
cmd = new SqlCommand(cmdString, conn);
conn.Open();
SqlDataReader myReader;
List<string> returnData = new List<string>();
myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
while (myReader.Read())
{
returnData.Add(myReader["Name"].ToString());
}
return returnData.ToArray();
}

Just save & run ....

type just one character in your textbox & wait for 2-3 seconds.....
u will find suggestion box is shown under it.

Here is Full code.....


Source code......


<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
asp:ScriptManager>
<asp:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server"
TargetControlID="TextBox1" ServiceMethod="GetCompletionList"
UseContextKey="True" MinimumPrefixLength="1">
asp:AutoCompleteExtender>
<asp:TextBox ID="TextBox1" runat="server">asp:TextBox>
div>


code behind....

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)
{
SqlConnection conn;
SqlCommand cmd;
string cmdString =
"Select Name from Person.CountryRegion WHERE Name LIKE '" +
prefixText + "%'";
conn = new
SqlConnection(@"Data Source=VRAKSH-VAIBHAVS;Initial Catalog=AdventureWorks;
User ID=sa;Password=data#123"
);
// Put this string on one line in your code
cmd = new SqlCommand(cmdString, conn);
conn.Open();
SqlDataReader myReader;
List<string> returnData = new List<string>();
myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
while (myReader.Read())
{
returnData.Add(myReader["Name"].ToString());
}
return returnData.ToArray();
}

Exporting data from GridView to pdf file in asp.net or how to export data from gridview to pdf in c#?

<!--[if gte mso 9]> Normal 0 false false false EN-IN X-NONE X-NONE MicrosoftInternetExplorer4

In this demo tutorial I will show you how to export data from gridview to pdf file.

Steps are as follows:

1. Create new web page.

2. Drag & drop gridview from toolbox & bind your database (don’t use enable sorting).

<asp:GridView ID="GridView2" runat="server">
        asp:GridView>

3. Drag & drop button.

  <asp:Button ID="Button1" runat="server" Text="Button"
onclick="Button1_Click" />

4. You need to download one dll file named iTextSharp.dll.

5. Now add reference of this downloaded dll.

6. Now go to code behind & some of the following codes.

using iTextSharp.text;

using iTextSharp.text.pdf;

using iTextSharp.text.html.simpleparser;


7. Now add following code in code behind.

public override void VerifyRenderingInServerForm(Control control)

{

/* Verifies that the control is rendered */

}

protected void Button1_Click(object sender, EventArgs e)

{

Response.ContentType = "application/pdf";

Response.AddHeader("content0disposition",

"attachment;filename=UserDetails.pdf");

Response.Cache.SetCacheability(HttpCacheability.NoCache);

StringWriter sw = new StringWriter();

HtmlTextWriter hw = new HtmlTextWriter(sw);

GridView1.AllowPaging = false;

GridView1.DataBind();

GridView1.RenderControl(hw);

GridView1.HeaderRow.Style.Add("width", "15%");

GridView1.HeaderRow.Style.Add("font-size", "10px");

GridView1.Style.Add("text-decoration", "none");

GridView1.Style.Add("font-family", "Arial, Helvetica, sans-serif");

GridView1.Style.Add("font-size", "8px");

StringReader sr = new StringReader(sw.ToString());

StreamReader reader = new StreamReader(new MemoryStream(Encoding.ASCII.Ge tBytes(sw.ToString())));

Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);

HTMLWorker htmlparser = new HTMLWorker(pdfDoc);

PdfWriter.GetInstance(pdfDoc, Response.OutputStream);

pdfDoc.Open();

htmlparser.Parse(reader);

pdfDoc.Close();

Response.Write(pdfDoc);

Response.End();

}

Now on button click , whole data from your gridview will be transferred in pdf file .




Using UpdateProgress of ajax in asp.net

In this post i will show how to use UpdateProgresss extension of ajax in asp.net

We do see in many websites that when ever somthing is getting loaded at that time some message is shown

like “Please wait .....” or “Loading....”

Or some gif images

This can be done by using UpdateProgresss of Ajax in ASP.NET easily.....

Let me show you one small example.....

1. Create new asp.net page.

2. Go to source code.

3. Add ScriptManager ......(without it your ajax will never work...)

<asp:ScriptManager ID="ScriptManager1" runat="server" />


4. Add one UpdatePanel & put code someting like this ...

<asp:UpdateProgress runat="server" id="PageUpdateProgress">
<ProgressTemplate>
Loading....
</ ProgressTemplate>
</ asp:UpdateProgress>


5. Now Add UpdateProgress Extension & write something like this ....

<asp:UpdatePanel runat="server" id="Panel">
<ContentTemplate>
<asp:Button runat="server" id="UpdateButton"
onclick="UpdateButton_Click"
text="Update" style="height: 26px" />
</ContentTemplate>
</asp:UpdatePanel>


6. Now go to code behind & write this code....

protected void UpdateButton_Click(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(5000);
}

7. Now just save it & run it....

8. Click on button..... You will find “Loading....” at top of button.

You can put anything for showing instead of “Loading....”

i.e. You can put any image like there are in many wesites....

Here is full code....

Source code...

<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdateProgress runat="server" id="PageUpdateProgress">
<ProgressTemplate>
Loading....
</ProgressTemplate>
</asp:UpdateProgress>
<asp:UpdatePanel runat="server" id="Panel">
<ContentTemplate>
<asp:Button runat="server" id="UpdateButton"
onclick="UpdateButton_Click"
text="Update" style="height: 26px" />
</ContentTemplate>
</asp:UpdatePanel>

Code Behind...

protected void UpdateButton_Click(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(5000);
}



Download Demo

value.Toint32() like value.ToString()

Hello here i will show you how can u create ur own function to convert data from string to int ....

We will do value.ToInt32() to convert string to Int.....just like we do value.ToSting() to convert to string........

public static class ConvertToInt
{
public static int Toint32(this string value)
{
return int.Parse(value);
}
}


after this u can use value.Toint32() insted of Convert.Toint32(value) ...
just like u use .ToString() for conversion

How to Delete files from folder?

Here is code ...........

DirectoryInfo di = new System.IO.DirectoryInfo("DirecotyPath");
FileInfo[] allFiles = di.GetFiles(searchPattern);
foreach (FileInfo file in allFiles)
{
file
.Delete();
}

How to get data from database into xml ?

It is very easy code....
You just have to use WriteXml() function

here is sample code.........

public DataSet GetLocationByMandate()

{

new SqlConnection("Data Source=.;Initial Catalog=login;Integrated Security=True");

DataSet Ds = new DataSet();

SqlDataAdapter Da = new SqlDataAdapter("your quary",connection);

Da.Fill(Ds);

Ds.WriteXml("locations1.xml");

}


Export xml data into text file

If you want to export attributes from XML file to text file then you just have to read that attribute from xml & write it in text file...I have shown hoe to write string into text file Here ....

Here is full code....Here i have used other way to write string into text file ie. WriteAllText function...

private void button1_Click(object sender, EventArgs e)
{

string text = select();

File.WriteAllText("file.txt", text);

}
public string select()
{
XmlDocument doc = new XmlDocument();
string s = "";
doc.Load("XMLFile1.xml");
XmlNodeList nodes = doc.SelectNodes("/root/row");
foreach (XmlNode node in nodes)
{
XmlAttributeCollection att = node.Attributes;
foreach (XmlAttribute att1 in att)
{
s = s + att1.InnerText + System.Environment.NewLine;

}
}
return s;
}


How to write a string in to text file ?


First You have to check whether file exists or not ...if yes then just map the path & write string into file using streamwriter...

if
(File.Exists(Server.MapPath("logfile.txt")))

{
FileStream aFile = new FileStream(Server.MapPath("logfile.txt"),FileMode.Append,FileAccess.Write);
StreamWriter sw = new StreamWriter(aFile);
sw.WriteLine("test to log file.");
sw.Close();

}