0

I am having a lot of trouble getting a dropdown to bind with data from my database with the appropriate departments.

This is what I have so far:

HTML:

<asp:GridView ID="gridDepartmentHistory" runat="server" AutoGenerateColumns="False" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True">
            <Columns>
                <asp:TemplateField HeaderText="Department">
                    <ItemTemplate>
                        <asp:Label ID="lblDepartment" runat="server" Visible="true" Text='<%# Eval("Department")%>'></asp:Label>
                        <asp:DropDownList ID="ddlDepartment" runat="server">
                        </asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="Start Date" HeaderText="Start Date" />
                <asp:BoundField DataField="End Date" HeaderText="End Date" />
            </Columns>
        </asp:GridView>

Code behind:

    Protected Sub gridDepartmentHistory_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gridDepartmentHistory.RowDataBound
    If (e.Row.RowType = DataControlRowType.DataRow) Then
        Dim ddlDepartment As DropDownList = CType(e.Row.FindControl("ddlDepartment"), DropDownList)
        Dim list As ICollection(Of Department) = Department.hrGetDepartmentList() 'Class method to fill a collection of items with the Department's Name and ID
        ddlDepartment.DataSource = list
        ddlDepartment.DataTextField = "Name"
        ddlDepartment.DataValueField = "ID"
        ddlDepartment.DataBind()
        Dim dept As String = CType(e.Row.FindControl("lblDepartment"), Label).Text
        ddlDepartment.Items.FindByText(dept).Selected = True
    End If
End Sub

When I run this it throws an exception saying:

Object reference not set to an instance of an object.

BTW: I am using this tutorial to help me through: http://www.aspsnippets.com/Articles/How-to-populate-DropDownList-in-GridView-in-ASPNet.aspx

Any help would be greatly appreciated! Thank you!

  • On which line you are getting this error? Is `hrGetDepartmentList` a `static` method or rather `Shared` in VB.Net? Also, please check: http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it – Rahul Singh Nov 28 '14 at 13:26
  • It is shared. I have filled dropdowns all across my page with that method. But it's in the gridview that I am having a lot of issues getting it to fill. – Giusepe Moreno Nov 28 '14 at 13:42
  • What is dept? FindByText(dept) ? – ALOK Nov 28 '14 at 13:45
  • The error is thrown when I'm actually binding the data to the datagrid (not included in the post), but it is due to the gridDepartmentHistory.RowDataBound procedure because if I remove that code the gridview binds just fine. – Giusepe Moreno Nov 28 '14 at 13:49
  • @GiusepeMoreno - Okay, thn plz check the last line..rest of the code looks fine, last line should be `ddlDepartment.Items.FindByText(ddlDepartment).Selected = True ` – Rahul Singh Nov 28 '14 at 13:50
  • @ALOK: Dim dept As String = CType(e.Row.FindControl("lblDepartment"), Label).Text – Giusepe Moreno Nov 28 '14 at 13:50
  • so i think you want a value which is retured by dept column in your db to be selected in dd. Actually i cant understand it as i never used vb but i use asp. Anyways logic will be same so i too can try – ALOK Nov 28 '14 at 13:53
  • @alok exactly. I would like that value returned by the DB to be selected in the dropdown and also allow the user to change if it needs changing. – Giusepe Moreno Nov 28 '14 at 13:57
  • Don't you have department id in your database table? – ALOK Nov 28 '14 at 14:07
  • @alok it seems you are right, the "Find by text" method wasnot selecting the correct dropdown because the dropdown was setup to be UPPERCASE while the lbl was in regular case. Thanks for the help. – Giusepe Moreno Nov 28 '14 at 14:29
  • Yes,Actually Its time for debugging. Try breakpoint to find out actually what value you are getting in dept string...Follow this tutorial for debugging http://www.tutorialspoint.com/asp.net/asp.net_debugging.htm – ALOK Nov 28 '14 at 14:33
  • @Alok if you want to post an answer about using the Dept ID instead of name, I'll mark it as accepted. Thanks! – Giusepe Moreno Nov 28 '14 at 16:23

2 Answers2

1

You simply need to retrieve dept id and store it in gridview as hidden (if you don't want to display it).

   Dim dept As String = CType(e.Row.FindControl("lblDepartmentId"), Label).Text
   ddlDepartment.SelectedValue = dept;

Hope it helps.

ALOK
  • 553
  • 6
  • 17
0

Your problem is that you ara trying to find the contorl in the row but is inside a table cell.

Try this.

Dim cbo As DropDownList = CType(YourDGV.Rows(x).Cells(0).FindControl("ddlDepartment"), DropDownList)
Harval
  • 76
  • 3
  • Thanks for taking the time in reviewing my question. It was actually something silly that caused the error. The DropDownList was set to an UPPERCASE, while the label where I was getting the text was in ProperCase. I will probably change it so it gets the Department ID, not the Name. – Giusepe Moreno Nov 28 '14 at 14:52