Thursday, March 22, 2012

Object reference not set to an instance of an object.

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 SubAnd the line that throws the exception is?....

--
HTH,

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

<hina.pandya@.gmail.com> wrote in message
news:1117043143.865298.52560@.g49g2000cwa.googlegro ups.com...
>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
cboUserIdTemp.SelectedValue = s
cboUserIdTemp.SelectedValue = s
cboUserIdTemp.SelectedValue = s
cboUserIdTemp.SelectedValue = s
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

hina.pandya@.gmail.com wrote:
> cboUserIdTemp.SelectedValue = s
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
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.

<hina.pandya@.gmail.com> wrote in message
news:1117044323.952273.117990@.g44g2000cwa.googlegr oups.com...
> cboUserIdTemp.SelectedValue = s
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.Fi ndByValue(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_Selected Value(System.String).

cboUserIdTemp.SelectedIndex =
cboUserIdTemp.Items.IndexOf(cboUserIdTemp.Items.Fi ndByValue(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.CloseConne ction)

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

0 comments:

Post a Comment