How to bind the ListBox in silverlight3 with items from DB by calling a service which return values.
This is what i did and in the code behind called the Service and ListBox2.DataContext=Result of the Service.
Please help me out.
Thanks in advance.
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
vijay ragsPosted Sep 20, 2011, 6:04 AM
Priya LingePosted Sep 20, 2011, 5:57 AM
Service.svc.cs :
public class Service1
{
SqlConnection con = new SqlConnection("Data Source=TFServer;Initial Catalog=pub4;Persist Security Info=True;User ID=Trustfort;Password=Admin@123");
SqlDataReader dr;
SqlCommand cmd;
[OperationContract] Getcustomerdata() list = new List();
public List
{
List
con.Open();
string query = "select ID,Name from Customer";
cmd = new SqlCommand(query, con);
dr = cmd.ExecuteReader();
while (dr.Read())
{
var trn = new Customer
{
Cus_ID = Convert.ToInt32(dr[0]),
Cus_name = dr[1].ToString()
};
list.Add(trn);
}
return list;
}
// Add more operations here and mark them with [OperationContract]
}
Class Customer :
namespace listbindinsl3.Web
{
public class Customer
{
private string _name;
private Int32 _cusID;
public string Cus_name
{
get { return _name; }
set { _name = value; }
}
public Int32 Cus_ID
{
get { return _cusID; }
set { _cusID = value; }
}
}
MainPage.xaml :
MainPage.xaml.cs :
public MainPage()
{
InitializeComponent();
getdata();
}
public void getdata()(ser_GetcustomerdataCompleted);
{
ServiceReference1.Service1Client ser = new listbindinsl3.ServiceReference1.Service1Client();
ser.GetcustomerdataCompleted += new EventHandler
ser.GetcustomerdataAsync();
}
void ser_GetcustomerdataCompleted(object sender, listbindinsl3.ServiceReference1.GetcustomerdataCompletedEventArgs e)
{
ListBox2.ItemsSource = e.Result;
}
}
Hope this will help you.
Thanks.