Object reference not set to an instance of an object.

H

hina.pandya

I m getting this error "Object reference not set to an instance of an
object." when i run my application.

i m trying to select a value from dropdownlist

here is the code.

Protected Sub GetMsg(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles dgJobQueue.PreRender
ASPNET_MsgBox("welcome to msgbox")
dgJobQueue.FindControl("cboUserID")

Dim cboUserIdTemp As DropDownList =
CType(dgJobQueue.FindControl("cboUserID"), DropDownList)
Dim s As String = GetUserName()
Dim listItem As ListItem
cboUserIdTemp.SelectedValue = s
End Sub
 
K

Kevin Spencer

And the line that throws the exception is?.....

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Sometimes you eat the elephant.
Sometimes the elephant eats you.
 
A

abcd

Hi Hina

can you try this

if (cboUserIdTemp.Items.FindByText(s) != null) {
cboUserIdTemp.SelectedValue = s
}

This is C# code you can easily convert it to VB.NET

thanks
 
H

hina.pandya

Nope, It not working .


I m trying to set selected value to dropdownlist which is inside
datagrid. and dropdownlist gets populated by other method.

Thanks
 
K

Kevin Spencer

It looks like you didn't find the Control you were looking for. Are you sure
it's a child Control of the Control you're looking for it in? Are you sure
you spelled the ID correctly?

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Sometimes you eat the elephant.
Sometimes the elephant eats you.
 
H

hina.pandya

This is the asp code that i have . ..now i m trying to set the selected
value as logged in user name .. i have user logged in id which returns
string. s now .. when ever i try to set cboUserIdTemp.SelectedValue =
s it gives me an error. ..

and if i try to do this

cboUserIdTemp.SelectedIndex =
cboUserIdTemp.Items.IndexOf(cboUserIdTemp.Items.FindByValue(s)) it
gives me following error
Object reference not set to an instance of an object.
===============================================
Protected Sub GetMsg(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles dgJobQueue.PreRender
ASPNET_MsgBox("welcome to msgbox")
dgJobQueue.FindControl("cboUserID")

Dim cboUserIdTemp As DropDownList =
CType(dgJobQueue.FindControl("cboUserID"), DropDownList)
Dim s As String = GetUserName()
'Dim listItem As ListItem

'cboUserIdTemp.SelectedValue = s -----> Error :Method not
found: Void
System.Web.UI.WebControls.ListControl.set_SelectedValue(System.String).

cboUserIdTemp.SelectedIndex =
cboUserIdTemp.Items.IndexOf(cboUserIdTemp.Items.FindByValue(s)) ----->
Error: Object reference not set to an instance of an object.
'cboUserIdTemp.SelectedIndex = 1
End Sub

====================================

Protected Function PopulateProjectDropDownList()

Dim myCommand As SqlCommand = New SqlCommand("GetProjectList",
ripConn)
myCommand.CommandType = CommandType.StoredProcedure

ripConn.Open()
Return myCommand.ExecuteReader(CommandBehavior.CloseConnection)

End Function

====================

<asp:TemplateColumn HeaderText="User Name">
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" Width="15%"></ItemStyle>
<ItemTemplate>
<asp:Label id="Label2" runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id=cboUserID OnPreRender = '<%# GetMsg() %>'
tabIndex=1 runat="server" AutoPostBack="True" DataValueField="UserName"
DataTextField="UserName" DataSource="<%# PopulateUserNameDropDownList()
%>">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top