0

I have a property in WCF Interface

string DBStatus(string Instance,string Asset,string Type);

[DataContract]
public class DatabaseDetails
{
    [DataMember]
    public string[] DBStatus { get; set; }
}

And in class implementing the following method

public string DBStatus(string Instance, string Asset, string Type)
{
    DatabaseDetails DbDetails = new DatabaseDetails();
    int DBStatus = 0;
    using (SqlConnection SqlConn = new SqlConnection())
    {
        try
        {
            //SqlConn.ConnectionString = "Data Source=" + ServerName + "Initial Catalog=" + DBName + "User id=" + UserId + "Password=" + Pwd;
            SqlConn.ConnectionString = "server=" + ServerName + ";database=" + DBName + ";UID=" + UserId + ";PWD=" + Pwd;
            SqlConn.Open();

            DbDetails.DBStatus[DBStatus] = "Online";
            DbDetails.DbInstance[DBStatus] = Instance;
            DbDetails.AssetName[DBStatus] = Asset;
            DBStatus++;
            //json = "Online";
        }
        catch (Exception e)
        {
            json = "Offline";
        }
    }

}

Getting Object reference error @ Line : DbDetails.DBStatus[DBStatus] = "Online"; I have initialised the object before using it. But not getting where am doing wrong.

John Saunders
  • 157,405
  • 24
  • 229
  • 388
Lalita
  • 133
  • 2
  • 4
  • 14
  • 2
    Is `DBStatus` a int variable? Have you defined it. Seriously your code is full of `DBStatus` **a variable, function, property**. Just a suggestion you need to look at variable name than using meaningless variable name – Satpal Dec 09 '13 at 08:40
  • DBStatus is a int variable its declared – Lalita Dec 09 '13 at 08:45
  • 1
    `I have initialised the object before using it.`, no you haven't. – Silvermind Dec 09 '13 at 08:45
  • here it is,Line Num 2:DatabaseDetails DbDetails = new DatabaseDetails(); @DBStatus(string Instance, string Asset, string Type) – Lalita Dec 09 '13 at 08:47
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Dec 09 '13 at 08:54

2 Answers2

0

I don't know why you need an array here but you have to initalize it before assigning values. You also have to state the array size. I'm assuming DBStatus is an integer. You should also initialize DbInstance and AssetName properties.

public string DBStatus(string Instance, string Asset, string Type)
{
    DatabaseDetails DbDetails = new DatabaseDetails 
                               { 
                                   DBStatus = new string[DBStatus + 1]
                                   //initialize DbInstance and AssetName too 
                               };

    using (SqlConnection SqlConn = new SqlConnection())
    {
        try
        {
            //SqlConn.ConnectionString = "Data Source=" + ServerName + "Initial Catalog=" + DBName + "User id=" + UserId + "Password=" + Pwd;
            SqlConn.ConnectionString = "server=" +ServerName+ ";database=" +DBName+ ";UID=" +UserId+ ";PWD="+Pwd ;
            SqlConn.Open();

            DbDetails.DBStatus[DBStatus] = "Online";
            DbDetails.DbInstance[DBStatus] = Instance;
            DbDetails.AssetName[DBStatus] = Asset;
            DBStatus++;
            //json = "Online";
        }
        catch (Exception e)
        {
            json = "Offline";
        }
    }

}
Ufuk Hacıoğulları
  • 36,026
  • 11
  • 106
  • 149
0

Though your code is very confusing, one guess is you need to initialize your string array.

DbDetails.DBStatus = new string[3];

DbDetails.DBStatus[DBStatus] = "Online";
DbDetails.DbInstance[DBStatus] = Instance;
DbDetails.AssetName[DBStatus] = Asset;
Ronak Patel
  • 2,430
  • 14
  • 19