This is a migrated thread and some comments may be shown as answers.

RadListBox and Sharepoint Code-Behind Project

10 Answers 141 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
John Esposito
Top achievements
Rank 1
John Esposito asked on 10 Feb 2010, 09:23 PM

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

10 Answers, 1 is accepted

Sort by
0
Sebastian
Telerik team
answered on 11 Feb 2010, 12:19 PM
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.
0
John Esposito
Top achievements
Rank 1
answered on 11 Feb 2010, 04:04 PM

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

0
John Esposito
Top achievements
Rank 1
answered on 12 Feb 2010, 04:43 PM

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

 

0
Veselin Vasilev
Telerik team
answered on 16 Feb 2010, 09:28 AM
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.
0
John Esposito
Top achievements
Rank 1
answered on 16 Feb 2010, 04:45 PM
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;  
           }  
 
    }  
}  
 
0
John Esposito
Top achievements
Rank 1
answered on 17 Feb 2010, 02:23 AM
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.

0
John Esposito
Top achievements
Rank 1
answered on 17 Feb 2010, 04:53 PM
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

0
Dimitar Milushev
Telerik team
answered on 19 Feb 2010, 02:44 PM
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.
0
John Esposito
Top achievements
Rank 1
answered on 19 Feb 2010, 04:08 PM

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

0
Dimitar Milushev
Telerik team
answered on 24 Feb 2010, 01:20 PM
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.
Tags
Ajax
Asked by
John Esposito
Top achievements
Rank 1
Answers by
Sebastian
Telerik team
John Esposito
Top achievements
Rank 1
Veselin Vasilev
Telerik team
Dimitar Milushev
Telerik team
Share this question
or