Data is Null. This method or property cannot be called on null values RRS feed

  • Question

  • Hi,

    I need some help with my program I am populating text boxes with data from a SQL Data BAse, but sometimes the values are "NULL" and so i get the following error : "Data is Null. This method or property cannot be called on null values"

    How can i write it so the reader ignores the null value and continues or puts in a blank??

    I am aware of IsDBNull function but am not sure how to implement it.

    any help greatly appreciated.

    Here is the code: 

    written in Visual Studio 2010 - Windows Form Application (c++) 

    private: System::Void listBoxCustomers_SelectedIndexChanged(System::Object^  sender, System::EventArgs^  e) {
    			 String^ ListBoxVal=listBoxCustomers->Text;
    			 String^ constring=L"datasource=;port=3306;username=root;password=root";
    				 MySqlConnection^ conDataBase=gcnew MySqlConnection(constring);
    				 MySqlCommand^  cmdDataBase=gcnew MySqlCommand("select * from equipe.customers where name ='"+ListBoxVal+"';",conDataBase);
    				 MySqlDataReader^ myReader;
    				 try {
    							String ^ companyVal = myReader->GetString("company");
    							txtCompany -> Text = companyVal;
    							String ^ address1Val = myReader->GetString("address1");
    							txtAddress1 -> Text = address1Val;
    							String ^ address2Val = myReader->GetString("address2");
    							txtAddress2 -> Text = address2Val;
    							String ^ cityVal = myReader->GetString("city");
    							txtCity -> Text = cityVal;
    							String ^ stateVal = myReader->GetString("state");
    							txtState -> Text = stateVal;
    							String ^ zipVal = myReader->GetString("zip");
    							txtZip -> Text = zipVal;
    							String ^ nameVal = myReader->GetString("name");
    							txtName -> Text = nameVal;
    							String ^ idVal = myReader->GetString("id");
    							txtId -> Text = idVal;
    				 }	catch(Exception^ex){



    Tuesday, July 16, 2013 7:45 AM


  • Hi Ariel,

    If there is a field in the database can contail null values, you need to check whether it is null value before you use them. There is an IsDBNull method to check the values.

    Best regards,

    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help

    • Marked as answer by Chester Hong Sunday, July 28, 2013 3:53 PM
    Friday, July 19, 2013 1:57 AM