user3985746
user3985746

Reputation:

how to show values from database in a dropdownlist which is inside a gridview?

I'm trying get values from database to dropdownlist which is placed inside a gridview item template. I'm using gridview to take values from user. In one column I'm using dropdownlist from which user has to select an item. According to the selection its cost price will populate automatically on the other column. But I'm unable to get the values in dropdownlist and getting an error "Object reference not set to an instance of an object."

Aspx code given below:

<asp:GridView ID="gvItemList" runat="server" ShowFooter="True" AutoGenerateColumns="False" AutoGenerateDeleteButton="True" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2" ForeColor="Black" ViewStateMode="Enabled" CssClass="newStyle9" style="text-align: center" OnRowDeleting="gvItemList_RowDeleting" OnRowDataBound="gvItemList_RowDataBound">
    <Columns>
        <asp:TemplateField HeaderText="Sl No" SortExpression="Id">                                    
                                    <ItemTemplate>
                                        <asp:Label ID="lblId" runat="server"
                                            Text='<%# Container.DataItemIndex+1 %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>
         <asp:TemplateField HeaderText="Item">
             <ItemTemplate>
                 <asp:DropDownList ID="ddlItem" runat="server" Height="25px" Width="128px">
                 </asp:DropDownList>
             </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Required Date">
            <ItemTemplate>
                <asp:TextBox ID="txtRequiredDate" runat="server" />
                <ajaxToolkit:CalendarExtender ID="txtRequiredDate_CalendarExtender" runat="server" Enabled="True" TargetControlID="txtRequiredDate">
                </ajaxToolkit:CalendarExtender>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Required Quantity">
            <ItemTemplate>
                <asp:TextBox ID="txtRequiredQuantity" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Cost Price">
            <ItemTemplate>
                <asp:Label ID="lblCostPrice" runat="server"></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>        
        <asp:TemplateField HeaderText="Total">
            <ItemTemplate>
                <asp:Label ID="lblTotal" runat="server"></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="UoM Code">
            <ItemTemplate>
                <asp:Label ID="lblUomCode" runat="server">Manual</asp:Label>

            </ItemTemplate>
            <FooterStyle HorizontalAlign="Right" />
            <FooterTemplate>
                <asp:Button ID="AddRowButton" runat="server" Text="Add New Item" 
                    OnClick="ButtonAdd_Click" />
            </FooterTemplate>

        </asp:TemplateField>
    </Columns>
    <FooterStyle BackColor="#CCCCCC" />
    <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
    <RowStyle BackColor="White" />
    <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#F1F1F1" />
    <SortedAscendingHeaderStyle BackColor="#808080" />
    <SortedDescendingCellStyle BackColor="#CAC9C9" />
    <SortedDescendingHeaderStyle BackColor="#383838" />
</asp:GridView>

Code under row databound is given below:

protected void gvItemList_RowDataBound(object sender, GridViewRowEventArgs e)
    {        
        DS_SiteDataTableAdapters.tbl_ItemTableAdapter item;
        item = new DS_SiteDataTableAdapters.tbl_ItemTableAdapter();
        DataTable dt = new DataTable();
        dt = item.GetItem();

        DropDownList ddlItem = (DropDownList)e.Row.FindControl("ddlItem");

        ddlItem.DataSource = dt; //here I'm getting an error "Object reference not set to an instance of an object."
        ddlItem.DataTextField = "Item";
        ddlItem.DataValueField = "Item";
        ddlItem.DataBind();        
    }

Any help is greatly appreciated!

Upvotes: 1

Views: 2644

Answers (5)

Rohit
Rohit

Reputation: 1550

if (e.Row.RowType == DataControlRowType.DataRow)
{
    //Find the DropDownList in the Row
    DropDownList ddlCountries = (e.Row.FindControl("ddlCountries") as DropDownList);
    ddlCountries.DataSource = GetData("SELECT DISTINCT Country FROM Customers");
    ddlCountries.DataTextField = "Country";
    ddlCountries.DataValueField = "Country";
    ddlCountries.DataBind();

    //Add Default Item in the DropDownList
    ddlCountries.Items.Insert(0, new ListItem("Select Country"));
}

Hope this helps!

Upvotes: 1

Akshay Randive
Akshay Randive

Reputation: 384

try this you get null reference because you are not able to find ddlItem control

at line DropDownList ddlItem = (DropDownList)e.Row.FindControl("ddlItem");

so instead you can try as below DropDownList ddlItem = (DropDownList)GridView.Rows[rowindex].FindControl("ddlItem"))

Upvotes: 0

User125
User125

Reputation: 108

just check below condition in your method

if (e.Row.RowType == DataControlRowType.DataRow)


        { your code }

I hope it will help u

Upvotes: 0

Rahul Singh
Rahul Singh

Reputation: 21795

You are getting Object Reference error because you are not checking for DataRow in your gridview. Because of that it is trying to find the dropdown in header where it is not present and thus you are getting Dropdown object as Null.

Add this condition to your gvItemList_RowDataBound event:-

protected void gvItemList_RowDataBound(object sender, GridViewRowEventArgs e)
{ 
if (e.Row.RowType == DataControlRowType.DataRow)
   {
      //Your rest code
   }
}

Upvotes: 0

Harry
Harry

Reputation: 3195

As mentioned in the comments, you are trying to use a null object, thinking that it has a referenced object.

You are doing something like this:

public class Example
{
   public void MyMethod()
   {

   }
}

Example myExample= null;
myExample.MyMethod();

You will get "Object reference not set to an instance of an object." which is the same as NullReferenceException because you are calling a method on a reference type which is null.

Upvotes: 0

Related Questions