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
EndIf
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
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
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
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
Programming to simplify, don't look for difficult way
Suwandi - Non Graduate Programmer
Member
1 Points
12 Posts
Object reference not set to an instance of an object in form view
Dec 18, 2019 07:35 AM|engzezo|LINK
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
Contributor
3370 Points
1409 Posts
Re: Object reference not set to an instance of an object in form view
Dec 18, 2019 08:40 AM|samwu|LINK
Hi engzezo,
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
All-Star
52673 Points
15717 Posts
Re: Object reference not set to an instance of an object in form view
Dec 18, 2019 09:05 AM|oned_gk|LINK
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
Suwandi - Non Graduate Programmer
Member
1 Points
12 Posts
Re: Object reference not set to an instance of an object in form view
Dec 18, 2019 09:12 AM|engzezo|LINK
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,