Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8820217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:33:18+00:00 2026-06-14T05:33:18+00:00

I am very new to C# and Sharepoint Programming. I am trying to learn

  • 0

I am very new to C# and Sharepoint Programming.

I am trying to learn about WebPart and C# used on it. I made a visual webpart which adds/deletes Items on a list. I have a Method which is called on a Button click which add Item in the List.

Here is my Method:

public void TestMethod()
{
    using (SPSite oSPSite = SPContext.Current.Site)
    {
        using (SPWeb ospweb = oSPSite.OpenWeb())
        {
            SPList lst = ospweb.Lists["CusomList1"];

            SPListItem item = lst.Items.Add();

            item["Item1"] = txt1.Text;
            item["Item2"] = txt3.Text;
            item["Item3"] = Convert.ToInt32(txt3.Text);
            item["Item4"] = txt4.Text;
            item.Update();
        }
    }            
}

This is called as:

protected void Button1_Click(object sender, EventArgs e)
{
    TestMethod();
}

This works Fine. I am trying to use the same method on a second WebPart which does the same thing (add item).

However when I added a new Visual Webpart on the same project and called the class and method as

protected void Button1_Click(object sender, EventArgs e)
{
    VWP1 NewClass = new VWP1();
    NewClass.TestMethod();
}

This Add button does not work and when I do a debug I get the following message:

Object reference not set to an instance of an object.

Can someone please tell me what should I do?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T05:33:19+00:00Added an answer on June 14, 2026 at 5:33 am

    What you need to do is separate out the logic of saving the item in the list from the logic of interacting with the user interface.

    Make a separate function that takes the data to be saved and saves it:

    public static void SaveItem(string item1, string item2, int item3, string item4)//TODO rename parameters
    {
        SPListItem newItem = SPContext.Current.Web.Lists["CusomList1"].AddItem();
        //set fields of new item
        newItem.Update();
    }
    

    Then you can put that method in a utility class somewhere.

    After you’ve done that you can call the method from each of the webparts:

    protected void Button1_Click(object sender, EventArgs e)
    {
        MyUtilityClass.SaveItem(txt1.Text, txt2.Text, Convert.ToInt32(txt3.Text), txt4.Text);
    }
    

    As for why, there are a number of things going on here. The main problem is that when you create a new instance of the first visual webpart and call the method it’s not accessing the textbox values of your second webpart, it’s accessing the textbox values of the newly created webpart; the one that hasn’t ever been shown the the user, or initialized by ASP. Because ASP (or you) never called it’s initialization functions the textbox field were all null, hence your error. If you did initialize it then they’d all have empty text values, and it still wouldn’t help you. The interaction with textboxes needs to happen within each of the different webparts; you can’t (or at the very least shouldn’t; it would be bad practice to allow it) access the internal controls from another class. What you can move to another class is everything besides the actual UI interaction; in this case the saving of the item to a list.

    Several side notes:

    • You put the current context’s SPSite in a using block; that will dispose of it. Don’t do that. Only dispose of Site/Web objects that you create. The current context’s site/web objects are re-used for different request; if you dispose of it that request will work, but when the disposed object is passed to the next request it will break, and it results in often hard to debug errors as the problem is in another request entirely.
    • You open up a new web in that example; are you sure that the current context’s web isn’t appropriate? If it’s not, and you really do want the root web and won’t always be at the root web, you can use SPContext.Current.Web.RootWeb to access it without opening up a new one.
    • You are using Convert.ToInt32 on a user provided value. This will break if they don’t enter a proper number, if they included commas, etc. Consider using int.TryParse so that you can more gracefully fail if they enter an invalid value.
    • You shouldn’t use list.Items.Add() to add an item; you should use list.AddItem(). Items.Add is considered deprecated.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Very new to PHP/programming in general, and I've been trying to run a PHP
Very new to working with Visual Basic / Excel. I am trying to write
I am basically a windows developer and very new to Sharepoint. I have Designed
Very new to FluentNHibernate, but I'm also excited about the area. I've recently started
Very new to JQuery and MVC and webdevelopment over all. I'm now trying to
Very new mobile developer here... I am trying to retrieve a list of tweets
Im very new in C++ I have found this post http://msdn.microsoft.com/en-us/magazine/cc163486.aspx and trying to
I'm very new to Sharepoint, I have an aspx page on a Sharepoint site
I'm very new to infopath & sharepoint. I have created a form in infopath
I am very new to Sharepoint world, and learning online and from some books

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.