473,327 Members | 2,065 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

problem in binding child datagrid

15
Hi

I am receiving Object reference not set to an instance of an Object when binding Child dataGrid in the ItemCommandEvent 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

Thanks

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>
  5.  
  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"));
  9.  
  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"));
  19.  
  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+"%' ";
  30.  
  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. }
  53.  
  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

✓ answered by tlhintoq

"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.

1 2213
tlhintoq
3,525 Expert 2GB
"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

3
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...
2
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...
5
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...
0
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...
1
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: ...
8
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...
2
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...
3
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...
8
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...
2
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.