2

I created a web part and there are five configurations: in the usercontrol.ascx, I get the values by the following code:

namespace tasks_email.tasks_email_webpart
{
    public partial class tasks_email_webpartUserControl : UserControl
    {

        public tasks_email_webpart WebPart { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
        }
 protected void btn_send_id_Click(object sender, EventArgs e)
        {

            string subject = " ";
            subject = this.WebPart.SUBJECT as string;
            string emailAddrS = " ";
            emailAddrS = this.WebPart.EMAILADDR as string;
            string checkout_changes = " ";
            checkout_changes = this.WebPart.CHECKOUTLISTSNAMES as string;
            SendCheckout("bo.wang@us.bnpparibas.com", subject, "this is a project test");

But when I clicked the button, it said: null reference exception, the variable cannot get the SUBJECT value no matter what stuff I editted the configuration

Can any body tell me how to handle that?

*I checked the webpart.cs, I did write like:

    namespace tasks_email.tasks_email_webpart
{
    [ToolboxItemAttribute(false)]
    public class tasks_email_webpart : Microsoft.SharePoint.WebPartPages.WebPart
    {

        [WebBrowsable(true), Category("Configurations"), Personalizable(PersonalizationScope.Shared), WebDisplayName("Subject")]
        public string SUBJECT { get; set; }

in ascx I created a button with the method:

<asp:Button ID="btn_send_id" runat="server" Text=" Email_changes " 
onclick="btn_send_id_Click" CssClass="Search_Submit" onclientclick="return checkna()" 
    />

the error report:

[NullReferenceException: Object reference not set to an instance of an object.]
   tasks_email.tasks_email_webpart.tasks_email_webpartUserControl.btn_send_id_Click(Object sender, EventArgs e) +375
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +114
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +139
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +28
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2980
user1686630
  • 109
  • 1
  • 2
  • 7

2 Answers2

1

When you are developing SP2010 visual web parts you should always inherit from Microsoft.SharePoint.WebPartPages.WebPart, so your part would look something like this:

[ToolboxItemAttribute(false)]
public class MyWebPart : Microsoft.SharePoint.WebPartPages.WebPart
{

    [Personalizable(), WebBrowsable, Category("GPWF Settings")]
    public string WebClientUrl { get; set; }

}

Because you are inheriting from UserControl you always get null reference on properties.

So if you don't set your props first you should modify your code like this:

    string subject = this.WebPart.SUBJECT as string;
    string emailAddrS = this.WebPart.EMAILADDR as string;
    string checkout_changes = this.WebPart.CHECKOUTLISTSNAMES as string; 
Gregor Primar
  • 6,759
  • 2
  • 33
  • 46
  • 1
    Based on Davids comments, I believe he found the cause of your problem. You are probably not setting your props, rather just reading them first... I will update your code, that will get you over this problem. – Gregor Primar Oct 11 '12 at 18:30
  • Mn... still null reference, is there some info I forgot to post? – user1686630 Oct 11 '12 at 18:54
  • Could you post complete web part code? You can skip irelevant parts. – Gregor Primar Oct 11 '12 at 18:56
  • Hi Dear Gregor, I just edited my questions, see if you satisfied that – user1686630 Oct 11 '12 at 19:00
  • how I set the SUBJECT value: I went into the front end, pressed the edit button, choose the configuration and entered the Subject form – user1686630 Oct 11 '12 at 19:01
  • As I see your solution your web part prop is created inside class that inherits from WebPart and you want to access it inside usercontrol. You should place your prop inside usercontrol. Please see followink link for more details (I think this will crack your case): http://www.sharemuch.com/2010/05/25/referencing-web-part-properties-in-sharepoint-2010-visual-web-part/ – Gregor Primar Oct 11 '12 at 21:11
  • Morning Gregor, glad to see your help this morning, btw, do you know a better way to find out which line of code cause the error the moment I pressed my button? – user1686630 Oct 12 '12 at 14:13
  • on more question: SPList olist = osite.AllWebs["usitp"].Lists[checkoutlistname]; – user1686630 Oct 12 '12 at 14:31
  • I want to get the list, but why it throws another null reference ? – user1686630 Oct 12 '12 at 14:32
0

A better way would be to add SUBJECT, EMAILADDR and CHECKOUTLISTSNAMES as public properties in the usercontrol like this:

public string Subject {get;set;}

And in the web part, where you instantiate the user control, you write:

protected override void CreateChildControls()
{
    var control = (tasks_email_webpartUserControl) Page.LoadControl(_ascxPath);
    control.Subject = this.SUBJECT;
}

By doing this, you still set the subject as a web part property but you pass the data to the usercontrol instead of trying to get the data from the usercontrol.

Joakim
  • 114
  • 1
  • 7