473,888 Members | 1,599 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem in binding child datagrid

15 New Member

I am receiving Object reference not set to an instance of an Object when binding Child dataGrid in the ItemCommandEven t of Parent dataGrid.

Can someone please guide me what is wrong in the code below?

I checked the table size and it is 2. Please advise


Expand|Select|Wrap|Line Numbers
  1. <asp:datagrid id="DataGrid1" runat="server" Height="176px" AutoGenerateColumns="False">
  2. <Columns>
  3.    <asp:BoundColumn Visible="False" DataField="eveid" HeaderText="EventId"></asp:BoundColumn>
  4.    <asp:BoundColumn Visible="False" DataField="conid" HeaderText="ContactId"></asp:BoundColumn>
  6. <asp:TemplateColumn HeaderText="Source">
  7.   <ItemTemplate>
  8.     <asp:DropDownList id="Dropdownlist2" runat="server" >
  9.                             <asp:ListItem Value="--Select--" Selected="True">--Select--</asp:ListItem>
  10.                                 <asp:ListItem Value="Web">Web</asp:ListItem>
  11.                                 <asp:ListItem Value="Email">Email</asp:ListItem>
  12.                                 <asp:ListItem Value="Phone">Phone</asp:ListItem>
  13.                                 <asp:ListItem Value="Other">Other</asp:ListItem>
  14.                             </asp:DropDownList>
  15.                         </ItemTemplate>
  16. </asp:TemplateColumn>
  17. <asp:TemplateColumn HeaderText="Detail">
  18. <ItemTemplate>
  19. <P>                    
  20. <asp:TextBox id="TextBox2" runat="server"></asp:TextBox>
  21. <asp:TextBox id="Textbox1" runat="server" Visible="False"></asp:TextBox>
  22. <asp:Button id="Button2" runat="server" Text="Search" CommandName="Search"></asp:Button></P>
  23. <P>
  24. <asp:Label id="Label13" runat="server" Visible="False" ForeColor="Red">Label</asp:Label></P>
  25. </ItemTemplate>
  26. <EditItemTemplate>
  27. <asp:DataGrid id="DataGrid2" runat="server"></asp:DataGrid>
  28. </EditItemTemplate>
  29. </asp:TemplateColumn>
  30. </Columns>
  31. </asp:datagrid>

