1

My code is this :

SqlConnection scn = new SqlConnection(ClsPublic.GetConnectionString());
                   SqlCommand scm = new SqlCommand("SELECT Name FROM Table WHERE (Blogger = @Blogger)", scn);
                   scm.Parameters.AddWithValue("@Blogger", lblBloger.Text);
                   scn.Open();
                   MyLabel.Text = scm.ExecuteScalar().ToString();
                   scn.Close();

in this line :

lblLastNo.Text = scm.ExecuteScalar().ToString();

Has this error :

Object reference not set to an instance of an object.

or when I using if statement , shows same error

object Blogger= "";
if (Blogger.ToString() != string.Empty)
{
    ....
}

in below code again shows same error .

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Persian.
  • 1,035
  • 3
  • 16
  • 34

4 Answers4

3

Most likely scm.ExecuteScalar() is bringing you a null value. You need to test the value returned before using it:

var result = scm.ExecuteScalar();
MyLabel.Text = result == null ? '' : result.ToString();
Adriano Carneiro
  • 57,693
  • 12
  • 90
  • 123
1

scm.ExecuteScalar() is returning null, or scm is null, or lblLastNo is null. That's the only reason you get the error 'Object reference not set ..'.

Brian Mains
  • 50,520
  • 35
  • 148
  • 257
  • Sometimes returns Null and Sometimes returns a value .. when it is returning a value I have no problem but when it is returning null shows error .. – Persian. Oct 04 '11 at 15:04
  • @Persian. - So check if the value is **null** before you try to cast it. If the object is a null you can assign the value of your result string to **null** since a null object if it was nullable would return **null** of course this begs the question why not get the string value back instead? – Security Hound Oct 04 '11 at 15:11
1

ExecuteScalar return an Object type. This is why you have the same behavior on scm.ExecuteScalar().ToString(); or Blogger.ToString().

The object type default implementation of the ToString method returns the fully qualified name of the type of the Object, as the following example shows.

If this object is NULL, you will receive the error Object reference not set to an instance of an object.

Your second case with :

object blogger= "";
if (blogger.ToString() != string.Empty)
{
    ....
}

Should not throw an Exception but return a string that represents the object instance. For example : "YourNameSpace.Blogger"

Patrick Desjardins
  • 136,852
  • 88
  • 292
  • 341
-1

Please enclose with try catch block, so that you can handle runtime exceptions and its makes life easier to understand the problem. and for if (Blogger.ToString() != string.Empty) this u can check for Null condition or if its string. Then you can check for String.IsNullorEmpty

  string str=blogger.toString();
    if (String.IsNullOrEmpty(str)) 
            //do something
   else 
     //do other part

String Is Null or EMpty

Brian Mains
  • 50,520
  • 35
  • 148
  • 257
Ravi Gadag
  • 15,735
  • 5
  • 57
  • 83
  • Dear Ravi , when blogger is null , it can't convert blogger to string type and shows error .. in your first line of code has error – Persian. Oct 04 '11 at 15:09
  • i assumed that u already handled that null condition.. i mentioned for string.. any way will check for blogger object for null. thanks for finding mistake.. :) – Ravi Gadag Oct 04 '11 at 15:12