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
- <asp:datagrid id="DataGrid1" runat="server" Height="176px" AutoGenerateColumns="False">
- <Columns>
- <asp:BoundColumn Visible="False" DataField="eveid" HeaderText="EventId"></asp:BoundColumn>
- <asp:BoundColumn Visible="False" DataField="conid" HeaderText="ContactId"></asp:BoundColumn>
- <asp:TemplateColumn HeaderText="Source">
- <ItemTemplate>
- <asp:DropDownList id="Dropdownlist2" runat="server" >
- <asp:ListItem Value="--Select--" Selected="True">--Select--</asp:ListItem>
- <asp:ListItem Value="Web">Web</asp:ListItem>
- <asp:ListItem Value="Email">Email</asp:ListItem>
- <asp:ListItem Value="Phone">Phone</asp:ListItem>
- <asp:ListItem Value="Other">Other</asp:ListItem>
- </asp:DropDownList>
- </ItemTemplate>
- </asp:TemplateColumn>
- <asp:TemplateColumn HeaderText="Detail">
- <ItemTemplate>
- <P>
- <asp:TextBox id="TextBox2" runat="server"></asp:TextBox>
- <asp:TextBox id="Textbox1" runat="server" Visible="False"></asp:TextBox>
- <asp:Button id="Button2" runat="server" Text="Search" CommandName="Search"></asp:Button></P>
- <P>
- <asp:Label id="Label13" runat="server" Visible="False" ForeColor="Red">Label</asp:Label></P>
- </ItemTemplate>
- <EditItemTemplate>
- <asp:DataGrid id="DataGrid2" runat="server"></asp:DataGrid>
- </EditItemTemplate>
- </asp:TemplateColumn>
- </Columns>
- </asp:datagrid>
Expand|Select|Wrap|Line Numbers
- private void ItemsGrid_Command(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
- {
- DataTable dt=new DataTable();
- DataRow dr;
- dt.Columns.Add("AcId",Type.GetType("System.String"));
- dt.Columns.Add("AcName",Type.GetType("System.String"));
- dt.Columns.Add("AcAddress",Type.GetType("System.String"));
- dt.Columns.Add("AcOwner",Type.GetType("System.String"));
- switch(e.CommandName)
- {
- case "Search":
- try
- {
- TextBox txt=(TextBox)(e.Item.Cells[8].FindControl("TextBox2"));
- DropDownList ddl=(DropDownList)(e.Item.Cells[5].FindControl("Dropdownlist2"));
- Label lbl=(Label)(e.Item.Cells[8].FindControl("Label13"));
- DataGrid dg2=(DataGrid)(e.Item.Cells[5].FindControl("DataGrid2"));
- if(ddl.SelectedValue == "" || ddl.SelectedValue == "--Select--")
- {
- lbl.Text="Please select Source";
- lbl.Visible=true;
- }
- else
- {
- if(login())
- {
- 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+"%' ";
- QueryResult qr = binding.query(Acct_query);
- sObject[] records = qr.records;
- if(qr.size>0)
- {
- for (int i=0; i<records.Length; i++)
- {
- Account acc = (Account)records[i];
- dr=dt.NewRow();
- dr["AcId"]=acc.Id;
- dr["AcName"]=acc.Name;
- dr["AcAddress"]=acc.BillingStreet;
- dr["AcOwner"]=acc.Owner.Name;
- dt.Rows.Add(dr);
- }
- dg2.DataSource=dt;
- dg2.DataBind();
- }
- else
- {
- }
- }
- }
- }
- catch(Exception ex)
- {
- Response.Write(ex.Message.ToString());
- }
- break;
- default:
- break;
- }
- }