0

Hi,

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)";
        dbConnection.Open();
        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();
                }
            }
        } 
        dbConnection.Close();
3
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by svilla
0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.