I spent my whole day but I could not solve the problem. I dont have much experience in Asp.net. Please help... DropDownList dont send selected value to the Sub DropDown_SelectedIndexChanged

The Error Message is:Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 38: Protected WithEvents ItemDropDown As System.Web.UI.WebControls.DropDownList
Line 39: Sub DropDown_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
Line 40: Dim x As String= ItemDropDown.SelectedItem.Value
Line 41: label.Text= x
Line 42: End Sub

<%@ Page Language="VB" Debug="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<script language="VB" Debug="true" runat="server">


Sub Page_Load(Source As Object, E As EventArgs)
If Not IsPostBack Then
bindDataGrid
End If
End Sub

Sub bindDataGrid
Dim myConn As SqlConnection
Dim mySqlAdapter As SqlDataAdapter
Dim connStr, sqlStr As String
Dim myDataSet As New Dataset
connStr="server=SELIM; uid=xxxx; pwd=xxxx; Database=TestFiles"


sqlStr="SELECT * FROM TestFiles"

myConn= New SqlConnection(connStr)
myConn.Open()
mySqlAdapter=New SqlDataAdapter(sqlStr,myConn)
mySqlAdapter.Fill(myDataSet,"Sarkilar")
FileList.DataSource=myDataSet.Tables("Sarkilar")
FileList.DataBind()
myConn.Close()
End Sub

Protected WithEvents ItemDropDown As System.Web.UI.WebControls.DropDownList
Sub DropDown_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim x As String= ItemDropDown.SelectedItem.Value
label.Text= x
End Sub

Sub doPaging(s As Object, e As DataGridPageChangedEventArgs)
FileList.CurrentPageIndex=e.NewPageIndex
bindDataGrid
End Sub

</script>

<html>

<body bgcolor="#E5F2D8">
<form runat="server">

<p><font face="Verdana" size="1" Color="#669933" ><center>
(Mozilla Kullanicilarinin dosyayi indirdikten sonra dosyanin uzantisinida mp3 olarak degistirmeleri gerekmektedir)
</center></font></p>


<asp:DataGrid id="FileList" runat="server"
BorderColor="Gray"
BorderWidth="2"
CellPadding="4"

ShowHeader="true"
Align="center"
AutoGenerateColumns="false"
AllowPaging="true" PageSize="10"
PagerStyle-Mode="NumericPages"
PagerStyle-HorizontalAlign="Center"
OnPageIndexChanged="doPaging"
>

<HeaderStyle BorderColor="white" BackColor="#669933"
ForeColor="White"
Font-Bold="True"
Font-Name="Arial"
Font-Size="9" HorizontalAlign="Center"/>

<Columns>

<asp:TemplateColumn HeaderText="Sarki Ismi">
<ItemTemplate>
<b>
<font face="arial" size="1" Color="#336633">
<%# DataBinder.Eval(Container.DataItem, "MyFileName") %>
</b>
</font>
</ItemTemplate>
</asp:TemplateColumn>


<asp:TemplateColumn HeaderText="Dosya Uzantisi">
<ItemTemplate>
<b>
<font face="arial" size="1" Color="#336633">
<%# DataBinder.Eval(Container.DataItem, "FileType") %>
</b>
</font>
</ItemTemplate>
</asp:TemplateColumn>

<asp:TemplateColumn HeaderText="Dosyayi Indir">
<ItemTemplate>
<b>
<font face="arial" size="1" Color="#336633">
<a href="indir.aspx?ID=<%# DataBinder.Eval(Container.DataItem, "ID") %>">
Indir</a>
</b>
</font>
</ItemTemplate>
</asp:TemplateColumn>


<asp:TemplateColumn HeaderText="Puanlama">
<ItemTemplate>
<b>
<font face="arial" size="1" Color="#336633">
<%# DataBinder.Eval(Container.DataItem, "ID") %>
</b>
</font>
</ItemTemplate>
</asp:TemplateColumn>

<asp:TemplateColumn HeaderText="Oy Ver">
<ItemTemplate>
<asp:DropDownList ID="ItemDropDown" Runat="server"
AutoPostBack="True"
OnSelectedIndexChanged="DropDown_SelectedIndexChanged">
<asp:ListItem Selected="True" Text="Puan Ver"/>
<asp:ListItem Value="1" Text="1"/>
<asp:ListItem Value="2" Text="2"/>
<asp:ListItem Value="3" Text="3"/>
<asp:ListItem Value="4" Text="4"/>
<asp:ListItem Value="5" Text="5"/>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>

</Columns>

</asp:DataGrid>

</form>

<asp:label id="label" runat="server"/>
<body>
</html>

Dani AI

Generated

The NullReferenceException means the field you reference (ItemDropDown) is Nothing at runtime. That happens because the DropDownList lives inside a DataGrid TemplateColumn — the grid creates one DropDownList per row. A page-level Protected WithEvents ItemDropDown does not point to the per-row instance, so trying to read ItemDropDown.SelectedItem will fail.

Use the sender argument inside the SelectedIndexChanged handler to get the actual DropDownList instance for the row that raised the event. Example pattern:

Protected Sub DropDown_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
    Dim ddl As System.Web.UI.WebControls.DropDownList = CType(sender, System.Web.UI.WebControls.DropDownList)
    label.Text = ddl.SelectedValue
End Sub

If you need to know which DataGrid row raised the event (for example to read a hidden ID or access other controls in that row), get the naming container and then FindControl or use the grid's DataKeys:

Dim item As DataGridItem = CType(ddl.NamingContainer, DataGridItem)
Dim idValue As String = CType(item.FindControl("IDHidden"), HiddenField).Value

As noted, SelectedValue is convenient; as mentioned, the DropDownList must post back (AutoPostBack). Also remove the page-level Protected WithEvents ItemDropDown declaration for the templated control (it will remain Nothing), ensure the label has runat="server", and avoid rebinding the DataGrid on every postback (bind only when Not IsPostBack, except when handling paging). For reference on reading values from list controls and locating template children, see the ListControl.SelectedValue documentation and Control.FindControl / NamingContainer guidance on Microsoft Docs.

Recommended Answers

All 3 Replies

selam,

1-yapmak istedigin sey nedir?
2-autopostback ten haberin var mi?

kolay gelsin

Try this instead;

Dim x As String= ItemDropDown.SelectedValue

Oh and use CODE tags around your code when posting in daniweb forums.

selim,

if you change autopostback property of dropdownlistbox to TRUE, that will help you. and you can use Dim x As String= dropdownlistbox1.text it like this. let us know what happend.

thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.