How to do search functionality in Silverlight?


Hi
I hope that you are knowing the basic concept of silverlight. If not, then please check my previous post of silverlight.

Here i have used SL 4.0 and sql server 2008.This is the few steps to do this task.

Step1:Create on Silverlight Application.
Step2:Add Silverlight Enabled WCF in Asp.net project.
Step3: Create one “Emp” class like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Search_Functionality.Web
{
public class Emp
{
public int Id { get; set; }
public string EmpName { get; set; }
public string EmpSal { get; set; }

}
}

Step4: Write a search method on basis of EmpName in WCF like this

using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;

namespace Search_Functionality.Web
{
[ServiceContract(Namespace = “”)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1
{
SqlConnection con = new SqlConnection(“Data Source=.\\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True”);
[OperationContract]
public List GetEmpName(string EmpName)
{
List Emps = new List();

con.Open();
SqlCommand cmd = new SqlCommand(“Select Id,EmpName,EmpSal from tblEmp where EmpName=@EmpName”, con);
cmd.Parameters.AddWithValue(“@EmpName”, EmpName);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
Emp emp1 = new Emp();
emp1.Id = Convert.ToInt32(dr[“Id”]);
emp1.EmpName = dr[“EmpName”].ToString();
emp1.EmpSal = dr[“EmpSal”].ToString();
Emps.Add(emp1);
}
con.Close();
return Emps;
}

}
}

Step5: Compile the service and consume in silverlight application

Step6: You design the silverlight page is like this

<Grid x:Name="LayoutRoot" Background="White">
<TextBox Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
<Button Content="Search" Height="23" HorizontalAlignment="Left" Margin="152,12,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />

<sdk:DataGrid AutoGenerateColumns="True" Height="auto" Width="auto" HorizontalAlignment="Left" Margin="12,81,0,0" Name="dataGrid1" VerticalAlignment="Top" />

</Grid>

Step7:Write the C# code for search functionality like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Search_Functionality
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
//this is used for making visible false of DataGrid
dataGrid1.Visibility = Visibility.Collapsed;

}

protected void search(string a)
{
ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
client.GetEmpNameCompleted += new EventHandler(client_GetEmpNameCompleted);
client.GetEmpNameAsync(a);
}

void client_GetEmpNameCompleted(object sender, ServiceReference1.GetEmpNameCompletedEventArgs e)
{

if (e.Result.Count > 0)
{
dataGrid1.ItemsSource = e.Result;

}
else
{
dataGrid1.Visibility = Visibility.Collapsed;
dataGrid1.ItemsSource = null;
}

}
private void button1_Click(object sender, RoutedEventArgs e)
{
dataGrid1.Visibility = Visibility.Visible;
string a = textBox1.Text;
search(a);
}

}
}

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.