Answered by:
Object reference not set to an instance of an object in form view

Question
-
User-1235671040 posted
Dear all
When I press a button am trying to change the form view from insert to edit mode, I got an error "Object reference not set to an instance of an object "
when I try to get the form view control in edit mode, as the following :
If FormView1.CurrentMode = FormViewMode.Insert Then
FormView1.ChangeMode(FormViewMode.Edit)
FormView1.DefaultMode = FormViewMode.Edit
End If
FormView1.DataBind()
Dim DiscDDLInsert As DropDownList = CType(FormView1.FindControl("DiscNewEditDDL"), DropDownList)
DiscDDLInsert.SelectedValue = DiscDDLInsert.Items.FindByText(DiscDDL.SelectedItem.Text).Value 'Get the "Object reference not set to an instance of an object " error
DiscDDLInsert.Enabled = True
so any suggestions
Thanks in advance
Wednesday, December 18, 2019 7:35 AM
Answers
-
User-1235671040 posted
Dear samwu and oned_gk
Thanks both for your efforts, the problem was with Form view Sql Data Source, there was issue with the select statement.
Regards,
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, December 18, 2019 9:12 AM
All replies
-
User288213138 posted
Hi engzezo,
Dim DiscDDLInsert As DropDownList = CType(FormView1.FindControl("DiscNewEditDDL"), DropDownList)
DiscDDLInsert.SelectedValue = DiscDDLInsert.Items.FindByText(DiscDDL.SelectedItem.Text).Value 'Get the "Object reference not set to an instance of an object " error
Please debug your code and check if you get the id of DiscNewEditDDL. If you get it, then check if all the values in DiscDDL are included in DiscNewEditDDL.
After my test, if DiscDDL.SelectedItem.Text is not included in DiscNewEditDDL, this error will occur.
If you still can't solve your problem, please post your code and DropDownList datasource.
Best regards,
Sam
Wednesday, December 18, 2019 8:40 AM -
User-1716253493 posted
If DiscDDLInsert.Items.FindByValue("abc") IsNot Nothing Then DiscDDLInsert.Items.FindByValue("abc").Selected = true End
Sorry, i'm not get your logic
You should have same controlid in edit and insert template
Ensure "DiscDDLInsert" is in both template
or check what is current mode to find the control
If FormView1.CurrentMode = FormViewMode.Edit Then Dim DiscDDLInsert As DropDownList = CType(FormView1.FindControl("DiscNewEditDDL"), DropDownList) End If
Wednesday, December 18, 2019 9:05 AM -
User-1235671040 posted
Dear samwu and oned_gk
Thanks both for your efforts, the problem was with Form view Sql Data Source, there was issue with the select statement.
Regards,
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, December 18, 2019 9:12 AM