RadListBox and Sharepoint Code-Behind Project

11 posts, 0 answers
  1. John Esposito
    John Esposito avatar
    11 posts
    Member since:
    Oct 2009

    Posted 10 Feb 2010 Link to this post

    Hello,

    I'm attemping to populate a RadListBox with data from a SQL Server database. I've never done this in a Sharepoint environment so I tried it first in a straight ASP.NET project that I created with Visual Studio 2008. I copied the code example provided in the documentation almost to the letter and it all worked perfectly. The same code will not work in the Sharepoint project.
     

    I'm creating the solution with Visual Studio 2008 and Visual Studio Extensions for Windows Sharepoint Services (VSeWSS). I'm putting the code to retrieve the data and populate the RadListBox in a C# code-behind file.
     

    At least one difference between the Sharepoint and ASP.NET programs is that I can run the ASP.NET code through the local development server to test it, something that I cannot do with Sharepoint (I must build and deploy it to the Sharepoint server when I want to test the code).

    Based on this I thought the problem might lie in the web.config file. I tried adding the System.Data assembly as a safe control, but that didn't work. Unfortunately it seems that I cannot debug a Sharepoint app as I can any other Visual Studio project so I cannot even provide a descriptive error message, only a web page that won't load.

    Any suggestions or ideas would be greatly appreciated.

    Thank you

  2. Sebastian
    Admin
    Sebastian avatar
    9934 posts

    Posted 11 Feb 2010 Link to this post

    Hi John,

    General instructions how to integrate and deploy our controls in Sharepoint environment are available under the "Integrating RadControls in MOSS" chapter from the online documentation:

    http://www.telerik.com/help/aspnet-ajax/moss-overview.html

    Please examine and configure your RadListBox instance accordingly (and add the Telerik.Web.UI and Telerik.Web.Design assemblies as safe control in your web.config) and let us know if you need further directions/assistance.

    Best regards,
    Sebastian
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. John Esposito
    John Esposito avatar
    11 posts
    Member since:
    Oct 2009

    Posted 11 Feb 2010 Link to this post

    Hello Sebastian and thank you for responding so quickly to my question. I suppose it's obvious that I do not have much experience with web development, Visual Studio, or C#. I did follow the instructions for integrating RadControls in MOSS, but I will go through them again to make sure I didn't miss anything.

    After posting my question I tried putting the code inline as opposed to in a code-behind file and it worked perfectly. Unfortunately I need the flexibility that using C# provides, so strictly programming inline is not an option for me.

    I'm trying to figure out how to capture any exceptions that are thrown, but in the mean time here is the code I'm using (by the way, I switched from a listbox to a combobox - same problem though):

    using System;  
    using System.Web;  
    using System.Web.UI.WebControls;  
    using Telerik.Web.UI;  
    using Microsoft.SharePoint;  
    using System.Data;  
    using System.Data.SqlClient;  
     
    namespace SPTelerikTest  
    {  
        public partial class SPTelerikTest : System.Web.UI.Page  
        {  
            protected Telerik.Web.UI.RadComboBox RadComboBox1;  
     
     
            protected void Page_Load(object sender, EventArgs e)  
            {  
               if (!Page.IsPostBack)  
                {  
                    BindToDataTable(RadComboBox1);  
                }   
            }  
     
            protected void Page_Init()  
            {  
            }  
     
            private void BindToDataTable(RadComboBox combo)  
               {  
                   SqlConnection conn = new SqlConnection("connection string");  
                   SqlCommand cmd = new SqlCommand("SELECT Location FROM Locations", conn);  
                  
                   SqlDataAdapter adapter = new SqlDataAdapter(cmd);  
     
                   DataSet locations = new DataSet();  
     
     
                   try 
                   {  
                       adapter.Fill(locations, "Locations");  /** code seems to fail here **/ 
                       combo.DataTextField = "Location";  
                       combo.DataValueField = "Location";  
                       combo.DataSource = locations;  
                       combo.DataBind();     
                   }  
                   catch (Exception ex)  
                   {  
                     return;  
                   }  
               }  
        }  
    }  
     
     

     

    Thanks

  5. John Esposito
    John Esposito avatar
    11 posts
    Member since:
    Oct 2009

    Posted 12 Feb 2010 Link to this post

    I've almost resolved this issue.I managed to display the exception that was being thrown and it was this:

    Request for the permission of type 'System.Data.SqlClient.SqlClientPermission, System.Data,Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'failed.

    With some research I found the following solution (Sharepoint developers please file this away in your notes):

    Cause:

    Anything that access database from SP requires at least the WSS_Medium security policy in the web.config file. If you receive a security message from the web part, it's usually the trust element in the web.config file.

    Fix:

    Open wss_mediumtrust.config & wss_minimaltrust.config usually (C:\Program Files\Common Files\Microsoft Shared\Web Server tensions\12\config\) look in your web.config file for the exact path.

    Find in wss_mediumtrust.config: <SecurityClass Name="SqlClientPermission" Description="System.Data.SqlClient.SqlClientPermission, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>

    Copy and paste it in to the <SecurityClasses> node of wss_minimaltrust.config. In the PermissionSet section of this configuration file,

    add the following:  Find in wss_mediumtrust.config: <IPermission class="SqlClientPermission" version="1" Unrestricted="true"/>

    Copy and paste it in to the a <PermissionSet> node of wss_minimaltrust.config.

    That about covers it.

    At this point I can make my code work with a basic ASP.NET DropDownList but not with a RadComboBox, which throws the following exception around the combo.DataTextField = "Location"  (from code snippet above) line:

    Object reference not set to an instance of object

     

  6. Veselin Vasilev
    Admin
    Veselin Vasilev avatar
    2992 posts

    Posted 16 Feb 2010 Link to this post

    Hi John Esposito,

    Can you try with a DataTable and see if its filled by the adapter?

    DataTable locations = new DataTable();
     
    try
    {
        adapter.Fill(locations);
        combo.DataTextField = "Location";
        combo.DataValueField = "Location";
        combo.DataSource = locations;
        combo.DataBind(); 
    }


    Regards,
    Veskoni
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  7. John Esposito
    John Esposito avatar
    11 posts
    Member since:
    Oct 2009

    Posted 16 Feb 2010 Link to this post

    As you suggested, I've tried using Datatable() vs. Dataset(), but still get the same results. I've included my code and attached an image of the result. I've put a RadComboBox and a DropDownList side by side and execute the exact same code for each. The DropDownList is populated with data, but the RadComboBox returns "Object reference not set to an instance of an object.".  

    using System;  
    using System.Web;  
    using System.Web.UI.WebControls;  
    using Telerik.Web.UI;  
    using Microsoft.SharePoint;  
    using System.Data;  
    using System.Data.SqlClient;  
    using Microsoft.SqlServer.MessageBox;  
     
    namespace SPTelerikTest  
    {  
        public partial class SPTelerikTest : System.Web.UI.Page  
        {  
            protected TextBox TextBox1;  
            protected TextBox TextBox2;  
            protected DropDownList DropDownList1;  
            protected RadComboBox RadComboBox1;  
     
            protected void Page_Load(object sender, EventArgs e)  
            {  
     
               if (!Page.IsPostBack)  
                {  
                    BindToDataTable(DropDownList1);  
                    BindToDataTable2(RadComboBox1);  
                }   
            }   
     
            protected void Page_Init()  
            {  
            }  
     
            private void BindToDataTable(DropDownList combo)  
            {  
                string str = "All is well!";  
     
                SqlConnection conn = new SqlConnection("connection string");  
                SqlCommand cmd = new SqlCommand("SELECT Location FROM Locations", conn);  
     
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);  
     
                /** DataSet locations = new DataSet(); **/ 
                DataTable locations = new DataTable();  
     
                try 
                {  
                    /** adapter.Fill(locations, "Locations"); **/ 
                    adapter.Fill(locations);  
                    combo.DataTextField = "Location";  
                    combo.DataValueField = "Location";  
                    combo.DataSource = locations;  
                    combo.DataBind();  
                }  
                catch (Exception ex)  
                {  
                    str = ex.Message;  
                }  
                TextBox1.Text = str;  
            }   
     
            private void BindToDataTable2(RadComboBox combo)   
               {  
                   string str = "All is well!";      
     
                   SqlConnection conn = new SqlConnection("connection string");  
                   SqlCommand cmd = new SqlCommand("SELECT Location FROM Locations", conn);  
                  
                   SqlDataAdapter adapter = new SqlDataAdapter(cmd);  
     
                   /** DataSet locations = new DataSet(); **/ 
                   DataTable locations = new DataTable();  
     
                   try 
                   {  
                       /** adapter.Fill(locations, "Locations"); **/ 
                       adapter.Fill(locations);  
                       combo.DataTextField = "Location";  
                       combo.DataValueField = "Location";  
                       combo.DataSource = locations;  
                       combo.DataBind();     
                   }  
                   catch (Exception ex)  
                   {  
                       str = ex.Message;  
                   }  
                   TextBox2.Text = str;  
               }  
     
        }  
    }  
     
  8. John Esposito
    John Esposito avatar
    11 posts
    Member since:
    Oct 2009

    Posted 16 Feb 2010 Link to this post

    Update: I managed to clear the "Object reference not set to an instance of an object" exception by inserting a RadComboBox1 = new RadComboBox() statement. Now my code doesn't throw any exceptions, but I still can't get the RadComboBox to display anything. I've determined that the RadComboBox control does successfully bind to the datasource by assigning its items to textboxes and successfully displaying those: 

     

    BindToDataTable2(RadComboBox1);  
     
    TextBox1.Text = RadComboBox1.Items[0].Value;  
    TextBox2.Text = RadComboBox1.Items[1].Value;  
    TextBox3.Text = RadComboBox1.Items[2].Value; 

    So the code is working properly, but the items are not being displayed when the page loads.

  9. John Esposito
    John Esposito avatar
    11 posts
    Member since:
    Oct 2009

    Posted 17 Feb 2010 Link to this post

    Just to summarize and restate my problem: The code in the code-behind file is working properly but there seems to be a disconnect between it and the .aspx page when it is loaded (only as far as Rad controls are concerned - the others I've been using for comparison work OK). I've tried explicitly declaring event handlers in the following manner:

    RadComboBox1.Load +=  new EventHandler(RadComboBox1_Load);  
     

     


    Experimenting with these has shown that none of the Rad control related events are being triggered at all. Again, the issue is only with the code-behind. If, for example, I use inline code to bind a Rad control to a data source, it seems to work fine. I need to be able to use code-behind for my project, though.

    Any advice would be greatly appreciated. 

    Thanks

  10. Dimitar Milushev
    Admin
    Dimitar Milushev avatar
    555 posts

    Posted 19 Feb 2010 Link to this post

    Hello John Esposito,

    If the RadComboBox1 variable is not instantiated (the "Object reference not set to an instance of an object" exception) it means that the code generated when parsing the .aspx file does not contain a RadComboBox1 control. By using 'RadComboBox1 = new RadComboBox()' you create a new instance, but this instance is never inserted in the Page's Control collection.

    Can you please check if the RadComboBox on the page has its ID set to RadComboBox1? Also, can you please send us the .aspx file besides the code-behind so we can check if there are any problems there?

    Best wishes,
    Dimitar Milushev
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  11. John Esposito
    John Esposito avatar
    11 posts
    Member since:
    Oct 2009

    Posted 19 Feb 2010 Link to this post

    Hello Dimitar,

    The fact that the control names might not match occurred to me also and I did verify that they were all identical; the problem persists though.

     

    You are correct to focus on the html vs. the C# code. Some posts I came across from people with similar problems prompted me to review the code in the .aspx file.  Though I did not find anything obvious I decided to start a new project and paid close attention to my html every step of the way.

     

    The Rad controls in the new project are working as they should, but I still can’t tell you what the problem was or why only Rad controls were affected. I’m just happy to be able to move forward with my project.

     

    Thanks to the team for your attention and feedback. I appreciate it.

    John

  12. Dimitar Milushev
    Admin
    Dimitar Milushev avatar
    555 posts

    Posted 24 Feb 2010 Link to this post

    Hello John,

    I'm glad that the issue is resolved, one way or another and you can continue. Feel free to contact us again if you have any questions.

    Kind regards,
    Dimitar Milushev
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017