ASP.NET - Add new datarow to datatable gives error Object reference not set to a

Asked By Firoz on 16-Nov-11 12:28 AM
Hi
i am trying to add new row in datatable by following code gives me error Object reference not set to an instance of an object.
code as follows.


            Dim row As DataRow       
            row = datatable.NewRow()
            row.Item(2) = txt_rate.Text
            row.Item(3) = dd_stngrp.SelectedItem.Value
            row.Item(4) = dd_stngrpcat.SelectedItem.Value
            row.Item(5) = dd_stngrppackage.SelectedItem.Value
            row.Item(6) = txt_rateeffrom.Text
            row.Item(7) = txt_rateefto.Text
            row.Item(8) = dd_programcat.SelectedItem.Value
            row.Item(9) = dd_timecat.SelectedItem.Value
            row.Item(10) = dd_fctduration.SelectedItem.Value
            datatable.Rows.Add(row)
         it will produce error at 2nd line Object reference not set to an instance of an object.
Please help me out Thanks.
Chintan Vaghela replied to Firoz on 16-Nov-11 12:30 AM
Hello,

Try datatable as follows

DataTable dt = new DataTable();

 

        DataRow dr = null;

        dt.Columns.Add(new DataColumn("Unit", typeof(string)));

 

        for (int i = 0; i < 10; i++)

        {

          dr = dt.NewRow();

          dr["Unit"] = "";

 

          dt.Rows.Add(dr);

        }

 

Jitendra Faye replied to Firoz on 16-Nov-11 12:32 AM
This error comes when you try to access any null object.

Before accessing any object check for null in your code.
Reena Jain replied to Firoz on 16-Nov-11 12:33 AM
hi,

just put your datatable in

if(!IsPostBack)

{
   DataTable dt = new DataTable();
addrows();
}

because every time page is loading and your datatable is declaring as new

hope this will help you. let me know if not
Firoz replied to Chintan Vaghela on 16-Nov-11 12:34 AM
datatable is global declaration and have already filled table 
i just want to add new row in that table....
Anoop S replied to Firoz on 16-Nov-11 12:38 AM
Try like this way

DataTable theTable = new DataTable();

// Add the row
theTable.Rows.Add("Hi");

// Add a column
theTable.Columns.Add("Test");
aneesa replied to Firoz on 16-Nov-11 12:43 AM

Adding Data to a Table 

The following example demonstrates how to create a new row by calling the NewRow method.
Dim workRow As DataRow = workTable.NewRow()
You then can manipulate the newly added row using an index or the column name, as shown in the following example.
workRow("CustLName") = "Smith"
workRow(1) = "Smith"
After data is inserted into the new row, the Add method is used to add the row to thehttp://msdn.microsoft.com/en-us/library/system.data.datarowcollection(v=vs.80).aspx, shown in the following code.
workTable.Rows.Add(workRow)
You can also call the Add method to add a new row by passing in an array of values, typed as http://msdn.microsoft.com/en-us/library/system.object(v=vs.80).aspx, as shown in the following example.
workTable.Rows.Add(new Object() {1, "Smith"})

Passing an array of values, typed as Object, to the Add method creates a new row inside the table and sets its column values to the values in the object array. Note that values in the array are matched sequentially to the columns, based on the order in which they appear in the table.
The following example adds 10 rows to the newly created Customers table.

Dim workRow As DataRow
Dim i As Integer

For i = 0 To 9
  workRow = workTable.NewRow()
  workRow(0) = i
  workRow(1) = "CustName" & I.ToString()
  workTable.Rows.Add(workRow)
Next


aneesa replied to Firoz on 16-Nov-11 12:45 AM
Function CreateData() As DataTable
     Dim DT As New DataTable()
     Dim Row1, Row2, Row3, Row4 As DataRow
     DT.Columns.Add(New DataColumn("name", System.Type.GetType("System.String")))
     DT.Columns.Add(New DataColumn("city", System.Type.GetType("System.String")))
     Row1 = DT.NewRow()
     Row1("name""Name Here"
     Row1("city""City Here"
     DT.Rows.Add(Row1)
     Row2 = DT.NewRow()
     Row2("name""Another Name Here"
     Row2("city""City 2 Here"
     DT.Rows.Add(Row2)
     Row3 = DT.NewRow()
     Row3("name""Name 3"
     Row3("city""City 3"
     DT.Rows.Add(Row3)
     Row4 = DT.NewRow()
     Row4("name""Name 4"
     Row4("city""City 4"
     DT.Rows.Add(Row4)
     Return DT
    End Function
Chintan Vaghela replied to Firoz on 16-Nov-11 01:11 AM
Hello,

Try this

DataRow dr = null;

          dr = dt.NewRow();

          dr[dt.Columns[0].ToString()] = "5";
  dr[dt.Columns[1].ToString()] = "5";

          dt.Rows.Add(dr);

Hope this helpful