NullReferenceException was unhandled

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • maria cristina
    New Member
    • Feb 2014
    • 1

    NullReferenceException was unhandled

    I make a windows form application with 4 text fields.
    And I connect the database to it. When I run the program I want to display the data in text fields but it displays nothing as shown below:



    and then I always got the error "NullReferenceE xception was unhandled". I don't know if why this happen. My codes is :

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace EmployeesDatabase
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
    
            }
            DatabaseConnection objConnect;
            string conString;
    
            DataSet ds;
            DataRow dRow;
    
            int MaxRows;
            int inc = 0;
    
            private void Form1_Load(object sender, EventArgs e)
            {
                try
                {
                    objConnect = new DatabaseConnection();
                    conString = Properties.Settings.Default.EmployeesConnectionString;
    
                    objConnect.connection_string = conString;
                    objConnect.Sql = Properties.Settings.Default.SQL;
    
                    ds = objConnect.GetConnection;
                    MaxRows = ds.Tables[0].Rows.Count;
    
    
                    NavigateRecords();
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.Message);
                }
            }
    
            private void NavigateRecords()
            {
                dRow = ds.Tables[0].Rows[t];
                txtFirstName.Text = dRow.ItemArray.GetValue(1).ToString();
                txtSurname.Text = dRow.ItemArray.GetValue(2).ToString();
                txtJobTitle.Text = dRow.ItemArray.GetValue(3).ToString();
                txtDepartment.Text = dRow.ItemArray.GetValue(4).ToString();
            }
        }
    }
    This is the screen show when I run the program, the error is in the method "NavigateRecord s". How can I solve this? Thanks in advance!

  • ThatThatGuy
    Recognized Expert Contributor
    • Jul 2009
    • 453

    #2
    Do check the count in the 'MaxRows' Variable before calling NavigateRecords (), I strongly feel there are no record returning from your query

    Comment

    • 2147483648
      New Member
      • Feb 2014
      • 11

      #3
      u r absolutely r8 that that why
      without rows he is asking to navigagate the records........

      Comment

      Working...