Web service to return different column from different join tables as fields (output)

StackOverflow https://stackoverflow.com/questions/16469992

  •  21-04-2022
  •  | 
  •  

Pergunta

I am trying to create a simple web service which basically takes input parameter A and then according to that parameter it returns result (from query with couple of tables joins) fields from different tables, how to capture that output to use it in rules while binding it with one infopath form field?

My code is below... here am getting them as string but want them as different fields...

public class Data
{
    //Create new method to get data from ** database
    public static List<string> GetData(string ORDNUM_10)
      //public struct GetData(string ORDNUM_10)      
    {
        string PMDES1_01 = "";
        string DESCRPTN_104 = "";
        string PRTNUM_10 = "";
        string ORDREF_10 = "";
        string TNXDTE_01 = "";
        //Create connection
        SqlConnection con = new SqlConnection(@"Data Source=*****;Initial Catalog=EEE;Integrated Security=true;");

        //SQL Command
        SqlCommand cmd = new SqlCommand("SELECT DISTINCT Account_Types.DESCRPTN_104, Part_Master.PMDES1_01,Order_Master.PRTNUM_10,Order_Master.ORDNUM_10,Order_Master.ORDRef_10,Part_Master.TNXDTE_01 FROM.............. (EUM_10 = '"+ ORDNUM_10 + "'", con); 

        //Open connection
        con.Open();
        //to read from SQL Server
        SqlDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            PMDES1_01 = dr["PMDES1_01"].ToString();
            PRTNUM_10 = dr["PRTNUM_10"].ToString();
            DESCRPTN_104 = dr["DESCRPTN_104"].ToString();                
            ORDREF_10 = dr["ORDREF_10"].ToString();
            TNXDTE_01 = dr["TNXDTE_01"].ToString();            
        }
        
        //close connections
        dr.Close();
        con.Close();

        return new List<string> { PMDES1_01, PRTNUM_10, DESCRPTN_104, ORDEF_10 };
    }
}   

How do I get this string as fields so that I can use them in binding in infopath form field?

Foi útil?

Solução

In Visual Studio, click the File menu, then New and select Project. Then find ASP.NET Empty Web Application within Web. Give it a sensible name and then click OK. In the Solution Explorer, right-click the new project under the solution, click Add and select New Item and find Web Service within Web. Name it Data.asmx and click Add. You should then see the code behind your new web service Data class.

Now you can declare your own class to represent a database record, e.g. see class Record below. Then create instances of Record in your GetData() method by calling the Get*Type*() methods on the SqlDataReader. You'll need to look at the data types on your database columns and call the appropriate method e.g. GetString(), GetInt32() etc...

class Data : System.Web.Services.WebService
{
    public class Record
    {
        public string AccountType { get; set; }
        public string PartDescription { get; set; }
        public int PartNumber { get; set; }
        public string OrderRef { get; set; }
        public DateTime TransactionDate { get; set; }
    }

    [WebMethod]
    public static List<Record> GetData(string param)    
    {
        SqlConnection con = new SqlConnection(@"ConnectionString");
        con.Open();
        SqlCommand cmd = new SqlCommand("SELECT type,desc,num,ref,date FROM foo", con);
        SqlDataReader dr = cmd.ExecuteReader();

        List<Record> records = new List<Record>();

        while (dr.Read())
        {
            records.Add(new Record() {
                AccountType = dr.GetString(0),
                PartDescription = dr.GetString(1),
                PartNumber = dr.GetInt32(2),
                OrderRef = dr.GetString(3),
                TransactionDate = dr.GetDateTime(4)                
            });
        }

        dr.Close();
        con.Close();
        return records;
    }
}

Hint: it's better to add parameters to the query via the cmd.Parameters collection rather than using string concatenation.

Now you should be in a position to run or deploy your web service and get the URL of its WSDL. For an example of calling from InfoPath, there is a nice walk-through here.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top