0

I am writting a programme, which imports data from data base. I want to place all these tables in my own object propeties. All these DataTable properties are filled until Base examBase = Base() is beeing executed, but after this line, the exam.Base.Tests doesnt's exist anymore. What causes the problem?

class Base
{
    MySqlConnection myConnection;
    MySqlDataAdapter testsDataAdapter;
    DataTable testsDataTable;
    MySqlDataAdapter questionsDataAdapter;
    DataTable questionsDataTable;
    MySqlDataAdapter answersDataAdapter;
    DataTable answersDataTable;



    public Base()
    {
        string myConnectionString = "Database=Exams;Data Source=localhost;User Id=root;Password=";
        myConnection = new MySqlConnection(myConnectionString);
        myConnection.Open();
        GetTests();
        GetQuestions();
        GetAnswers();
    }

    private void GetTests()
    {
        string testQuery = "SELECT * FROM tests";
        testsDataAdapter = new MySqlDataAdapter(testQuery, myConnection);
        testsDataTable = new DataTable();
        testsDataAdapter.Fill(testsDataTable);
        testsDataTable.PrimaryKey = new DataColumn[] { testsDataTable.Columns["TestID"] };

        this.Tests = testsDataTable;
    }

    private void GetQuestions()
    {
        string questionQuery = "SELECT * FROM questions";
        questionsDataAdapter = new MySqlDataAdapter(questionQuery, myConnection);
        questionsDataTable = new DataTable();
        questionsDataAdapter.Fill(questionsDataTable);

        this.Questions = questionsDataTable;
    }

    private void GetAnswers()
    {
        string answerQuery = "SELECT * FROM answers";
        answersDataAdapter = new MySqlDataAdapter(answerQuery, myConnection);
        answersDataTable = new DataTable();
        answersDataAdapter.Fill(answersDataTable);

        this.Answers = answersDataTable;
    }
    public DataTable Tests { get; set; }
    public DataTable Questions { get; set; }
    public DataTable Answers { get; set; }


}

and the exception is firstly seen in GetName() method:

 class Test
{
    Base examBase;
    private List<Question> questions;

    public Test(int testID)
    {
        examBase = new Base();
        this.TestID = testID;

        GetName();
        GetDescription();
        GetAuthor();
        GetQuestions();

    }

    private void GetName()
    {

        this.Name = examBase.Tests.Rows.Find(this.TestID)["Name"].ToString();

    }

edit

Ok, the object examBase.Tests exists, but there's something with whis Find() method. In my base, in the table "Tests" I have a primary key (column TestID), but I get the KeyMissing exception. Meybe I use this Find() incorrectly?

marani
  • 1
  • 1
  • Welcome to StackOverflow! Have you tried debugging your applications to find where `examBase.Tests` goes missing? – John Odom May 01 '15 at 20:25
  • 1
    You don't have to have a connection open when using a DataAdapter. It will open and close it for you as necessary. – LarsTech May 01 '15 at 20:29

1 Answers1

0

I think the "Name" field is null. Do a null check before converting to a string:

Change

this.Name = examBase.Tests.Rows.Find(this.TestID)["Name"].ToString();

To

if(examBase.Tests.Rows.Find(this.TestID)["Name"] != null)
{
    this.Name = examBase.Tests.Rows.Find(this.TestID)["Name"].ToString();
}

Failing that

if(examBase.Tests.Rows.Find(this.TestID) != null)
{
    this.Name = examBase.Tests.Rows.Find(this.TestID)["Name"].ToString();
}
garryp
  • 5,508
  • 1
  • 29
  • 41
  • I am sure that test with this exact testID exists and has a name.. – marani May 01 '15 at 20:51
  • If the value ["Name"] is null rather than a blank string that ToString() you have will throw a NullReferenceException. – garryp May 01 '15 at 21:06
  • I copied Your code and now it stops on `if(examBase.Tests.Rows.Find(this.TestID)["Name"] != null)`. I added an edit note to my question (up). – marani May 01 '15 at 21:13
  • See my edit. Basically your problem is that somewhere you are attempting to invoke a function/get a property/access a field on a null. – garryp May 01 '15 at 21:40