I am trying to add a dropdownlist to a datagrid. I am using code that I got from the learnvisualstudio.net videos. I am getting the following error when I click the edit linkbutton:
Object reference not set to an instance of an object.
It points to:
ddlVendor.Items.FindByValue(drv("vendorid")).Selected = True
Which is a part of the following method:
Private Sub dgInvoices_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgInvoices.ItemDataBound
Dim lit As ListItemType = e.Item.ItemType
If lit = ListItemType.EditItem Then
Dim ddlVendor As DropDownList
ddlVendor = e.Item.FindControl("ddlVendor")
Dim drv As DataRowView = e.Item.DataItem
If Not drv Is Nothing Then
ddlVendor.Items.FindByValue(drv("vendorid")).Selected = True
End If
End If
End Sub
All of my data adapters, my dataset, and my sqlconn are in a component named cmpITData.vb, and my project name is Recipe4. My datagrid looks like the following:
<asp:datagrid id="dgInvoices" runat="server" BorderColor="#DEBA84" BorderStyle="None" CellSpacing="2"
BorderWidth="1px" BackColor="#DEBA84" CellPadding="3" AutoGenerateColumns="False" Font-Names="Arial"
Font-Size="X-Small" AllowSorting="True" ShowFooter="True">
<SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#738A9C"></SelectedItemStyle>
<ItemStyle ForeColor="#8C4510" BackColor="#FFF7E7"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#A55129"></HeaderStyle>
<FooterStyle ForeColor="#8C4510" BackColor="#F7DFB5"></FooterStyle>
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton runat="server" Text="Edit" CommandName="Edit" CausesValidation="false"></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton runat="server" Text="Update" CommandName="Update"></asp:LinkButton>
<asp:LinkButton runat="server" Text="Cancel" CommandName="Cancel" CausesValidation="false"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton runat="server" Text="Delete" CommandName="Delete" CausesValidation="false"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="invoice no">
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.invoiceno") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEditInvoice" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.invoiceno") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="vendor">
<ItemTemplate>
<asp:Label runat="server" Text='<%# BindJob(Container.DataItem) %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<aspropDownList ID="ddlVendor" Runat="server" DataSource="<%# DACmp.dsBudget %>" DataMember="spSelVendors" DataTextField="vendor" DataValueField="vendorid">
</aspropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle HorizontalAlign="Center" ForeColor="#8C4510" Mode="NumericPages"></PagerStyle>
</asp:datagrid>
Any help would be so greatly appreciated, it's driving me mad!! Thanks!!
Object reference not set to an instance of an object.
It points to:
ddlVendor.Items.FindByValue(drv("vendorid")).Selected = True
Which is a part of the following method:
Private Sub dgInvoices_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgInvoices.ItemDataBound
Dim lit As ListItemType = e.Item.ItemType
If lit = ListItemType.EditItem Then
Dim ddlVendor As DropDownList
ddlVendor = e.Item.FindControl("ddlVendor")
Dim drv As DataRowView = e.Item.DataItem
If Not drv Is Nothing Then
ddlVendor.Items.FindByValue(drv("vendorid")).Selected = True
End If
End If
End Sub
All of my data adapters, my dataset, and my sqlconn are in a component named cmpITData.vb, and my project name is Recipe4. My datagrid looks like the following:
<asp:datagrid id="dgInvoices" runat="server" BorderColor="#DEBA84" BorderStyle="None" CellSpacing="2"
BorderWidth="1px" BackColor="#DEBA84" CellPadding="3" AutoGenerateColumns="False" Font-Names="Arial"
Font-Size="X-Small" AllowSorting="True" ShowFooter="True">
<SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#738A9C"></SelectedItemStyle>
<ItemStyle ForeColor="#8C4510" BackColor="#FFF7E7"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#A55129"></HeaderStyle>
<FooterStyle ForeColor="#8C4510" BackColor="#F7DFB5"></FooterStyle>
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton runat="server" Text="Edit" CommandName="Edit" CausesValidation="false"></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton runat="server" Text="Update" CommandName="Update"></asp:LinkButton>
<asp:LinkButton runat="server" Text="Cancel" CommandName="Cancel" CausesValidation="false"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton runat="server" Text="Delete" CommandName="Delete" CausesValidation="false"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="invoice no">
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.invoiceno") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEditInvoice" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.invoiceno") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="vendor">
<ItemTemplate>
<asp:Label runat="server" Text='<%# BindJob(Container.DataItem) %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<aspropDownList ID="ddlVendor" Runat="server" DataSource="<%# DACmp.dsBudget %>" DataMember="spSelVendors" DataTextField="vendor" DataValueField="vendorid">
</aspropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle HorizontalAlign="Center" ForeColor="#8C4510" Mode="NumericPages"></PagerStyle>
</asp:datagrid>
Any help would be so greatly appreciated, it's driving me mad!! Thanks!!