|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
adding a new row to the repeater controlHere'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 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 "buran" <b@b.com> wrote in message Dear ASP.NET Programmers,news:eeGE5ZFNFHA.2680@TK2MSFTNGP09.phx.gbl... 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 Thank you :)
Show quoteHide quote "Jc Morin" <microsoftnewsgr***@jcmorin.net> wrote in message news:O3NFUhFNFHA.3076@TK2MSFTNGP14.phx.gbl... > 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 > > > "buran" <b@b.com> wrote in message > news:eeGE5ZFNFHA.2680@TK2MSFTNGP09.phx.gbl... > 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 > >
Checkbox in a datagrid always returns false
hosting app from pc Highlight the datalist row Div tag to display/ hide a control ....please help Chart position RadioBNuttonList Problem query database, create control, display on page Validation on controls within a repeater Trouble Filling DropDownList in Datagrid Compatibility problem of IEWebConrols |
|||||||||||||||||||||||