?
Solved

Object Reference not set to an Instance of an Object

Posted on 2012-03-20
12
Medium Priority
?
275 Views
Last Modified: 2012-03-25
I am writing C# code for a Sharepoint solution and I thought I declared as an instance of an object and I still get the error.  Here is my code...I tend to get an error on the line where it has the code hillContentDB.Server.Name = "";....what am I doing wrong? :

 
                SPServer HillSPServer = new SPServer();

                HillSPServer.Name = dbserver;



                HillSpDatabase hillContentDB = new HillSpDatabase();

                hillContentDB.Server.Name = "";
                hillContentDB.Name = "";

                hillContentDB.Server.Name = HillSPServer.Name;
               
                hillContentDB.Name = dbname;
0
Comment
Question by:VBBRett
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
12 Comments
 
LVL 22

Expert Comment

by:p_davis
ID: 37744386
where does this get instantiated? most likely the name server property is null at that point.
0
 
LVL 22

Expert Comment

by:p_davis
ID: 37744388
by this i mean

hillContentDB...

sorry
0
 

Author Comment

by:VBBRett
ID: 37744429
how can I instantiate hillContentDB?  I made a derived class of a SPContentDB class from Sharepoint.  So, what I have is the following in the derived class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Administration;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Collections.Generic;
using System.Data;
using System.Security;
using Microsoft.SharePoint.Administration.AccessControl;

namespace SiteCreateVisualWebPartSolution.VisualWebPart1
{
    class HillSpDatabase: SPDatabase
    {
        static void Main()
        {
            SPDatabase dBase = new SPDatabase();

           

           
        }

         public void GrantAccess(System.Security.Principal.SecurityIdentifier securitykey)
        {
            base.GrantAccess(securitykey);
        }
    }
}
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37744545
Can't find any Server property in your HillSPDatabase class, is it a part of SPDatabase, if yes can we set empty string to Name as you have done...

where the code for Server property and its Name Property's Set Method and how does they behave....
0
 

Author Comment

by:VBBRett
ID: 37744557
HillSPDatabase class is derived from SPDatabase Sharepoint class.  The reason why I derived the SPDatabase class with HillSPDatabase class is because I wanted to be able to use the adduser protected method from SPDatabase class.  Are you wanting me to set the string name to empty in the HillSPDatabase class or in the code where I declare the HillSPDatabase variable as HillContentDB?
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37744594
you are using default constructor of SPDatabase. As your class contains no constructor, it will automatically call base class's Default constructor.

This doesn't set the Server(SPServer type) property.
You need to use the one with SPDatabaseParameters option.

See Here:-

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.administration.spdatabase.spdatabase.aspx
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.administration.spdatabaseparameters_members.aspx

Seems that The Server Property of SPDatabase is null, reason why it is giving you object reference not set.. error...
0
 

Author Comment

by:VBBRett
ID: 37744624
so what do I do?  Can you tell me where in my code I should change or correct?
0
 
LVL 20

Accepted Solution

by:
BuggyCoder earned 1500 total points
ID: 37744683
Refer to the links provided, you just need to call the other constructor and supply it required parameters..

SPDatabase db=new SPDatabase(SPDatabaseParameters.FromConnectionString("",SPDatabaseParameterOptions.None));

or

class HillSpDatabase: SPDatabase
{
public HillSpDatabase(string connectionString)
:base(SPDatabaseParameters.FromConnectionString(connectionString,SPDatabaseParameterOptions.None))
{
}
}

now you can do something like this:-
HillSpDatabase db=new HillSPDatabase("<connection_string>");


it should work ideally
0
 

Author Comment

by:VBBRett
ID: 37744702
I added the following to my base HillSpdatabase class:

 public HillSpDatabase();

        public HillSpDatabase(SPDatabaseParameters parameters);

        public HillSpDatabase(string name, SPDatabaseServiceInstance serviceInstance);

so now my class looks like the following...



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Administration;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Collections.Generic;
using System.Data;
using System.Security;
using Microsoft.SharePoint.Administration.AccessControl;

namespace SiteCreateVisualWebPartSolution.VisualWebPart1
{
    class HillSpDatabase: SPDatabase
    {
        static void Main()
        {
            SPDatabase dBase = new SPDatabase();

           
         
           
        }

        public HillSpDatabase();

        public HillSpDatabase(SPDatabaseParameters parameters);

        public HillSpDatabase(string name, SPDatabaseServiceInstance serviceInstance);
       
       
         public void GrantAccess(System.Security.Principal.SecurityIdentifier securitykey)
        {
            base.GrantAccess(securitykey);
        }
    }
}
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37744718
looks ok, just use constructors with parameters, supply the values and it will work fine, trust me....
0
 

Author Comment

by:VBBRett
ID: 37744745
I never worked with SPDatabaseParameters type before.  So say I want to pass the following parameters:

server name = srv1
datbase name = dbn1
userid = dbcreator
password = pass123!

How would I pass these parameters to the SPDatabaseParameters so that it is able to use the parameter?
0
 

Author Comment

by:VBBRett
ID: 37744856
How would I pass the parameter strings, I'm not sure how it works?
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month10 days, 12 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question