8 Replies - 5172 Views - Last Post: 29 October 2014 - 02:39 PM Rate Topic: -----

#1 ari03   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 17-November 12

Data is Null. This method or property cannot be called on Null values?

Posted 29 October 2014 - 01:10 PM

hi everyone ,,
i have linked textebox with column and it work well but when those any column have null i got error message[Data is Null. This method or property cannot be called on Null values]
,
however i tried to know how to use *IsDBNull* that they said it will ignore error but i couln't use it so please help :)
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                SqlConnection cn = new SqlConnection("Data Source=PC-mypc\\SQLEXPRESS;Initial Catalog=ID_card;Integrated Security=True");
                SqlCommand cmd = new SqlCommand("select * from tbladd where name='" + comboBox1.Text + "'", cn);
                SqlDataReader dr;
                cn.Open();
                dr = cmd.ExecuteReader();

                while (dr.Read())
                {
                    int smd = dr.GetInt32(dr.GetOrdinal("id"));
                    string smd1 = dr.GetString(dr.GetOrdinal("name"));

                    string smd2 = dr.GetString(dr.GetOrdinal("place"));
                    string smd3 = dr.GetString(dr.GetOrdinal("class"));

                    textBox1.Text = smd.ToString();
                    textBox4.Text = smd3;

                    textBox3.Text = smd2;
                    textBox2.Text = smd1;

                }
                while (dr.Read())
                {


                }
                cn.Close();
            }
            catch (Exception er)
            {
                MessageBox.Show(er.Message);
            }
            }



Is This A Good Question/Topic? 0
  • +

Replies To: Data is Null. This method or property cannot be called on Null values?

#2 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: Data is Null. This method or property cannot be called on Null values?

Posted 29 October 2014 - 01:32 PM

Please show your attempt to use IsDBNull.
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: Data is Null. This method or property cannot be called on Null values?

Posted 29 October 2014 - 01:37 PM

There is an example of its use with the docs:

SqlDataReader.IsDBNull Method
Was This Post Helpful? 0
  • +
  • -

#4 ari03   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 17-November 12

Re: Data is Null. This method or property cannot be called on Null values?

Posted 29 October 2014 - 01:41 PM

i use it like this

if (!dr.IsDBNull(dr.GetOrdinal("cn"))) ;

i use it like this

if (!dr.IsDBNull(dr.GetOrdinal("cn"))) ;
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: Data is Null. This method or property cannot be called on Null values?

Posted 29 October 2014 - 01:50 PM

You have an empty if-statement there, if the value is DBNull.. it doesn't do anything.

This is from the example I referenced:
if (!reader.IsDBNull(reader.GetOrdinal("MiddleName")))
               Console.Write(" {0}", reader.GetString(reader.GetOrdinal("MiddleName")))

This says ifthe MiddeName value is not null, then write it to the console.

What are you planning to do if "cn" is null? Once you've decided you can finish your if-statement by carrying out your decision.

if (!dr.IsDBNull(dr.GetOrdinal("cn"))) ;

This also presumes that "cn" is a field of your table, is it just a coincidence that this is also the name of your connection object?
Was This Post Helpful? 0
  • +
  • -

#6 ari03   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 17-November 12

Re: Data is Null. This method or property cannot be called on Null values?

Posted 29 October 2014 - 01:52 PM

View Postandrewsw, on 29 October 2014 - 01:37 PM, said:

There is an example of its use with the docs:

SqlDataReader.IsDBNull Method

i tried with that but it wasn't helpful , i have searched a lot but i couldn't solve it
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: Data is Null. This method or property cannot be called on Null values?

Posted 29 October 2014 - 01:55 PM

Did you read my previous post? What are you planning to do if a value of the field "cn" is null?
Was This Post Helpful? 0
  • +
  • -

#8 ari03   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 17-November 12

Re: Data is Null. This method or property cannot be called on Null values?

Posted 29 October 2014 - 02:18 PM

thank you mr andrewsw i think i'm making it hard im planing if is null messagebox show ("") or ignore this error

im trying to do as this
if (!reader.IsDBNull(reader.GetOrdinal("MiddleName")))
               Console.Write(" {0}", reader.GetString(reader.GetOrdinal("MiddleName")))

but but what i should write instead of console write ?
and sorry i'm at begin of programming
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: Data is Null. This method or property cannot be called on Null values?

Posted 29 October 2014 - 02:39 PM

if (reader.IsDBNull(reader.GetOrdinal("YourFieldName"))) {
    MessageBox.Show("");
}



But.. you were already displaying a MessageBox in your original code, so I don't know what is confusing you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1