adding a new row to the repeater control

B

buran

Dear ASP.NET Programmers,

Here's my problem: I have a page (as usual :), in which I'm going to display invoices in a repeater control. I am binding data to the repeater control (ID: repHospCosts) without any problems. I have also a button on the page, I am going to add a new datarow programmatically and display it when pressed. Unfortunately, I cannot refer to the dataset in the event handler of the button (or at least I think so). I get the following error: Object reference not set to an instance of an object. drow = dtable.NewRow

Below is the code:

Dim dtable As DataTable
Dim drow As DataRow

If Not Page.IsPostBack Then

LoadHospCosts()

End If

Sub LoadHospCosts()

strSql = "spGetHospAmounts"
myCommand.CommandText = strSql
myCommand.CommandType = CommandType.StoredProcedure
myCommand.Parameters.Clear()
myCommand.Parameters.Add("@ourFileNo", Session("selectedFileNumber"))
myDataAdapter.Fill(myDataSet, "HospCosts")
repHospCosts.DataSource = myDataSet
repHospCosts.DataMember = "HospCosts"
repHospCosts.DataBind()

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

dtable = myDataSet.Tables("HospCosts")
drow = dtable.NewRow
drow("SPName") = "Buran"
drow("InvoiceAmount") = 100
dtable.Rows.Add(drow)
dtable.AcceptChanges()

Dim subTotal As Double
For Each drow In dtable.Rows
subTotal += drow.Item("InvoiceAmount")
Next
lblSubTotal.Text = Format(subTotal, "n")

End Sub

I need any help I can get, thanks in advance...

Buran
 
J

Jc Morin

Hi Buran,

The problem is simple you bind your DataTable only on the first page load,
when you click the button, this is now a postback and you re-bind the data
on postback.

A solution would be to retrieve again the information from the database on a
postback or some mechanism with session or cache to keep it from postback to
postback.

Hope that help.


--------------------------
Jean-Claude Morin, MCP
Software Developer
2k1Soft/kCentric, Canada


Dear ASP.NET Programmers,
Here's my problem: I have a page (as usual :), in which I'm going to display
invoices in a repeater control. I am binding data to the repeater control
(ID: repHospCosts) without any problems. I have also a button on the page, I
am going to add a new datarow programmatically and display it when pressed.
Unfortunately, I cannot refer to the dataset in the event handler of the
button (or at least I think so). I get the following error: Object reference
not set to an instance of an object. drow = dtable.NewRow
Below is the code:
Dim dtable As DataTable
Dim drow As DataRow
If Not Page.IsPostBack Then
LoadHospCosts()
End If
Sub LoadHospCosts()
strSql = "spGetHospAmounts"
myCommand.CommandText = strSql
myCommand.CommandType = CommandType.StoredProcedure
myCommand.Parameters.Clear()
myCommand.Parameters.Add("@ourFileNo",
Session("selectedFileNumber"))
myDataAdapter.Fill(myDataSet, "HospCosts")
repHospCosts.DataSource = myDataSet
repHospCosts.DataMember = "HospCosts"
repHospCosts.DataBind()

End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
dtable = myDataSet.Tables("HospCosts")
drow = dtable.NewRow
drow("SPName") = "Buran"
drow("InvoiceAmount") = 100
dtable.Rows.Add(drow)
dtable.AcceptChanges()
Dim subTotal As Double
For Each drow In dtable.Rows
subTotal += drow.Item("InvoiceAmount")
Next
lblSubTotal.Text = Format(subTotal, "n")
End Sub
I need any help I can get, thanks in advance...
Buran
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top