I seem to have an issue with this code...its giving me an error at this line:

lblDB.Text = currentRecord.ToString();

The error is System.NullReferenceException was unhandled by user code

I have debugged and connection to DB is working and the dataReader does have the record in it but I am stuck in the output! Can you please help me out?

Thanks in advance....

string dbConnectionString = "SERVER=******;" + "DATABASE=*****;" + "UID=*****;" + "PASSWORD=********;";
        MySqlConnection dbConnection = new MySqlConnection(dbConnectionString);
        MySqlCommand dbQueryString = dbConnection.CreateCommand(); 
        MySqlDataReader dataReader; 
        dbQueryString.CommandText = "SELECT " +
                                      "tbl_news.news_title, " +
                                      "tbl_news.news_desc, " +
                                      "tbl_users.user_name, " +
                                      "tbl_news.news_datecreated " +
                                    "FROM " +
                                      "tbl_news " +
                                      "INNER JOIN tbl_users ON (tbl_news.news_user_fk = tbl_users.user_id)";
        dataReader = dbQueryString.ExecuteReader();
        DataSet data = new DataSet();
        if (dataReader.HasRows)
            while (dataReader.Read())
                string currentRecord = "";
                for (int i = 0; i < dataReader.FieldCount; i++)
                    currentRecord += dataReader.GetValue(i).ToString();
                    lblDB.Text = currentRecord.ToString();

Recommended Answers

All 2 Replies

Member Avatar

Are any of the returned fields null?

You may need to test to make sure that the value returned from the database is not a null before converting it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.