Grid problem
-
Hello, I am trying to create a ItemTemplate at runtime for a ComponentArt Grid. This GridView works in a very similar way to the Asp.Net 2.0 GridView so the implementation method should be the same. When I don't use the ItemTemplate everything works fine. However, when I use the ItemTemplate I get the following error: "Exception Details: System.NullReferenceException: Object reference not set to an instance of an object." On the code line lCollaborator.Text = tcCollaborator.DataItem("Name").ToString & tcCollaborator.DataItem("City").ToString I think my problem is really the binding method. Could someone please tell me what might be wrong? I also tried using the Asp.Net 2.0 GridView and I am getting the same problems. I post my VB.NET code. Please, fell free to answer in VB.NET or C#. GRID.aspx.vb 1 Partial Class Grid 2 Inherits System.Web.UI.Page 3 4 ' -- [Controls] ------------------------------------------- 5 6 Protected WithEvents cagCollaborators As New ComponentArt.Web.UI.Grid 7 8 9 ' -- [Events and Methods] ------------------------------------------- 10 11 ' Page_Init 12 Protected Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Init 13 14 phGrid.Controls.Add(cagCollaborators) 15 16 End Sub ' Page_Init 17 18 ' Page_Load 19 Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load 20 21 ' Create grid server template 22 Dim cagstCollaborators As ComponentArt.Web.UI.GridServerTemplate = New ComponentArt.Web.UI.GridServerTemplate() 23 24 ' Create grid template 25 Dim gitCollaborators As GridITemplate = New GridITemplate 26 cagstCollaborators.Template = gitCollaborators 27 cagstCollaborators.ID = "cagstCollaborators" 28 29 ' Add grid server template to grid 30 cagCollaborators.ServerTemplates.Add(cagstCollaborators) 31 32 ' Create grid column 33 Dim gcCollaborators As ComponentArt.Web.UI.GridColumn = New ComponentArt.Web.UI.GridColumn 34 gcCollaborators.DataCellServerTemplateId = "cagstCollaborators" 35 36 ' Create grid level 37 Dim glCollaborators As New ComponentArt.Web.UI.GridLevel 38 glCollaborators.Columns.Add(gcCollaborators) 39 40 ' Add grid level to grid 41 cagCollaborators.Levels.Add(glCollaborators) 42 43 ' Bind grid 44 cagCollaborators.DataBind() 45 46 End Sub ' Page_Load 47 48 ' {Grid} ... 49 50 ' cagCollaborators_Init 51 Private Sub cagCollaborators_Init(ByVal sender As Object, ByVal
-
Hello, I am trying to create a ItemTemplate at runtime for a ComponentArt Grid. This GridView works in a very similar way to the Asp.Net 2.0 GridView so the implementation method should be the same. When I don't use the ItemTemplate everything works fine. However, when I use the ItemTemplate I get the following error: "Exception Details: System.NullReferenceException: Object reference not set to an instance of an object." On the code line lCollaborator.Text = tcCollaborator.DataItem("Name").ToString & tcCollaborator.DataItem("City").ToString I think my problem is really the binding method. Could someone please tell me what might be wrong? I also tried using the Asp.Net 2.0 GridView and I am getting the same problems. I post my VB.NET code. Please, fell free to answer in VB.NET or C#. GRID.aspx.vb 1 Partial Class Grid 2 Inherits System.Web.UI.Page 3 4 ' -- [Controls] ------------------------------------------- 5 6 Protected WithEvents cagCollaborators As New ComponentArt.Web.UI.Grid 7 8 9 ' -- [Events and Methods] ------------------------------------------- 10 11 ' Page_Init 12 Protected Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Init 13 14 phGrid.Controls.Add(cagCollaborators) 15 16 End Sub ' Page_Init 17 18 ' Page_Load 19 Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load 20 21 ' Create grid server template 22 Dim cagstCollaborators As ComponentArt.Web.UI.GridServerTemplate = New ComponentArt.Web.UI.GridServerTemplate() 23 24 ' Create grid template 25 Dim gitCollaborators As GridITemplate = New GridITemplate 26 cagstCollaborators.Template = gitCollaborators 27 cagstCollaborators.ID = "cagstCollaborators" 28 29 ' Add grid server template to grid 30 cagCollaborators.ServerTemplates.Add(cagstCollaborators) 31 32 ' Create grid column 33 Dim gcCollaborators As ComponentArt.Web.UI.GridColumn = New ComponentArt.Web.UI.GridColumn 34 gcCollaborators.DataCellServerTemplateId = "cagstCollaborators" 35 36 ' Create grid level 37 Dim glCollaborators As New ComponentArt.Web.UI.GridLevel 38 glCollaborators.Columns.Add(gcCollaborators) 39 40 ' Add grid level to grid 41 cagCollaborators.Levels.Add(glCollaborators) 42 43 ' Bind grid 44 cagCollaborators.DataBind() 45 46 End Sub ' Page_Load 47 48 ' {Grid} ... 49 50 ' cagCollaborators_Init 51 Private Sub cagCollaborators_Init(ByVal sender As Object, ByVal
-
IMO, it should be easier if you tell which line is causing the NullReferenceException, and I guess that you can debug to see which object that you use is null.
Hi, sure. I just debugged and I get an error in the following line: lCollaborator.Text = "Name: " & tcCollaborator.DataItem("Name").ToString I though it was because of tcCollaborator.DataItem("Name").ToString But now I replaced this line by the following and I don't get any error: Dim a As String = tcCollaborator.DataItem("Name") Of course I don't see anything in my grid. Now I am confused. :-( Any idea? Thanks, Miguel
-
Hi, sure. I just debugged and I get an error in the following line: lCollaborator.Text = "Name: " & tcCollaborator.DataItem("Name").ToString I though it was because of tcCollaborator.DataItem("Name").ToString But now I replaced this line by the following and I don't get any error: Dim a As String = tcCollaborator.DataItem("Name") Of course I don't see anything in my grid. Now I am confused. :-( Any idea? Thanks, Miguel
Okay, now it's much clearer and I guess I know the reason :). Here, it's clear that the tcCollaborator.DataItem("Name") object is null, and that throws the exception when you use the ToString method of the null object. The reason this object is null is that you are trying to get the data object bound to the container using the DataItem property, and basically it only has value after data is bound to the control, but here you put your code in the InstantiateIn method which occurs before the data binding happens. So you can use the DataBinding event of the lCollaborator object and put your code to query the data item and set the Text property in the event handler
-
Okay, now it's much clearer and I guess I know the reason :). Here, it's clear that the tcCollaborator.DataItem("Name") object is null, and that throws the exception when you use the ToString method of the null object. The reason this object is null is that you are trying to get the data object bound to the container using the DataItem property, and basically it only has value after data is bound to the control, but here you put your code in the InstantiateIn method which occurs before the data binding happens. So you can use the DataBinding event of the lCollaborator object and put your code to query the data item and set the Text property in the event handler
Hi, I read carefully what you wrote and I got the same error after making a few changes to my code. This is what I did: ' InstantiateIn Public Sub InstantiateIn(ByVal container As Control) Implements ITemplate.InstantiateIn ' Define template container Dim tcCollaborator As ComponentArt.Web.UI.GridServerTemplateContainer = CType(container, ComponentArt.Web.UI.GridServerTemplateContainer) ' Add lCollaborator data binding handler AddHandler lCollaborator.DataBinding, AddressOf lCollaborator_DataBinding ' Add item template child controls container.Controls.Add(lCollaborator) End Sub ' InstantiateIn ' lCollaborator_DataBinding Private Sub lCollaborator_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs) ' Create and define lCollaborator Dim lCollaborator As Label = CType(sender, Label) ' Create and define lNews container Dim container As ComponentArt.Web.UI.GridServerTemplateContainer = CType(lCollaborator.NamingContainer, ComponentArt.Web.UI.GridServerTemplateContainer) ' Define lCollaborator text lCollaborator.Text = "Name: " & container.DataItem("Name").ToString End Sub ' lCollaborator_DataBinding Any idea? I am completely out of ideas ... Thanks, Miguel
-
Hi, I read carefully what you wrote and I got the same error after making a few changes to my code. This is what I did: ' InstantiateIn Public Sub InstantiateIn(ByVal container As Control) Implements ITemplate.InstantiateIn ' Define template container Dim tcCollaborator As ComponentArt.Web.UI.GridServerTemplateContainer = CType(container, ComponentArt.Web.UI.GridServerTemplateContainer) ' Add lCollaborator data binding handler AddHandler lCollaborator.DataBinding, AddressOf lCollaborator_DataBinding ' Add item template child controls container.Controls.Add(lCollaborator) End Sub ' InstantiateIn ' lCollaborator_DataBinding Private Sub lCollaborator_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs) ' Create and define lCollaborator Dim lCollaborator As Label = CType(sender, Label) ' Create and define lNews container Dim container As ComponentArt.Web.UI.GridServerTemplateContainer = CType(lCollaborator.NamingContainer, ComponentArt.Web.UI.GridServerTemplateContainer) ' Define lCollaborator text lCollaborator.Text = "Name: " & container.DataItem("Name").ToString End Sub ' lCollaborator_DataBinding Any idea? I am completely out of ideas ... Thanks, Miguel
-
Now, you might want to debug to see what the container.DataItem("Name") contains. Does it really have any column/property named "Name"?
Hi, To be honest I am not able to debug and see what is inside container.DataItem("Name"). However, in the code I posted I have the function which creates the DataTable: Public Shared Function Collaborators() As DataTable ' Create collaborators data table Dim dtCollaborators As New DataTable ' Add columns to collaborators data table With dtCollaborators.Columns .Add(New DataColumn("Name", GetType(String))) .Add(New DataColumn("Mobile", GetType(String))) .Add(New DataColumn("Email", GetType(String))) .Add(New DataColumn("City", GetType(String))) End With ' Create and add a new collaborator row Dim drRow01 As DataRow drRow01 = dtCollaborators.NewRow ' Define collaborator row values drRow01("Name") = "John" drRow01("Mobile") = "983498223" drRow01("Email") = "john@mydomain.com" drRow01("City") = "New York" ' Add row to collaborators data table dtCollaborators.Rows.Add(drRow01) ' Create and add a new collaborator row Dim drRow02 As DataRow drRow02 = dtCollaborators.NewRow ' Define collaborator row values drRow02("Name") = "Andrew" drRow02("Mobile") = "983498223" drRow02("Email") = "andrew@mydomain.com" drRow02("City") = "Paris" ' Add row to collaborators data table dtCollaborators.Rows.Add(drRow02) ' Return collaborators data table Return dtCollaborators End Function ' Collaborators It seems ok, right? What should I do? How can I see what is inside container.DataItem("Name") as you mentioned? Thank You, Miguel
-
Hi, To be honest I am not able to debug and see what is inside container.DataItem("Name"). However, in the code I posted I have the function which creates the DataTable: Public Shared Function Collaborators() As DataTable ' Create collaborators data table Dim dtCollaborators As New DataTable ' Add columns to collaborators data table With dtCollaborators.Columns .Add(New DataColumn("Name", GetType(String))) .Add(New DataColumn("Mobile", GetType(String))) .Add(New DataColumn("Email", GetType(String))) .Add(New DataColumn("City", GetType(String))) End With ' Create and add a new collaborator row Dim drRow01 As DataRow drRow01 = dtCollaborators.NewRow ' Define collaborator row values drRow01("Name") = "John" drRow01("Mobile") = "983498223" drRow01("Email") = "john@mydomain.com" drRow01("City") = "New York" ' Add row to collaborators data table dtCollaborators.Rows.Add(drRow01) ' Create and add a new collaborator row Dim drRow02 As DataRow drRow02 = dtCollaborators.NewRow ' Define collaborator row values drRow02("Name") = "Andrew" drRow02("Mobile") = "983498223" drRow02("Email") = "andrew@mydomain.com" drRow02("City") = "Paris" ' Add row to collaborators data table dtCollaborators.Rows.Add(drRow02) ' Return collaborators data table Return dtCollaborators End Function ' Collaborators It seems ok, right? What should I do? How can I see what is inside container.DataItem("Name") as you mentioned? Thank You, Miguel
Just curious why you cannot debug the code? Anyway, you need to write some information to the web page ( simply using the Response object) or a log file to investigate which type of the container.DataItem object. Is it the DataRowView? Can you then also try to cast something like CType(container.DataItem, DataRowView)("Name").ToString()?