Telerik radGrid - possible to do use AllowAutomaticUpdates when Datasource = a dataset? - asp.net

I am setting the datasource of my radGrid to a dataset (that I have stored in session).
I have enabled AllowAutomaticUpdates and EnableViewState, implemented NeedDataSource, set DatakeyNames, etc. (see code below)
However, when I press the Edit button and make a change and press the Update link, the record does not update and leave Edit Mode.....it just stays in edit mode, and no error of any kind occurs.
So, the question is....does anyone know if radGrid with EnableViewstate even supports AutomaticUpdates, so the changes in the grid will be automatically pushed into the dataset to which it is bound?
One would think you could read the documentation, but I have been unable to find a definitive answer.
Thanks
<telerik:Radgrid id="grid" runat="server" AllowPaging="True" AllowSorting="True" AllowAutomaticUpdates="true"
AutoGenerateEditColumn="True" GridLines="None" >
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim data As New DataGateway
Dim ds As DataSet = data.GetEmployeesByProgram(Year:=2009, ProgramName:="Long Term Incentive Stock Program")
Dim dt As DataTable = ds.Tables(0)
ds.Tables(0).PrimaryKey = New DataColumn() {dt.Columns("EmployeeNum"), dt.Columns("ProgramName"), dt.Columns("Year")}
Session("datasource") = ds
With Me.grid
.AllowAutomaticUpdates = True
.AutoGenerateColumns = True
.AllowSorting = True
.AutoGenerateEditColumn = True
.EnableViewState = True 'IS REQUIRED!!!
Me.grid.MasterTableView.AllowAutomaticUpdates = True
Me.grid.MasterTableView.EditMode = GridEditMode.InPlace
End With
End If
End Sub
Private Sub grid_NeedDataSource(ByVal source As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles grid.NeedDataSource
Debug.WriteLine("NeedDataSource: " & e.RebindReason.ToString)
Dim ds As DataSet = CType(Session("datasource"), DataSet)
Me.grid.MasterTableView.DataKeyNames = New String() {"EmployeeNum", "ProgramName", "Year"}
Me.grid.DataSource = ds
End Sub

In short, there is one key issue at play here:
The "automatic" operations are only supported when you're using a Data Source control to bind the grid. That includes the ObjectDataSource, so you can use your DAL with the ODS and then support auto upserts/updates/deletes.
When binding directly to a data table, you must handle the updates manually. That's because it is the data source controls- not the RadGrid- that are providing the "auto" logic for CRUD operations. Fortunately, it's not hard to handle update manually if that's the path you prefer. Check out some of the demos on Telerik.com for examples:
http://demos.telerik.com/aspnet-ajax/grid/examples/dataediting/editmodes/defaultcs.aspx
You can also use the RadGrid with ViewState disabled if you want to. The best way to do that is to use the Grid's support for client-side databinding (though that does require that you expose your DAL via a Service Layer). Check out the demos for that approach here:
http://demos.telerik.com/aspnet-ajax/grid/examples/client/insertupdatedelete/defaultcs.aspx
Hope that helps!
-Todd

Your question has already been answered in the Telerik forums:
http://www.telerik.com/community/forums/aspnet/grid/is-it-possible-to-do-automaticupdates-to-a-dataset.aspx

Related

VB.Net exception: Object reference not set to an instance of an object

I'm currently working on coding a web page for a school project. This site is supposed to be a simple online store where people can order prints of artwork. The specific page I'm working on has a Drop Down List (ddlArt) that is bound to my database and displays a list of the different art pieces available. When the user selects one of the items, all the information on that item is pulled from the database and displayed on the page in a variety of labels and such. The only thing is that I'm getting a null reference exception error saying "Object reference not set to an instance of an object" when I go to try to run the page. I got the same error on a homework assignment earlier in the year and managed to get it fixed, but I can't remember what I did and I can't get help from school until next week, so I thought I'd try my luck on here. Here's my code:
Private selectedArt As Art
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
ddlArt.DataBind()
End If
selectedArt = Me.GetSelectedArt
lblArtID.Text = selectedArt.ArtID()
lblArtName.Text = selectedArt.ArtName()
lblCaption.Text = selectedArt.Caption()
lblDescription.Text = selectedArt.Description()
imgArt.ImageUrl = "~/images/" & selectedArt.FileName()
End Sub
Private Function GetSelectedArt() As Art
Dim artTable As DataView = CType(SqlDataSource1.Select(DataSourceSelectArguments.Empty), DataView)
artTable.RowFilter = "ArtID = '" & ddlArt.SelectedValue & "'"
Dim artRow As DataRowView = artTable(0)
Me.imgArt.ImageUrl = "~/images/" & artRow("FileName")
Dim art As New Art
art.ArtID = artRow("ArtID").ToString
art.ArtName = artRow("ArtName").ToString
art.Caption = artRow("Caption").ToString
art.Description = artRow("LongDescription").ToString
art.FileName = artRow("FileName").ToString
Return art
End Function
And here's the code for the Art class, in case anybody is interested:
Public Class Art
Public Property ArtID As Integer
Public Property ArtName As String
Public Property ArtType As String
Public Property Caption As String
Public Property FileName As String
Public Property Description As String
End Class
When I get the error, it highlights the artTable.RowFilter = "ArtID = '" & ddlArt.SelectedValue & "'" line in the GetSelectedArt function. I've tried comparing it to my corrected homework assignment that I mentioned, but I can't seem to find the problem. My VB is a little fuzzy because it's been awhile since I actually took the class. Any suggestions? Thanks a bunch!
If I understand your comment above correctly, on the initial page load there is nothing in the ddlArt, because the user must first choose an art type.
If that is correct, then your answer to my question is your answer.
For whatever reason (and without seeing at least the Select statement), artTbl is not getting instantiated, which is why you're seeing the Object reference not set to an instance of an object error.
One way to fix this (without knowledge of your SqlDataSource it's hard to give a precise answer) is to modify your Page Load method so that GetSelectedArt is only called when the user has selected an item from the drop down list. Right now GetSelectedArt is called every time the page loads.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
ddlArt.DataBind()
Else
selectedArt = Me.GetSelectedArt
lblArtID.Text = selectedArt.ArtID()
lblArtName.Text = selectedArt.ArtName()
lblCaption.Text = selectedArt.Caption()
lblDescription.Text = selectedArt.Description()
imgArt.ImageUrl = "~/images/" & selectedArt.FileName()
End If
End Sub
However, the above modification will only prevent GetSelectedArt from being called on the initial page load. If your SqlDataSource.Select command is still returning nothing, then you're still going to have this problem.
A better solution would be to call the GetSelectedArt on the ddlArt.SelectedIndexChanged event handler. This way you'll know that you have (or should have) a valid SelectedValue from ddlArt.
Also, if you don't populate the drop down until the user selects an art type from the radio button list, why are you binding the drop down list on the initial page load (and what are you binding it to)? Or is the drop down list and detail information on a different page from the radio button list?
May be .. with ArtID as integer
artTable.RowFilter = "ArtID = " & format(ddlArt.SelectedValue)

Why is My GridView FooterRow Referencing the Wrong Row?

I have a GridView and, using a fairly common method, I'm using a FooterRow and TemplateFields to provide the missing insert ability. So far so good.
The footer contains a TemplateField with a LinkButton to provide the postback that does the insertion. In the handler for the LinkButton's click, the Insert() method is called on the ObjectDataSource that the GridView is bound to. The ObjectDataSource's insert parameters are populated in the handler for its Inserting event. The code for all of this (abridged) looks like this:
Markup:
<asp:GridView ID="gvComplexRates" runat="server" AutoGenerateColumns="False"
DataKeyNames="id" DataSourceID="odsComplexMileageRates"
EnableModelValidation="True" ShowFooter="True">
<Columns>
<asp:TemplateField ShowHeader="False">
:
:
<FooterTemplate>
<asp:LinkButton ID="addLinkButton" runat="server" CausesValidation="false"
CommandName="Insert" Text="Add"></asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
:
:
</asp:GridView>
Code Behind:
Private Sub gvComplexRates_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvComplexRates.RowCommand
Select Case e.CommandName
Case "Insert"
odsComplexMileageRates.Insert()
End Select
End Sub
Private Sub odsComplexMileageRates_Inserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceMethodEventArgs) Handles odsComplexMileageRates.Inserting
Dim fuelTypeDropDown As DropDownList = gvComplexRates.FooterRow.FindControl("ddFuelTypeInsert")
Dim engineTypeDropDown As DropDownList = gvComplexRates.FooterRow.FindControl("ddEngineTypeInsert")
Dim rateTextBox As TextBox = gvComplexRates.FooterRow.FindControl("tbRateInsert")
Dim vatRateTextBox As TextBox = gvComplexRates.FooterRow.FindControl("tbVatRateInsert")
e.InputParameters("expense_type_id") = ddExpenseTypeSelect.SelectedValue
e.InputParameters("fuel_type_id") = fuelTypeDropDown.SelectedValue
e.InputParameters("engine_type_id") = engineTypeDropDown.SelectedValue
e.InputParameters("rate") = rateTextBox.Text
e.InputParameters("vat_rate") = vatRateTextBox.Text
End Sub
Two of the fields in my FooterRow are DropDownLists that are populated from other tables. Again this works fine and I can add, edit and remove rows without problem.
The problem comes when I use a modal dialog from this page to insert extra rows into the tables used to populate the DropDownLists in the FooterRow. The insert operations work fine and the modal dialog closes and at this point I use a javascript postback (basically a call to __doPostBack()) so that my FooterRow DropDownLists can be updated. The code for this is:
Protected Sub updateFuelEngineDropdowns()
odsFuelTypes.Select()
odsEngineTypes.Select()
Dim dropDown As DropDownList = gvComplexRates.FooterRow.FindControl("ddFuelTypeInsert")
dropDown.DataBind()
dropDown = gvComplexRates.FooterRow.FindControl("ddEngineTypeInsert")
dropDown.DataBind()
End Sub
This sub, updateFuelEngineDropdowns(), is called from the Page Load event. The first time I called it it worked fine. For some reason in subsequent runs through the debugger I'm getting NullReferenceExceptions. Digging into the debug object viewer it is apparent that the GridView FooterRow is referencing the row above the footer which contains no controls (at least not at this non-editing stage) and so, quite reasonably, gives my the Null reference.
The debug QuickView expressions I use are:
gvComplexRates.FooterRow.Controls(3)
DirectCast(gvComplexRates.FooterRow.Controls(3),System.Web.UI.WebControls.DataControlFieldCell).Controls(1)
The first of these shows a tag of td. Which makes sense. The second shows text of "10" which is the content for the row above the footer.
Does anybody know why this is happening?
Thanks Dan
Where are you "providing the missing insert ability"?
You have to rebuild the footer-controls on every postback, the GridView.RowCreated-Event would be a good place.
Update:
You have to Databind your GridView after you inserted new rows into your Dropdowns' Tables.
Right, this is a bit embarrassing. I gave a little white lie in the original question. By omission rather than a deliberate attempt to mislead. I am not, in fact, using a GridView but a subclass of it that will display rows when the datasource contains no data. This enables the user to insert new rows when the table is empty. This subclass overrides the FooterRow property and, to my shame, it was this that was getting things wrong. So I made two errors here: first I failed to test my GridView subclass properly and second I sought to prevent what I thought would be unnecessary attention on my subclass by not showing its use in the code snippets I included in the question. My bad. Thanks to Tim for taking the time to try and help me.
Dan

RangeValidator not working when selecting DropDownList value using jQuery

I inherited a site which I am in the middle of updating which has a DropDownList and a RangeValidator assigned to it. The data is bound to the dropdown on Page_Load. The values are retrieved from the database so the value property of each item is set to the unique ID of the item in the DB.
The RangeValidator looks something like:
<asp:rangevalidator id="ddRangeValidator" runat="server" ControlToValidate="ddMenu" ErrorMessage="Please select value in range" MinimumValue="1" MaximumValue="100000" Type="Integer">*</asp:rangevalidator>
I have a method which automatically populates this value in jQuery e.g.
$("#ddMenu").val("An Option");
This works, however, when I try to post the page the range validation fails. Then even if I manually select that value, or select another valid value it still won't validate. The only way to make it validate is to select non-valid value and then re-selecting a valid one.
Any ideas?
Update
Here is the data binding code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load, Me.Load
If Not Page.IsPostBack Then
Dim ds As New DataSet()
Dim myDbObject As New myDbObject()
ds = myDbObject.ToDataSet() // retrieves all objects from the database
// store the results in a temporary view to filter
Dim dv As DataView
dv = New DataView(ds.Tables(0), "IsLive = True", "ID", DataViewRowState.CurrentRows)
Dim i As Integer = 0
ddMenu.Items.Add("Select a value")
ddMenu.Items.Item(0).Value = 0
// add all objects from filtered list into drop down menu
For i = 0 To dv.Count - 1
With ddMenu.Items
// add a new item
.Add(dv.Item(i).Item("Name"))
// set the Value property as unique ID of object
.Item(i + 1).Value = dv.Item(i).Item("ID")
End With
Next
End If
End If
Turns out it was an issue with my page loading twice...so now the issue is going to be tracking down why it is loading twice!
I guess the Range validator verifies the actual "value" of the dropdown list. May it be possible that by using jQuery to populate your dropdown the actual entry doesn't get a proper option "value"? Could you maybe post the rendered HTML code of your dropdown list?

How do I Prevent FormView from clearing user's entered Values after Insert Method has fired?

I have been struggling with getting FormViews to work the way Microsoft expects me to for about a day and have figure a bunch of great stuff out.
I can catch e.Exception and e.ReturnValue in the ObjectDataSource.Inserting Event Handler and I can even cheat and check other properties of the Object in the ObjectDataSource.ObjectDisposing by checking the e.ObjectInstance ... and I even learned that FormView's Inserting Handler Runs AFTER the ObjectDisposing Handler so If there is a problem found I still have time to react to it and st the e.KeepInInsertMode to true on the FormView.
My problem is, it seems that the values entered by the user into the Insert form are cleared regardless.
So, How do I Prevent a FormView from clearing after it's Insert Method has fired?
(Using ASP.NET + VB)
I don't think posting my code here will really do much good and i would have to modify it to trim out confidential business logic stuff... so I'll skip it for now.
edit:
I have found a temporary and admittedly terribly cludgy solution (in case no one ever finds a REAL solution to the problem).
I have a page variable defined as:
Dim eInsertArgs As FormViewInsertedEventArgs
And then I do the following in my ItemInserted handler
If boolInsertErrorOccurred = False Then
e.KeepInInsertMode = True
eInsertArgs = e
Else
eInsertArgs = Nothing
End If
Then on each of the controls I have something like this in that controls databinding event:
If IsNothing(eInsertArgs) = False Then
Dim _sender As TextBox = sender
_sender.Text = eInsertArgs.Values("_FieldName")
End If
The effect of this is that I am setting the values BACK to the submitted values AFTER ASP.NET binds the FormView to the default (blank) Template.
Please help me find a less terrible solution. :)
You need to create your own server control which inherits from the FormView control.
Public Class MyFormView
Inherits FormView
Protected Overrides Sub OnDataSourceViewChanged(ByVal sender As Object,
ByVal e As EventArgs)
If (MyBase.CurrentMode = FormViewMode.Insert) Then
MyBase.RequiresDataBinding = False
Else
MyBase.OnDataSourceViewChanged(sender, e)
End If
End Sub
End Class
Please take a look at this page: http://www.dotnetmonster.com/Uwe/Forum.aspx/asp-net/76885/FormView-Insert

