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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Priya LingeRank #785 · 2.9K reputationPosted Sep 20, 2011, 1:57 AM EDT
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.
vijay rags58 reputationPosted Sep 20, 2011, 2:04 AM EDT