Add new row to data table

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
 
G

Gaysorn

Here is the stack trace

Stack Trace:

[NullReferenceException: Object reference not set to an
instance of an object.]
a_gsk.test2.btnAddDBA_Click(Object sender, EventArgs
e) in \\wwwroot$\a_gsk\res\test2.aspx.vb:75
System.Web.UI.WebControls.Button.OnClick(EventArgs e)

System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEv
entHandler.RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent
(IPostBackEventHandler sourceControl, String
eventArgument)
System.Web.UI.Page.RaisePostBackEvent
(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain()
 
M

Miha Markic

In the click event, try replacing
ds.Tables("myDataTable")
with
dt

And, put a breakpoint in first row, then go step by step (F10).

--
Miha Markic - RightHand .NET consulting & development
miha at rthand com

Gaysorn said:
Here is the stack trace

Stack Trace:

[NullReferenceException: Object reference not set to an
instance of an object.]
a_gsk.test2.btnAddDBA_Click(Object sender, EventArgs
e) in \\wwwroot$\a_gsk\res\test2.aspx.vb:75
System.Web.UI.WebControls.Button.OnClick(EventArgs e)

System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEv
entHandler.RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent
(IPostBackEventHandler sourceControl, String
eventArgument)
System.Web.UI.Page.RaisePostBackEvent
(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain()


-----Original Message-----
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
.
 
G

Gaysorn

I replaced ds.Tables("myDataTable") with dt. Then I step
into, the error happened at rowDataRow("util_id") = 7516
with message say "System.ArgumentException:
Column 'util_id' does not belong to table myDataTable. at
System.Data.DataRow.set_Item(String columnName, Object
value)"

Thank you for your help.

-----Original Message-----
In the click event, try replacing
ds.Tables("myDataTable")
with
dt

And, put a breakpoint in first row, then go step by step (F10).

--
Miha Markic - RightHand .NET consulting & development
miha at rthand com

Here is the stack trace

Stack Trace:

[NullReferenceException: Object reference not set to an
instance of an object.]
a_gsk.test2.btnAddDBA_Click(Object sender, EventArgs
e) in \\wwwroot$\a_gsk\res\test2.aspx.vb:75
System.Web.UI.WebControls.Button.OnClick(EventArgs e)

System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEv
entHandler.RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent
(IPostBackEventHandler sourceControl, String
eventArgument)
System.Web.UI.Page.RaisePostBackEvent
(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain()


-----Original Message-----
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
.


.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top