ASP.NET - Add new datarow to datatable gives error Object reference not set to a
Asked By Firoz
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.
R B replied to Firoz
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);
}
Vickey F replied to Firoz
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
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 R B
datatable is global declaration and have already filled table
i just want to add new row in that table....
Anoop S replied to Firoz
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

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
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
R B replied to Firoz
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
Net hi friends Any one send frequently asked Important questions in C# .Net, ADO .Net, Asp .Net and Sql Server. . . . . . . . tx in Advance. . . . . . Hi, Find this. . (B)What is an IL? (B A) What is scavenging? (B) What are different types of caching using cache object of ASP.NET? (B) How can you cache different version of same page using ASP.NET cache object? (A) How will implement Page Fragment Caching? (B) Can you compare ASP.NET sessions with classic ASP? (B) Which are the various modes of storing ASP.NET session
Company" ]; GridView1.DataBind(); } } private DataSet CreateDS() { DataSet ds; if (Session[ "DataList_ParentChild" ] = = null ) { ds = new DataSet(); DataTable dt = new DataTable( "Company" ); DataRow dr; dt.Columns.Add( new DataColumn( "ID" , typeof (Int32))); dt.Columns.Add( new DataColumn( "CompanyName" , typeof ( string ))); dt.Columns.Add( new DataColumn( "Address" , typeof ( string ))); dt.Columns.Add( new DataColumn( "Name" , typeof ( string ))); dt.Columns.Add( new DataColumn( "Dept" , typeof ( string ))); for ( int i = 1; i < 10; i++) { dr = dt.NewRow(); dr[0
how we can bind datatable with gridview hello How we can bind data table with gridview Hi, Here is a EventArgs e) { string ConnString = "ConnectionString" ; if (!IsPostBack) { SqlDataAdapter da = new SqlDataAdapter( "Select * from emp" , ConnString); DataTable dt = new DataTable(); da.Fill(dt); GridView1.DataSource = dt; GridView1.DataBind(); } } Hope this will help you Use this code DataTable dt = new DataTable(); dt.Columns.Add("col1"); dt.Columns.Add("col2"); foreach (GridViewRow row in GridView1.Rows) { string col1 = row[0].Tostring(); string col2 = row[1].Tostring(); DataRow dr = dt.NewRow(); dr[0] = col1; dr[1] = col2; dt.Rows.Add(dr); } Now you can use DataTable. Try and let me know. hi. . if Datatable is dt gridview.DataSource = dt; gridview.DataBind
Gridview using Jquery hello to all, how to bind gridview using JQuery.and gridview is asp.net control. Use telerik radgrid's client side binding, see this for an example: http: / / demos using System.Data; public class DataStore { public DataStore() { / / / / TODO: Add constructor logic here / / } public static DataTable GetDataTable() { DataTable dt = new DataTable("Names"); DataColumn dc1 = new DataColumn("Name"); DataColumn dc2 = new DataColumn("Age"); dt.Columns.AddRange(new DataColumn[] { dc1, dc2 }); DataRow dr1 = dt.NewRow
will see image column in the GridView control just like shown below: GridView Examples for ASP.NET 2.0: Displaying Images in a GridView Column Click here to return to the TOC. In the ASP.NET 1.x class I teach, the course-long project for the students is to create album using a DataGrid . To display an image in a column in a DataGrid in ASP.NET 1.x you have to use a TemplateColumn with an Image Web control inside the TemplateColumn . With ASP.NET 2.0 the GridView includes an ImageField that can be used to display an image