G
Gaysorn
Hi:
How do I add the newrow to the existed datatable in
asp.net? Below is my coding that will give me the error
message say "System.NullReferenceException: Object
reference not set to an instance of an object." when I
try to click at btnAddDBA_Click to add new row to data
table.
Dim ds As New DataSet("myDataSet")
Dim dt As New DataTable("myDataTable")
Private Sub Page_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not Me.IsPostBack Then
PopulateDataTable()
BindGrid()
End If
End Sub
Private Sub PopulateDataTable()
ds.Tables.Add(dt)
Dim colDataColumn1 As New DataColumn("util_id")
colDataColumn1.DataType = System.Type.GetType
("System.Int32")
dt.Columns.Add(colDataColumn1)
Dim colDataColumn2 As New DataColumn("year")
colDataColumn2.DataType = System.Type.GetType
("System.Int32")
dt.Columns.Add(colDataColumn2)
Dim colDataColumn3 As New DataColumn("dba_name")
colDataColumn3.DataType = System.Type.GetType
("System.String")
dt.Columns.Add(colDataColumn3)
' create a DataRow
Dim rowDataRow As DataRow
rowDataRow = ds.Tables("myDataTable").NewRow
ds.Tables("myDataTable").Rows.Add(rowDataRow)
rowDataRow("util_id") = 7516
rowDataRow("year") = 2003
rowDataRow("dba_name") = "This is the first line
for DBA"
ds.AcceptChanges()
End Sub
Private Sub BindGrid()
dgDBA.DataSource = ds
dgDBA.DataBind()
End Sub
Private Sub btnAddDBA_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnAddDBA.Click
Dim rowDataRow As DataRow
Try
rowDataRow = ds.Tables("myDataTable").NewRow
ds.Tables("myDataTable").Rows.Add(rowDataRow)
rowDataRow("util_id") = 7516
rowDataRow("year") = 2003
rowDataRow("dba_name") = "New line for DBA"
ds.AcceptChanges()
BindGrid
Catch exc As Exception
Response.Write(exc.ToString)
End Try
End Sub
How do I add the newrow to the existed datatable in
asp.net? Below is my coding that will give me the error
message say "System.NullReferenceException: Object
reference not set to an instance of an object." when I
try to click at btnAddDBA_Click to add new row to data
table.
Dim ds As New DataSet("myDataSet")
Dim dt As New DataTable("myDataTable")
Private Sub Page_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not Me.IsPostBack Then
PopulateDataTable()
BindGrid()
End If
End Sub
Private Sub PopulateDataTable()
ds.Tables.Add(dt)
Dim colDataColumn1 As New DataColumn("util_id")
colDataColumn1.DataType = System.Type.GetType
("System.Int32")
dt.Columns.Add(colDataColumn1)
Dim colDataColumn2 As New DataColumn("year")
colDataColumn2.DataType = System.Type.GetType
("System.Int32")
dt.Columns.Add(colDataColumn2)
Dim colDataColumn3 As New DataColumn("dba_name")
colDataColumn3.DataType = System.Type.GetType
("System.String")
dt.Columns.Add(colDataColumn3)
' create a DataRow
Dim rowDataRow As DataRow
rowDataRow = ds.Tables("myDataTable").NewRow
ds.Tables("myDataTable").Rows.Add(rowDataRow)
rowDataRow("util_id") = 7516
rowDataRow("year") = 2003
rowDataRow("dba_name") = "This is the first line
for DBA"
ds.AcceptChanges()
End Sub
Private Sub BindGrid()
dgDBA.DataSource = ds
dgDBA.DataBind()
End Sub
Private Sub btnAddDBA_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnAddDBA.Click
Dim rowDataRow As DataRow
Try
rowDataRow = ds.Tables("myDataTable").NewRow
ds.Tables("myDataTable").Rows.Add(rowDataRow)
rowDataRow("util_id") = 7516
rowDataRow("year") = 2003
rowDataRow("dba_name") = "New line for DBA"
ds.AcceptChanges()
BindGrid
Catch exc As Exception
Response.Write(exc.ToString)
End Try
End Sub