Flex and .Net Webservice Interaction

5 views
Skip to first unread message

sudhir

unread,
Jan 15, 2008, 11:51:02 PM1/15/08
to Flex India Community
Can anybody tell me how to consume an asp.net webservice which returns
a dataset. I am able to consume normal xml response but unable to
consume a dataset....also the CRUD wizard is unable to generate proper
classes for the wsdl i provide.

If anyone has already worked on consuming .net datasets from flex
please help...



Thanks in advance
sudheer

saurabh narula

unread,
Jan 16, 2008, 12:08:50 AM1/16/08
to flex_...@googlegroups.com
sudhir,
Flex would not understand .net datasets directly, try to return the data as xml formatted string or array of objects(create a class containg the field names) and consume the data in flex in the form of arraycollection/array/xmllist etc
Following are the examples of doing the same in .net

[WebMethod]
    public label_data[] fetch_country()
    {
        SqlConnection SCon = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings ["ConnectionStringLocal"]);
        SCon.Open();
        SqlCommand Cmd = new SqlCommand("Fetch_Country", SCon);
        Cmd.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter da = new SqlDataAdapter(Cmd);
        label_data[] cc1;
        DataSet ds = new DataSet();
        da.Fill(ds);
        int k = ds.Tables[0].Rows.Count + 1;
        cc1 = new label_data[k];
        int i;
        int t;
        cc1[0] = new label_data();
        cc1[0].label = "ALL";
        cc1[0].data = "0";
        for (i = 0; i < k - 1; i++)
        {
            cc1[i + 1] = new label_data();
            cc1[i + 1].label = (string)ds.Tables[0].Rows[i]["CNTRY_NM"];
            cc1[i + 1].data = (string)ds.Tables[0].Rows[i]["CNTRY_KEY"];
        }
        SCon.Close();
        return cc1;
    }
 
or

    [WebMethod]
    public string GetSalesRevenue(string sCustId, string sFromDate, string sToDate)
    {
        // Variable Declaration
        string sQuery = string.Empty;
        string sSBE = string.Empty;
        string sRev = string.Empty;
        string sTmp = string.Empty;
        string sDate = string.Empty;
        int flag = 0;

        // Object Declaration
        StringBuilder objSBCustXML;
        DataSet objDS = null;

        // Create a new Instance
        objSBCustXML = new StringBuilder();

        // Start building the xml
        objSBCustXML.Append("<root>");

        sFromDate = sFromDate.Substring(0, 2) + sFromDate.Substring(6, 4);
        sToDate = sToDate.Substring(0, 2) + sToDate.Substring(6, 4);


        try
        {
            // Build the Query
            sQuery = **some query**

            // Exceute the Query to get the DataSet
            objDS = this.ExecuteQuery (sQuery);

            // Check for the null condition and number of tables
            if (objDS != null && objDS.Tables.Count != 0)
            {
                // Check for number of rows in the tables
                if (objDS.Tables[0].Rows.Count != 0)
                {
                    // Create XML out of the table data
                    foreach (DataRow objDR in objDS.Tables[0].Rows)
                    {
                        if (flag == 0)
                        {
                            flag = 1;
                            continue;
                        }
                        // Get the Customer Id and Name
                        sRev = objDR.ItemArray[2].ToString();
                        sSBE = objDR.ItemArray[1].ToString();
                        sDate = objDR.ItemArray[0].ToString();
                        if (!sSBE.Equals( string.Empty) && sSBE != "")
                        {
                            sTmp = "<sales rev=\"" + sRev + "\" sbe=\"" + sSBE + "\" date=\"" + sDate + "\"/>";
                            objSBCustXML.Append(sTmp);
                        }
                    }
                }
            }
        }
        catch (Exception objExp)
        {
            // Do nothing for now
        }
        finally
        {
            objDS = null;
        }

        objSBCustXML.Append("</root>");

        return objSBCustXML.ToString();
    }

Flex code:-
private function csa_onResult(e:ResultEvent):void
               {
                   var st:String=e.result as String;
                var xml1:XMLList=new XMLList(st);    //with root
               
                var st1:String=st.slice(6,(st.length-7));//string without root tag                           
                var xml2:XMLList=new XMLList(st1);//xml without root tag                           

//other logic
          

}

-saurabh narula
http://saurabhnarula.wordpress.com

sudhir

unread,
Jan 16, 2008, 12:27:10 AM1/16/08
to Flex India Community
Thanks for your reply saurabh, the problem here is i ahev nno control
or access over the .net webservice.....i am supposed to consume it
thats all.....

Do you kow how to consume it in java??

Thanks
sudheer

On Jan 16, 10:08 am, "saurabh narula" <reachsaurabhnar...@gmail.com>
wrote:
> if (!sSBE.Equals(string.Empty) && sSBE != "")
> {
> sTmp = "<sales rev=\"" + sRev + "\" sbe=\"" +
> sSBE + "\" date=\"" + sDate + "\"/>";
> objSBCustXML.Append(sTmp);
> }
> }
> }
> }
> }
> catch (Exception objExp)
> {
> // Do nothing for now
> }
> finally
> {
> objDS = null;
> }
>
> objSBCustXML.Append("</root>");
>
> return objSBCustXML.ToString();
> }
>
> Flex code:-
> private function csa_onResult(e:ResultEvent):void
> {
> var st:String=e.result as String;
> var xml1:XMLList=new XMLList(st); //with root
>
> var st1:String=st.slice(6,(st.length-7));//string without
> root tag
> var xml2:XMLList=new XMLList(st1);//xml without root tag
>
> //other logic
>
> }
>
> -saurabh narulahttp://saurabhnarula.wordpress.com

saurabh narula

unread,
Jan 16, 2008, 2:45:24 AM1/16/08
to flex_...@googlegroups.com
sudhir,
i am afraid flex wont understand .net datasets  directly..you have to change the return type of .net web service..
also,if you know the data source of your .net web service then probably you can generate other set of web services in flex 2 or use the data application creation feature of flex 3 to create web services for you.

-saurabh narula
Reply all
Reply to author
Forward
0 new messages