Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I placed a drop down list in a datagrid. I want to pre-select which value in the drop down list is selected based an a value from a database. I keep getting the following error...

 

Exception Details: System.NullReferenceException: Object variable or With block variable not set.

 

Here is my code...

 

Private Sub dgTest_Edit(ByVal sender As System.Object, ByVal e As DataGridCommandEventArgs) Handles dgTest.EditCommand

Dim T As New DropDownList()

T = CType(e.Item.FindControl("ddlTest"), DropDownList)

Response.Write(T.SelectedIndex & "HELP")

End Sub

 

It seems to me that no control is found when I call FindControl resulting in the NullReferenceException.

 

Does anyone know why this would be occuring?

 

Thanks

Jason Lee Mayfield

  • Moderators
Posted

You need to do this in your aspx page...

<asp:DropDownList id="ddlTest" runat="server" datavaluefield="my_column_id" 
datatextfield="my_column_text" 
SelectedIndex='<%# GetDD_SelectedIndex(Container.DataItem("my_column_id"))  %>' 
DataSource="<%#myEditData%>"></asp:DropDownList>

 

The function GetDD_SelectedIndex takes the id column and returns the selected index from the database.

 

myEditData function returns the dataset needed to bind to this dropdown

Visit...Bassic Software

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...