2 Replies - 2507 Views - Last Post: 10 March 2012 - 04:32 AM Rate Topic: -----

#1 StickHerHappy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 15-January 12

how to Check if the every value in row is null or no value

Posted 10 March 2012 - 03:09 AM

string Input_Rec = "SELECT ID,Stud_Name,Pre_Q1,Pre_Q2,Pre_Q3,Pre_Q4,Pre_Proj,Pre_Major,Pre_Total,Mid_Q1,Mid_Q2,Mid_Q3,Mid_Q4,Mid_Proj,Mid_Major,Mid_Total,Fin_Q1,Fin_Q2,Fin_Q3,Fin_Q4,Fin_Proj,Fin_Major,Fin_Total FROM Student WHERE ID=" + textBox1.Text;
            
connect.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\directory\sample.accdb;Persist Security Info=False;";
            
connect.Open();

OleDbCommand command = new OleDbCommand(Input_Rec, connect);
            OleDbDataReader reader = command.ExecuteReader();
            
            if (reader.Read())
            {
                 if (reader[0] != null || reader[0].ToString() != " " &&.... 
            }



I've used reader[0].ToString()!= null || reader[0].ToString()!= " "; to compare if the value of the data in database is null but still can't filter the null values..

Is This A Good Question/Topic? 0
  • +

Replies To: how to Check if the every value in row is null or no value

#2 Momerath   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1021
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: how to Check if the every value in row is null or no value

Posted 10 March 2012 - 03:33 AM

reader[0].ToString != " " is not an empty string, it's a string with a single space. So you are saying "if it isn't null or a single space, it's good". Use String.Empty (or "" with no space between them) to check for an empty string.

You should also be using different logic in there, as a null string will return a false for the first part of the or statement, which means it will check the 2nd part which should generate an error (null reference exception). So you want something like
if (!(reader[0] == null || reader[0].ToString() == String.Empty()) ...
This way it checks if null, and if true stops evaluating the expression and returns true, which is flipped to false by the ! operator.

You might also try (if this still isn't working) comparing it to OleDbType.Empty

This post has been edited by Momerath: 10 March 2012 - 03:44 AM

Was This Post Helpful? 1
  • +
  • -

#3 StickHerHappy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 15-January 12

Re: how to Check if the every value in row is null or no value

Posted 10 March 2012 - 04:32 AM

great it works! tnx sir :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1