Expand|Select|Wrap|Line Numbers
  1. private void ItemsGrid_Command(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
  2. {
  3. DataTable dt=new DataTable();
  4. DataRow dr;
  5. dt.Columns.Add("AcId",Type.GetType("System.String"));
  6. dt.Columns.Add("AcName",Type.GetType("System.String"));
  7. dt.Columns.Add("AcAddress",Type.GetType("System.String"));
  8. dt.Columns.Add("AcOwner",Type.GetType("System.String"));
  10. switch(e.CommandName)
  11. {
  12. case "Search":
  13. try
  14. {    
  15. TextBox txt=(TextBox)(e.Item.Cells[8].FindControl("TextBox2"));
  16. DropDownList ddl=(DropDownList)(e.Item.Cells[5].FindControl("Dropdownlist2"));
  17. Label lbl=(Label)(e.Item.Cells[8].FindControl("Label13"));
  18. DataGrid dg2=(DataGrid)(e.Item.Cells[5].FindControl("DataGrid2"));
  20. if(ddl.SelectedValue == "" || ddl.SelectedValue == "--Select--")
  21. {
  22. lbl.Text="Please select Source";
  23. lbl.Visible=true;
  24. }
  25. else
  26. {
  27. if(login())
  28.  {
  29. string Acct_query="Select Id,Name,BillingStreet,Billingcity,BillingState,BillingPostalCode,Owner.Name from Account where RECORDTYPEID='0123000000005QB' and Ownerid='"+Session["UserID"].ToString()+"' and name like '%"+ddl.SelectedValue+"%' ";
  31. QueryResult qr = binding.query(Acct_query); 
  32. sObject[] records = qr.records;
  33. if(qr.size>0)
  34. {
  35.                               for (int i=0; i<records.Length; i++) 
  36. {
  37. Account acc = (Account)records[i];
  38. dr=dt.NewRow();
  39. dr["AcId"]=acc.Id;
  40. dr["AcName"]=acc.Name;
  41. dr["AcAddress"]=acc.BillingStreet;
  42. dr["AcOwner"]=acc.Owner.Name;
  43. dt.Rows.Add(dr);
  44. }
  45. dg2.DataSource=dt;
  46. dg2.DataBind();
  47. }
  48. else
  49. {
  50. }
  51.  }
  52. }
  54. }
  55. catch(Exception ex)
  56. {
  57. Response.Write(ex.Message.ToString());
  58. }
  59. break;
  60. default:
  61. break;
  62. }
  63. }
Mar 12 '10 #1
1 2235
3,525 Recognized Expert Specialist
"How do I fix a 'object reference not set to an instance of an object' error?
The line your code stopped on while debugging holds all your answers.
One of the variables/objects was created but not initialized. For example:
Expand|Select|Wrap|Line Numbers
  1. string TempString;// Created but not initialized so this is still null
  2. //versus
  3. string TempString = string.empty;
Debug your project again. This time, when it breaks on a line look at the Locals pallet to see which variable/object is null. You can also hover your mouse over each variable and the hothelp will shows its value. One of them will be null.
Mar 12 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

by: Dinesh_GR | last post by:
Hi all, I have many parent tags and many child tags under the respective parent.. in an XML file. On the click of a button the application should pick up the one parent and the corresponding child and bind it to a grid. Like that it goes on for the next parent and the related child for the next button click.
by: Pranav Shah | last post by:
I have a windows form that uses a dataset with two DataTables. The Master table values for each row are displayed in textboxes. The Detail are shown in a DataGrid. They are related through a DataRealation based on OrderID. My problem is that when new values are inserted into the DataGrid I need to insert the OrdereID value automatically, I am not sure how??
by: Jeff | last post by:
IDE: VS 2003 :NET OS: XP Pro My app have a form with a tab-control on it. The tab-control have 2 tabpages. One of the tabpages displays a datagrid, and the other tabpage displays details (order date, name, address etc) about the selected row in the datagrid... My problem is when I enter a new record in the details tabpage (saves data to database), and go back to the datagrid. Only the data from the PM-table
by: mike | last post by:
Hi there: I've read an excellent "how to"-article by Microsoft (no. 306227) - partly cited cited at the end of this email). I have implemented the code related to the part "How to Add a CheckBox Programmatically" in my code. I have changed it to use a OLDDB.DBReader to populate the datagrid and for the databinding. It works perfectly - also when using the edit-mode in the datagrid.
by: Demetri | last post by:
Someone posted the following back in June and I am now doing the same thing. I'll just paste what was asked and see if anyone can give us an answer: --------------------------------------------------------------------------------- I have a custom collection of objects, each of which includes a child object called MyUserOpener. In declarative binding, I can bind this property to a label in a template control with the following syntax: ...
by: Richard L Rosenheim | last post by:
I have a dataset containing a parent table related to a child table. The child table contains an ID field (which is configured as autonumber in the datatable), the ID of the parent, plus some addition fields. I'm able to get the datagrid to be properly populate with only the child records relating to the specified parent by setting the ..DefaultView.RowFilter property. Currently, to properly add a new child record, I need to manually...
by: yuanh23 | last post by:
Hi, I have 3 talbes "customers","orders","details". i wanna to use 3 datagrids to show those tables. and when the selected row in parent talbe changes, the selected row in child table changes automaticly. If there is only 2 tables, that's quite easy. add the relation "CusVsOrder" to dataset. binding the "patient" with the parent datagrid, binding the relation "customers.CusVsOrder" with the child datagrid. while, now there are 3 tables,...
by: no | last post by:
Hi all, I have a dataset that contain 2 tables and a relationship between them (master detail). I bind this dataset to a form that include some textboxes that bind to the parent record, and a datagrid that is binded to the detailed records. Here is the code: Code:
by: BD | last post by:
Moving from MS Access to .Net using C# languag. Problem trying to duplicate form with structure as follows: 1. Main form 'Work Order' displayed as textboxes 2. subform 'Customers' textboxes showing name, address, etc related by 'CustomerID' 3. subform 'Locations' textboxes showing name, address, etc related by 'LocationID' 4. subform 'Material' on tabbed interface datasheet related by 'WorkOrderID' 5. subform 'Labor' on tabbed...
by: =?Utf-8?B?Y3JlYXZlczA2MjI=?= | last post by:
I have a nested datagrid in a xaml file, the parent datagrid loads the vendor information and the details loads the documents for that vendor in a datagrid. Everything is working fine until I click in the child datagrid and then go to the combobox and choose another vendor. When the new vendor is loaded nothing shows in the datagrid but the itemsource shows the info is there. Know if I click on the child cell and then click back on the...
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.