Link to home
Start Free TrialLog in
Avatar of fedders_world
fedders_world

asked on

Populating dropdownlists from a webservice - URENT 500 PNT

Hi in the passed i have populated dropdown lists by:

Dim connectionStringa As String = "Provider=SQLOLEDB; Data Source=#####; Initial Catalog=####; User ID=#### Password=####;"
        Dim fulluser As String = userstring

        Dim conna As New OleDb.OleDbConnection(connectionStringa)
        Dim strSQLa As String
        strSQLa = "SELECT usernumber, rssfeedurl, rssdfeedtitle FROM userrssfeed  WHERE usernumber= " & " '" & Label1.Text & "'" & ""
        Dim cmda As New OleDb.OleDbCommand(strSQLa, conna)
        Dim dataAdapter As New OleDb.OleDbDataAdapter(cmda)
        Dim dt As New DataTable
        conna.Open()
        dataAdapter.Fill(dt)
        conna.Close()
        DropDownList1.DataSource = dt

        DropDownList1.DataTextField = "rssdfeedtitle"
        DropDownList1.DataValueField = "rssfeedurl"
        DropDownList1.DataBind()
        DropDownList1.Items.Add("Select the RSS feed you wish to view from this list box")
        DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByText("Select the RSS feed you wish to view from this list box"))
        conna.Close()


Now i have 2 questions:

First of all how can i in a web service expose the results of a query like this:   strSQLa = "SELECT usernumber, rssfeedurl, rssdfeedtitle FROM userrssfeed  WHERE usernumber= " & " '" & Label1.Text & "'" & ""

secondly how can i populate a dropdown list using a web serivece, both the textfield and value f
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

You could just have your WebService return a DataTable which you can then bind to, and pass your Label1.Text as a parameter:

    Public Function GetRssDetails(UserNumber As String) As DataTable

        Dim conna As New OleDb.OleDbConnection(connectionStringa)
        Dim strSQLa As String
        strSQLa = "SELECT usernumber, rssfeedurl, rssdfeedtitle FROM userrssfeed  WHERE usernumber= " & " '" & UserNumber & "'"
        Dim cmda As New OleDb.OleDbCommand(strSQLa, conna)
        Dim dataAdapter As New OleDb.OleDbDataAdapter(cmda)
        Dim dt As New DataTable
        conna.Open()
        dataAdapter.Fill(dt)

        Return dt

    End Function

Then bind to it like:

    DropDownList1.DataSource = myWebService.GetRssDetails( Label1.Text )
    DropDownList1.DataTextField = "rssdfeedtitle"
    DropDownList1.DataValueField = "rssfeedurl"
    DropDownList1.DataBind()
Avatar of mmarinov
mmarinov

Hi fedders_world,

and another thing in addition to carl_tawn reply
if you have use your web service in .net 1.1 you can not pass datatable as result because it can not be serialized
in this scenario you will have to use dataset instead of datatable object and then you will have to modify the datasource line like
DropDownList1.DataSource = myWebService.GetRssDetails( Label1.Text ).Tables(0)

Cheers!
Avatar of fedders_world

ASKER

Thanks so far!!

i've tries what you said:

  Public Function get_user_feeds(ByVal session As String) As DataTable

        Dim uidapi As New user_api.user_account
        Dim getuid As String = uidapi.get_uidfrom_session(session)
        Dim strSQLpass As String
        Dim connectionStringpass As String = "Provider=SQLOLEDB; Data Source=###,4656; Initial Catalog=###; User ID=###; Password=###;"

        Dim conna As New OleDb.OleDbConnection(connectionStringpass)

        strSQLpass = "SELECT id, Uid, Ftitle FROM RSSflib_Table WHERE Uid= " & " '" & getuid & "'" & ""

        Dim cmda As New OleDb.OleDbCommand(strSQLpass, conna)
        Dim dataAdapter As New OleDb.OleDbDataAdapter(cmda)
        Dim dt As New DataTable
        conna.Open()
        dataAdapter.Fill(dt)

        Return dt


    End Function


ANYWAY, when i try that i get this error:
System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: Cannot serialize the DataTable. DataTable name is not set.
   at System.Data.DataTable.WriteXmlSchema(XmlWriter writer, Boolean writeHierarchy)
   at System.Data.DataTable.System.Xml.Serialization.IXmlSerializable.WriteXml(XmlWriter writer)
   at System.Xml.Serialization.XmlSerializationWriter.WriteSerializable(IXmlSerializable serializable, String name, String ns, Boolean isNullable, Boolean wrapped)
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write3_DataTable(Object o)
   at Microsoft.Xml.Serialization.GeneratedAssembly.DataTableSerializer.Serialize(Object objectToSerialize, XmlSerializationWriter writer)
   at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
   --- End of inner exception stack trace ---
   at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
   at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o)
   at System.Web.Services.Protocols.XmlReturnWriter.Write(HttpResponse response, Stream outputStream, Object returnValue)
   at System.Web.Services.Protocols.HttpServerProtocol.WriteReturns(Object[] returnValues, Stream outputStream)
   at System.Web.Services.Protocols.WebServiceHandler.WriteReturns(Object[] returnValues)
   at System.Web.Services.Protocols.WebServiceHandler.Invoke()


any idea why? Btw im using VS 2005, with VB, .NET framework 2 (Asp.net)

thanks again :)
Have you tried the dataset suggestion i have posted?
regards
im a little new to all this, how would i use a dataset?
thanks

feds
ASKER CERTIFIED SOLUTION
Avatar of mmarinov
mmarinov

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
HAH!!

It works, but i have not yet tried populating a dropdown box. Just a quick question before i do. What is the reson behind the '.Tables(0) in
DropDownList1.DataSource = myWebService.GetRssDetails( Label1.Text ).Tables(0)

feds
this gets the first table from the dataset object - it equal to myWebService.GetRssDetails( Label1.Text ) if the method returns datatable