6
Answers

How do i retrieve data from database using webclient and display in listbox?

Sarah

Sarah

13y
1.9k
1
Hi people, may i know how can i retrieve data from database(linq to sql) using webclient and display in listbox? I realied most of the articles talk about creating xml file and use XDocument or XElement. Is there any possibility that i can consume the web service and retrieve the data?

Urgent. People please help me :(
Answers (6)
0
Sarah
NA 8 3.9k 13y
No error but the data doesn't display in the listbox. :( sorry for the trouble. i'm not good in codings.
0
Prabhu Raja
NA 4.7k 1.5m 13y
Hope this will work.

foreach(char str in e.result)
{
   listBox1.Items.Add(str.ToString());
}



0
Sarah
NA 8 3.9k 13y
Hi Pravin,
             the codes doesnt work well.
 
error for foreach: Cannot convert type 'char' to 'string'.


error for ListBox.Add(str); : Error 1 'System.Windows.Controls.ListBox' does not contain a definition for 'Add' and no extension method 'Add' accepting a first argument of type 'System.Windows.Controls.ListBox' could be found (are you missing a using directive or an assembly reference?)

0
Pravin More
NA 2.6k 136.1k 13y
hi sarah ,
                ok then you can add it by foreach.......

try like this...

foreach(string str in e.result)
{
   listBox1.Add(str);
}

note:- i hope you can do any typecast or to list if required.

let me know if works or not.

Thanks,
Pravin.
0
Sarah
NA 8 3.9k 13y
Hi Pravin, i have alrdy done that in my web service. The problem is i'm using web client method so i think i'm supposed to use the XDocument to help retrieve the data to the listbox successfully. i cant seems to retrieve the data from database :(

this is how my codings in xaml.cs look like:

public Details
{
InitializeComponent();
WebClient proxy = new WebClient();
            string baseUri = "http://localhost:1723/ServiceImpl.svc/GetDetails";
            proxy.DownloadStringCompleted += new DownloadStringCompletedEventHandler(proxy_DownloadStringCompleted);
            proxy.DownloadStringAsync(new Uri(baseUri));
}

void proxy_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
       {
           if (e.Error != null)
           { return; }
           if (e.Result == null)
            MessageBox.Show("NUlllllllllll");

           XDocument xmlCurrent = XDocument.Parse(e.Result);


           ListBox.DataContext = xmlCurrent;

       }
0
Pravin More
NA 2.6k 136.1k 13y
Hi Sarah,

               you can return List<string> from your web service and bind it to listbox.

                 i.e. return type of service will be List<string>