1

I am doing an insertion operation, But every time I am getting one error in line:

DataRow row = ds.Tables[0].NewRow();

Saying,

Nullreferenceexception Was Unhandled.

Here is my 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 employeDB1
{
public partial class Insert : Form
{
    public Insert()
    {
        InitializeComponent();
    }
    DatabaseConnection objConnect;
    int inc = 0;
    int MaxRows;
    DataSet ds;
    private void Insert_Load(object sender, EventArgs e)
    {
    }

    private void button1_Click(object sender, EventArgs e)
    {
            DataRow row = ds.Tables[0].NewRow();
            row[1] = int.Parse(textBox1.Text);
            row[2] = textBox2.Text;
            row[3] = textBox3.Text;
            row[4] = int.Parse(textBox4.Text);
            ds.Tables[0].Rows.Add(row);
            try
            {
            objConnect.UpdateDatabase(ds);
            MaxRows = MaxRows + 1;
            inc = MaxRows - 1;
            MessageBox.Show("RECORD INSERTED");
            }
            catch(Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }
    }
}
codeling
  • 11,056
  • 4
  • 42
  • 71

4 Answers4

1

You have declared a DataSet in this line

DataSet ds;

but not set it to anything, so when you call

ds.Tables[0]

ds points to null, not an instance of DataSet.

Initialise your DataSet as follows

DataSet ds = new DataSet();
Trevor Pilley
  • 16,156
  • 5
  • 44
  • 60
  • Thank u. This error has gone. but now on the same line I am getting "index out of range exception was unhandled" error. plz tell me what to do? – user2971714 Jan 14 '14 at 14:47
  • You haven't added a table, so when you call `ds.Tables[0]` you are getting `null` not a `DataTable` – Trevor Pilley Jan 14 '14 at 14:48
1

You probably need to do the initialization.

DataSet ds = new DataSet();

I don't see any place from where you are filling your dataset.

What I understand is, You are creating a Dataset in which a table has 4 columns and then send this dataset to another function. For that

// create a dataset
DataSet ds = new DataSet();

// create a datatable
DataTable dt = new DataTable();

// add the columns into this table
dt.Columns.Add("col1");
dt.Columns.Add("col2");
dt.Columns.Add("col3");
dt.Columns.Add("col4");

// add this table to dataset
ds.Tables.Add(dt);

Now you can use the dataset ds as you are using.

Sachin
  • 40,216
  • 7
  • 90
  • 102
  • Thank u. This error has gone. but now on the same line I am getting "index out of range exception was unhandled" error. plz tell me what to do? – user2971714 Jan 14 '14 at 14:25
0

You need to have an instance of DataSet

DataSet ds = new DataSet(); must exist in your code before any use of ds.

  • Please put some effort in to your answers, do you really expect this to help the OP who is clearly struggling with the basics – musefan Jan 14 '14 at 13:30
  • Thank u. This error has gone. but now on the same line I am getting "index out of range exception was unhandled" error. plz tell me what to do? – user2971714 Jan 14 '14 at 14:52
0

Your dataset is null as it hasn't been populated with anything.

Look at the MSDN example here.

From the above example:

 // Fill the DataSet.
 DataSet dataSet = new DataSet("Suppliers");
 adapter.Fill(dataSet);
Damon
  • 3,004
  • 7
  • 24
  • 28
  • Thank u. This error has gone. but now on the same line I am getting "index out of range exception was unhandled" error. plz tell me what to do? – user2971714 Jan 14 '14 at 14:57