Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
If e.Row.RowType = DataControlRowType.Header Then
            Dim ddlVCrop As New DropDownList
            ddlVCrop = CType(e.Row.FindControl("ddlCrop"), DropDownList)
            If Not ddlVCrop Is DBNull.Value Then
                Dim ds As New DataSet
                ds = objYield.getVariety
                If ds.Tables(0).Rows.Count > 0 Then
                 ddlVCrop.DataSource = ds
                    ddlVCrop.DataValueField = "VARIETY_CODE"
                    ddlVCrop.DataTextField = "VARIETY_CODE"
                    ddlVCrop.DataBind()
                    ddlVCrop.Items.Insert(0, "Select One")
                   
End If


Hi

In the above code when i am trying to bind the "ds" to "dropdown"(in the sentence in bold), i am facing null reference exception.Any one plz tell me the solution to my problem.

thanks in advance.
[edit]code block added[/edit]
Posted
Updated 11-Apr-11 1:52am
v3

Change the code to,

VB
If ddlVCrop IsNot Nothing Then
 
Share this answer
 
Comments
Harsha 2 11-Apr-11 8:09am    
Tnx for reply,

By adding the solution you have suggested I am able to handle the exception.But my concern is to bind the dataset to dropdown in the GridView dynamically which is not happening. So plz guide me in that aspect.
Venkatesh Mookkan 11-Apr-11 9:21am    
I have a feeling that your GridView row does not have a DropDownList named "ddlCrop". Please cross verified. It is better to diagnose if you post your ASPX code too.
To avoid the exception, I'd do this:

VB
Dim ds As DataSet = objYield.getVariety()
If ds IsNot Nothing AndAlso ds.Tables.Count > 0 AndAlso ds.Tables(0).Rows.Count > 0 Then
   ... do your processing
End If
 
Share this answer
 
i think your drodown is in gridview header template

try to find out your dropdown in this way


GridView1.HeaderRow.FindControl("DropDownList1")
 
Share this answer
 
Something appears to be null here.
Try debugging through your code, have a look at the values of the underlying variables and perhaps you will be able to figure out what is really wrong (why this value is null).
 
Share this answer
 
Comments
Harsha 2 11-Apr-11 8:49am    
Tnx for reply,
Could you explain the need of giving "data key names" in the gridview inline editing.
By adding the solution you have suggested I am able to handle the exception.But my concern is to bind the dataset to dropdown in the GridView dynamically which is not happening. So plz guide me in that aspect.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900