Inheriting DropDownList and adding custom values using its DataSourceObject

I'm inheriting a DropDownList to add two custom ListItems. The first item is "Select one..." and the second item gets added at the end, it's value is "Custom".
I override DataBind and use the following code:
Dim data As List(Of ListItem) = CType(DataSource, List(Of ListItem))
data.Insert(0, New ListItem("Select one...", SelectOneListItemValue))
If DisplayCustomOption Then
data.Insert(data.Count, New ListItem("Custom", CustomListItemValue))
End If
DataSource = data
MyBase.DataBind()
The problem is this code won't work if the DataSource is anything other than a List of ListItem. Is there a better way of doing this?
You could make the assumption that the data source always inherits IList, in which case you could do the following:
Dim data As IList = CType(DataSource, IList)
data.Insert(0, New ListItem("Select one...", SelectOneListItemValue))
' And so on...
Of course, this assumes that whatever the data source is, it allows you to add objects of the type ListItem. But this might be generic enough for what you need.
You could just leave the databind alone and add your special Items in the DataBound event handler.
Protected Sub MyDropDownList_DataBound(sender As Object, e As EventArgs) _
Handles MyDropDownList.DataBound
MyBase.Items.Insert(0, New ListItem("Select One...", SelectOneListItemValue))
If DisplayCustomOption Then
MyBase.Add(New ListItem("Custom", Custom))
End If
End Sub
(My VB.NET is limited, so you may need to adjust syntax)

Resources