protected void Button1_Click(object sender, EventArgs e)
    {
        if (ddl.SelectedIndex >= 0)
        {
            localhost.Service serviceObj = new localhost.Service();
            //localhost.Table tableObj = listOfTables[ddl.SelectedValue];
            localhost.Field[] array = serviceObj.ReadField((int)Session["orgID"], Convert.ToInt32(ddl.SelectedValue));
            List<localhost.Field> values = new List<localhost.Field>(array);

            for (int i = 0; i < values.Count; i++)
            {
                TableRow row = new TableRow();
                TableCell cell1 = new TableCell();
                cell1.Text = values[i].FieldNameProperty.ToString();

                TableCell cell2 = new TableCell();
                cell2.Text = values[i].FieldDataType.ToString();
                row.Cells.Add(cell1);
                row.Cells.Add(cell2);
                Table1.Rows.Add(row);
            }
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        localhost.Service serviceObj = new localhost.Service();
        int orgID = (int)Session["orgID"];
        int objID = (int)Session["objID"];
        localhost.Field[] array = serviceObj.ReadField(orgID, objID);
        FieldList = new List<localhost.Field>(array);
        DropDownList1.DataSource = FieldList;
        DropDownList1.DataTextField = "FieldNameProperty";
        DropDownList1.DataValueField = "FieldIDProperty";
        DropDownList1.DataBind();

        Label4.Visible = false;
        Label3.Visible = false;
    }
    protected void ShwField_Click(object sender, EventArgs e)
    {
        listOfTables = (List<localhost.Table>)ViewState["listOfTables"];
        localhost.Table tableObj = listOfTables[ddl.SelectedIndex];

        localhost.Service serviceObj = new localhost.Service();
        localhost.Field[] array = serviceObj.ReadField((int)Session["orgID"], tableObj.ObjIDProperty);
        values = new List<localhost.Field>(array);
        DropDownList1.DataSource = values;
        DropDownList1.DataTextField = "FieldNameProperty";
        DropDownList1.DataValueField = "FieldIDProperty";
        DropDownList1.DataBind();
        ViewState["listOfValues"] = values;
    }
    void ddl_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddl.SelectedIndex > 0)
        {
            localhost.Table tableObj = listOfTables[ddl.SelectedIndex];

            localhost.Service serviceObj = new localhost.Service();
            localhost.Field[] array = serviceObj.ReadField((int)Session["orgID"], tableObj.ObjIDProperty);
            values = new List<localhost.Field>(array);
            DropDownList1.DataSource = values;
            DropDownList1.DataTextField = "FieldNameProperty";
            DropDownList1.DataValueField = "FieldIDProperty";
            DropDownList1.DataBind();
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        PreRender += new EventHandler(_Default_PreRender);

        if (!Page.IsPostBack)
        {
            int OrgID = (int)Session["orgID"];

            Label1.Visible = false;
            Label2.Visible = false;

            textboxes = new List<TextBox>();
            labels = new List<Label>();

            localhost.Service serviceObj = new localhost.Service();
            localhost.Table[] array = serviceObj.GetTables(OrgID);
            listOfTables = new List<localhost.Table>(array);
            ddl.DataSource = listOfTables;
            ddl.DataTextField = "TNameProperty";
            ddl.DataValueField = "ObjIDProperty";
            ddl.DataBind();

            List<localhost.Field> fieldlist = new List<localhost.Field>();
            if (ddl.SelectedIndex >= 0)
            {
                localhost.Table tableObj = listOfTables[ddl.SelectedIndex];
                localhost.Field[] fields = serviceObj.ReadField((int)Session["orgID"], tableObj.ObjIDProperty);
                fieldlist = new List<localhost.Field>(fields);
                int count = fieldlist.Count;
                Table methodtable = new Table();
                methodtable.Width = 500;
                methodtable.BorderStyle = BorderStyle.Solid;
                methodtable.BorderWidth = 5;
                methodtable.Caption = "Object Fields";
                methodtable.HorizontalAlign = HorizontalAlign.Center;
                for (int i = 0; i < count; i++)
                {
                    TableRow tr = new TableRow();

                    TableCell tc = new TableCell();
                    Label lb = new Label();
                    lb.ID = "lb" + i;
                    lb.EnableViewState = true;
                    lb.Text = fieldlist[i].FieldNameProperty + "::";
                    labels.Add(lb);
                    tc.Controls.Add(lb);

                    TableCell tc2 = new TableCell();
                    TextBox tb = new TextBox();
                    tb.ID = "tb" + i;
                    tb.EnableViewState = true;
                    tb.Text = Request.Form[tb.ClientID];
                    textboxes.Add(tb);
                    tc2.Controls.Add(tb);

                    tr.Controls.Add(tc);
                    tr.Controls.Add(tc2);

                    methodtable.Controls.Add(tr);

                }
                Panel1.Controls.Add(methodtable);
                Session["fieldlist"] = fieldlist;
            }
        }
    }
    private int CreateServices(string wsdl_text)
    {
        Type service;
        try
        {
            //Try Database Connection
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SAASDB"].ToString());
            try
            {
                conn.Open();
            }
            catch (Exception e)
            {
                //Return Error
                return -1;
            }

            localhost.Service serviceObj = new localhost.Service();

            //Insert into Data Table the services
            int count = 0;
            List<string> fieldnames = new List<string>();
            List<localhost.Field> fieldlist = new List<localhost.Field>();
            localhost.Field[] fields = serviceObj.ReadField(11, 30);
            fieldlist = new List<localhost.Field>(fields);
            foreach (localhost.Field item in fieldlist)
            {
                fieldnames.Add(count.ToString());
                count++;
            }

            Uri uri = new Uri(wsdl_text);
            WebRequest webRequest = WebRequest.Create(uri);
            System.IO.Stream requestStream = webRequest.GetResponse().GetResponseStream();

            // Get a WSDL file describing a service
            ServiceDescription sd = ServiceDescription.Read(requestStream);
            string sdName = sd.Services[0].Name;

            // Initialize a service description servImport
            ServiceDescriptionImporter servImport = new ServiceDescriptionImporter();
            servImport.AddServiceDescription(sd, String.Empty, String.Empty);
            servImport.ProtocolName = "Soap";
            servImport.CodeGenerationOptions = CodeGenerationOptions.GenerateProperties;

            CodeNamespace nameSpace = new CodeNamespace();
            CodeCompileUnit codeCompileUnit = new CodeCompileUnit();
            codeCompileUnit.Namespaces.Add(nameSpace);
            // Set Warnings
            ServiceDescriptionImportWarnings warnings = servImport.Import(nameSpace, codeCompileUnit);

            if (warnings == 0)
            {
                StringWriter stringWriter = new StringWriter(System.Globalization.CultureInfo.CurrentCulture);
                Microsoft.CSharp.CSharpCodeProvider prov = new Microsoft.CSharp.CSharpCodeProvider();
                prov.GenerateCodeFromNamespace(nameSpace, stringWriter, new CodeGeneratorOptions());

                // Compile the assembly with the appropriate references
                string[] assemblyReferences = new string[2] { "System.Web.Services.dll", "System.Xml.dll" };
                CompilerParameters param = new CompilerParameters(assemblyReferences);
                param.GenerateExecutable = false;
                param.GenerateInMemory = true;
                param.TreatWarningsAsErrors = false;
                param.WarningLevel = 4;

                CompilerResults results = new CompilerResults(new TempFileCollection());
                results = prov.CompileAssemblyFromDom(param, codeCompileUnit);
                Assembly assembly = results.CompiledAssembly;
                service = assembly.GetType(sdName);

                string input_params = "";
                string return_type = "";

                MethodInfo[] methodInfo = service.GetMethods();

                foreach (MethodInfo t in methodInfo)
                {
                    List<string> valueNames = new List<string>();
                    if (t.Name == "Discover")
                        break;

                    input_params = "";
                    return_type = "";

                    foreach (ParameterInfo parameter in t.GetParameters())
                    {
                        //paramname.Text = "(" + temp.ParameterType.Name + "  " + temp.Name + ")";
                        input_params = input_params + parameter.Name + ":" + parameter.ParameterType.Name + " ";
                    }

                    //Get The Return type of the Service(Method)
                    return_type = t.ReturnType.ToString();

                    //Insert into Service Object(Methods Table)
                    valueNames.Add(Session["OrgID"].ToString());
                    valueNames.Add(wsdl_text);
                    valueNames.Add(t.Name);
                    valueNames.Add(input_params);
                    valueNames.Add(return_type);
                    bool success = serviceObj.InsertData(11, 30, "WSDL-Instance", fieldnames.ToArray(), valueNames.ToArray());
                    if (success == false)
                    {
                        return -1;
                    }
                }

                return 1;
            }
            else
            {
                return -1;
            }
        }
        catch (Exception ex)
        {
            return -1;
        }
    }