воскресенье, 11 сентября 2011 г.

Add items to dropdown in a Infopath with sql data


First add a field, check "Repeating" checkbox and select this field in the drop down control from "Look up values in the form's data source".
Then using the following code, add the data.



 private string getConnectionString(SPWebApplication myWebApplication,string typeBD)
        {
            string ConnectionString = string.Empty;
            if (myWebApplication.Properties != null && myWebApplication.Properties.Count > 0 && myWebApplication.Properties.ContainsKey(typeBD))
            {
                ConnectionString = GetString(myWebApplication.Properties[typeBD]);
            }
            return ConnectionString;
        }
        private string GetString(object obj)
        {
            string str = "";
            if (obj != null)
                str = obj.ToString();
            return str;
        }

        public void FormEvents_Loading(object sender, LoadingEventArgs e)
        {
             string connection = "";
             using (SPSite site = new SPSite(this.ServerInfo.SharePointSiteUrl.ToString()))
             {
                 connection = getConnectionString(site.WebApplication, "Akcent");
                 string query = "";
                 query = @"SELECT
                                 [id]
                                ,[guid]
                                ,[code]
                                ,[caption]
                                ,[type_id]
                            FROM [Budget].[SACG].[v_ref_cfo_src]";
                 int field_sql = 3;//поле отображения
                 SetDataToDropDown(connection, query, "ValuesField", field_sql);
}
}

This method get the field with data and set you values!

private void SetDataToDropDown(string connection,string queryString,string fieldName,int field_sql)
        {
            DataSet dsLoc = GetDataFromSql(connection,queryString);
            XPathNavigator DOMMain = DataSources[""].CreateNavigator();
            XPathNavigator Loc = DOMMain.SelectSingleNode("/my:myFields/my:"+fieldName, this.NamespaceManager);
            Loc.SetValue("");
            XPathNavigator templateNode = Loc.Clone();
            foreach (DataRow dr in dsLoc.Tables[0].Rows)
            {
                XPathNavigator newNode = templateNode.Clone();
                string value = dr[field_sql].ToString();
                newNode.SetValue(value);
                Loc.InsertAfter(newNode);
            }
            Loc.DeleteSelf();
        }
        private DataSet GetDataFromSql(string connection_string, string queryString)
        {
            DataSet dsLoc = new DataSet();
            SPSecurity.RunWithElevatedPrivileges(delegate()
           {
               using (SqlConnection connection = new SqlConnection(connection_string))
               {
                   connection.Open();
                   SqlCommand command = new SqlCommand(
                       queryString, connection);
                   SqlDataAdapter sqlData = new SqlDataAdapter(command);
                   sqlData.Fill(dsLoc);
                   connection.Close();
               }
           });
            return dsLoc;
        }

It's all!

Комментариев нет:

Отправить комментарий