Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to check if field value is NULL?

How to check if field value is NULL?

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    anju
    wrote on last edited by
    #1

    Hi all, How to check if field value is NULL? In my application i am using the code as follows to get the Recordset field value while (VARIANT_FALSE == pRecordset->EndOfFile) { strTemp.Format("Test Column:%s", (LPSTR)(_bstr_t)pRecordset->Fields->GetItem("Test")->Value); AfxMessageBox(strTemp); pRecordset->MoveNext(); } while running the application if the Field value is NULL. I am getting Runtime Error as "abnormal Programme termination". please help me anju

    R I J 3 Replies Last reply
    0
    • A anju

      Hi all, How to check if field value is NULL? In my application i am using the code as follows to get the Recordset field value while (VARIANT_FALSE == pRecordset->EndOfFile) { strTemp.Format("Test Column:%s", (LPSTR)(_bstr_t)pRecordset->Fields->GetItem("Test")->Value); AfxMessageBox(strTemp); pRecordset->MoveNext(); } while running the application if the Field value is NULL. I am getting Runtime Error as "abnormal Programme termination". please help me anju

      R Offline
      R Offline
      Rama Krishna Vavilala
      wrote on last edited by
      #2

      You need to do something like this.

      _variant_t vt = pRecordset->Fields->GetItem("Test")->Value;

      if (vt.vt == VT_NULL)
      {
      AfxMessageBox("Field is NULL");
      }
      else
      {
      strTemp.Format("Test Column:%s",(LPSTR)(_bstr_t)vt);
      AfxMessageBox(strTemp);
      }

      C# is fundamentally broken. - Christian Graus

      A 1 Reply Last reply
      0
      • A anju

        Hi all, How to check if field value is NULL? In my application i am using the code as follows to get the Recordset field value while (VARIANT_FALSE == pRecordset->EndOfFile) { strTemp.Format("Test Column:%s", (LPSTR)(_bstr_t)pRecordset->Fields->GetItem("Test")->Value); AfxMessageBox(strTemp); pRecordset->MoveNext(); } while running the application if the Field value is NULL. I am getting Runtime Error as "abnormal Programme termination". please help me anju

        I Offline
        I Offline
        Imran Farooqui
        wrote on last edited by
        #3

        Consider query: select nick from from mytable. Suppose the value represented by nick can be NULL. So change the query as: select isnull(nick, 'something') as nick from mytable Here 'something' can be anything you desire. This can be a single space character also. Now when you collect value of nick from Recordset object, it'll not be null but the value supplied by you as a parmeter above. You can check it and treat as NULL Imran Farooqui World first Urdu Instant Messenger[^] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Java is a tool for creating applications that torture users with its awful speed and its ugly interfaces. Daniel Turini commenting on this article

        A 1 Reply Last reply
        0
        • A anju

          Hi all, How to check if field value is NULL? In my application i am using the code as follows to get the Recordset field value while (VARIANT_FALSE == pRecordset->EndOfFile) { strTemp.Format("Test Column:%s", (LPSTR)(_bstr_t)pRecordset->Fields->GetItem("Test")->Value); AfxMessageBox(strTemp); pRecordset->MoveNext(); } while running the application if the Field value is NULL. I am getting Runtime Error as "abnormal Programme termination". please help me anju

          J Offline
          J Offline
          James Pullicino
          wrote on last edited by
          #4

          This line: (LPSTR)(_bstr_t)pRecordset->Fields->GetItem("Test")->Value); is very dangerous because of the following reasons: If Fields fails, an exception will be thrown. If GetItem fails, an exception will be thrown. If Value cannot be converted to a _bstr_t, an exception will be thrown. Segment your code a little better. Add exception handling, or use the raw_ methods of your smart pointers. Here is an example on how to improve your code (this is from memory): IFieldPtr field; try { field = pRecordset->Fields->GetItem("Test"); } catch(com_error e) { field = NULL; } if(field != NULL) { _variant_t vtVal; try { vtVal = field->Value; } catch (_com_error e) {vtVal.vt = VT_ERROR; } _bstr_t bstrVal; if(vtVal.vt != VT_ERROR) { try{ bstrVal = (_bstr_t)vtVal; } catch(com_error e) {bstrVal = L"";} } AfxMessageBox((LPCTSTR)bstrVal); } --James Drinking In The Sun Forgot Password?

          A 1 Reply Last reply
          0
          • I Imran Farooqui

            Consider query: select nick from from mytable. Suppose the value represented by nick can be NULL. So change the query as: select isnull(nick, 'something') as nick from mytable Here 'something' can be anything you desire. This can be a single space character also. Now when you collect value of nick from Recordset object, it'll not be null but the value supplied by you as a parmeter above. You can check it and treat as NULL Imran Farooqui World first Urdu Instant Messenger[^] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Java is a tool for creating applications that torture users with its awful speed and its ugly interfaces. Daniel Turini commenting on this article

            A Offline
            A Offline
            anju
            wrote on last edited by
            #5

            Hi Imran Farooqui, Thanks for Your Reply. Please need more help to me.............. Suppose i have to select entire table then how can i make all null fields in the table to 'Something'. in my application i am opening the recordset as follows pRecordset->Open("mytable", _variant_t((IDispatch*)pConnection,true), adOpenDynamic, adLockPessimistic,adCmdTable); and i am getting all the fields pRecordset->MoveFirst(); while (!pRecordset->EndOfFile) { strTemp.Format("Field1:%s",(LPSTR)(_bstr_t)pRecordset->Fields->GetItem("Field1")->Value); AfxMessageBox(strTemp); pRecordset->MoveNext(); } :rose: anju

            I 1 Reply Last reply
            0
            • R Rama Krishna Vavilala

              You need to do something like this.

              _variant_t vt = pRecordset->Fields->GetItem("Test")->Value;

              if (vt.vt == VT_NULL)
              {
              AfxMessageBox("Field is NULL");
              }
              else
              {
              strTemp.Format("Test Column:%s",(LPSTR)(_bstr_t)vt);
              AfxMessageBox(strTemp);
              }

              C# is fundamentally broken. - Christian Graus

              A Offline
              A Offline
              anju
              wrote on last edited by
              #6

              Hi Rama Krishna, ThankQ this technique is work to me well :rose: anju

              1 Reply Last reply
              0
              • J James Pullicino

                This line: (LPSTR)(_bstr_t)pRecordset->Fields->GetItem("Test")->Value); is very dangerous because of the following reasons: If Fields fails, an exception will be thrown. If GetItem fails, an exception will be thrown. If Value cannot be converted to a _bstr_t, an exception will be thrown. Segment your code a little better. Add exception handling, or use the raw_ methods of your smart pointers. Here is an example on how to improve your code (this is from memory): IFieldPtr field; try { field = pRecordset->Fields->GetItem("Test"); } catch(com_error e) { field = NULL; } if(field != NULL) { _variant_t vtVal; try { vtVal = field->Value; } catch (_com_error e) {vtVal.vt = VT_ERROR; } _bstr_t bstrVal; if(vtVal.vt != VT_ERROR) { try{ bstrVal = (_bstr_t)vtVal; } catch(com_error e) {bstrVal = L"";} } AfxMessageBox((LPCTSTR)bstrVal); } --James Drinking In The Sun Forgot Password?

                A Offline
                A Offline
                anju
                wrote on last edited by
                #7

                Hi James Pullicino, Thanks a lot.. I never thing about what u pointed. now my code is changed as per your instructions. once again thank to you :rose: anju

                1 Reply Last reply
                0
                • A anju

                  Hi Imran Farooqui, Thanks for Your Reply. Please need more help to me.............. Suppose i have to select entire table then how can i make all null fields in the table to 'Something'. in my application i am opening the recordset as follows pRecordset->Open("mytable", _variant_t((IDispatch*)pConnection,true), adOpenDynamic, adLockPessimistic,adCmdTable); and i am getting all the fields pRecordset->MoveFirst(); while (!pRecordset->EndOfFile) { strTemp.Format("Field1:%s",(LPSTR)(_bstr_t)pRecordset->Fields->GetItem("Field1")->Value); AfxMessageBox(strTemp); pRecordset->MoveNext(); } :rose: anju

                  I Offline
                  I Offline
                  Imran Farooqui
                  wrote on last edited by
                  #8

                  In your code, you'r opening a complete table using RecordSet object. I do not prefer this technique because you have no control over the selected records. Instead i prefer to explicitly open the table using SQL queries. m_pRecordSet = m_pConnection->Execute("select * from mytable", &vRecsAffected, adOptionUnspecified); Only if you are accessing the table in this way, you can use, isnull option. Imran Farooqui World first Urdu Instant Messenger[^] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Java is a tool for creating applications that torture users with its awful speed and its ugly interfaces. Daniel Turini commenting on this article

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups