Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dropdownlist in a datagrid - error 1

Status
Not open for further replies.

slwolf8

Technical User
Apr 23, 2001
82
US
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>&nbsp;
<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>
<asp:DropDownList ID="ddlVendor" Runat="server" DataSource="<%# DACmp.dsBudget %>" DataMember="spSelVendors" DataTextField="vendor" DataValueField="vendorid">
</asp:DropDownList>
</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!!
 
Instead of:

Code:
If Not drv Is Nothing Then
   ddlVendor.Items.FindByValue(drv("vendorid")).Selected = True
End If

Try:

Code:
If Not drv Is Nothing Then
   ListItem li =ddlVendor.Items.FindByValue(drv("vendorid"))

   If Not li Is Nothing Then
       li.Selected = True
   End If
End If

It must not be finding the value you're looking for in the DropDownList.
 
hey thanks!!! that worked! now unfortunately my ddl comes up empty, but at least there's no error! thanks!
 
No problem. If you think the value you're searching for actually exists, make sure it's bound properly to the value field of the DropDownList.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top