1

I have the following dropdownlist in my project which takes its data from its relevant SqlDataSource. The problem which I am facing with is that the dropdownlist includes non-english items(ProvinceName) and I can get neither the DataTextField value nor the DataValueField value of an item in code behind file.

here is the markup:

<div class="col-lg-6">
    <label for="ddlProvince" class="control-label">Province</label>
    <asp:DropDownList AutoPostBack="true" ID="ddlProvince" 
         DataSourceID="sqlDsProvince" DataTextField="ProvinceName" 
         DataValueField="ProvinceID" CssClass="dropdown form-control" runat="server" />
    <asp:SqlDataSource ID="sqlDsProvince" runat="server" ConnectionString="<%$ connectionStrings:connectionStr %>" SelectCommand="SELECT [ProvinceID], [ProvinceName] FROM [Provinces]" ProviderName="System.Data.SqlClient">
    </asp:SqlDataSource>
</div>

When I run the following statements

testLabel.Text = ddlProvince.SelectedItem.Text;

or

testLabel.Text=ddlProvince.SelectedValue;

I get the NullReferenceException

UPDATE I have another dropdownlist which doesn't include non-english item when I get items of this dropdownlist into testLabel everything works

<asp:DropDownList ID="ddlGraduationClass" CssClass="dropdown form-control" runat="server">
                                <asp:ListItem Text="12" Value="12"></asp:ListItem>
                                <asp:ListItem Text="14" Value="14"></asp:ListItem>
                            </asp:DropDownList>
Elham Kohestani
  • 3,013
  • 3
  • 20
  • 29
  • 1
    Are you selecting an item on the dropdown before trying to read it? – Guilherme Aug 08 '17 at 05:44
  • I am not selecting a specific item as there I haven't used . What I want is only to select the value of selected Item. What ever item it would be. – Elham Kohestani Aug 08 '17 at 05:48
  • 1
    Have you populated the DDL before postback using `DataBind()` method? Check related code in `Page_Load` & `SelectedIndexChanged` which possibly throws NRE. – Tetsuya Yamamoto Aug 08 '17 at 05:52
  • @TetsuyaYamamoto the dropdownlist works fine with its related SqlDataSource why it would be necessary to use DataBind()? Secondly, when the page loads a default item is selected for the dropdownlist so I think DataBind() wouldn't be necessary. – Elham Kohestani Aug 08 '17 at 06:11
  • @ElhamKohestani I think `DataBind` method should placed in `Page_Load` inside `IsPostBack` check to rebind DDL with `SqlDataSource`. Have you tried that & is the DDL worked properly afterwards? – Tetsuya Yamamoto Aug 08 '17 at 06:17

2 Answers2

1

The ddlProvince.SelectedItem & ddlProvince.SelectedValue has null value while the dropdownlist options doesn't have any option items during Page_Load event, or data rebinding during postback has occurred (considering AutoPostBack="true", so it can trigger postback). To rebind the data at first time, use IsPostBack check on corresponding event:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        ddlProvince.DataSource = sqlDsProvince;
        ddlProvince.DataTextField = "ProvinceName";
        ddlProvince.DataValueField = "ProvinceID";
        ddlProvince.DataBind(); // don't forget to add this for binding dropdownlist items
    }
}

NB: ddlGraduationClass can also bound for another SqlDataSource in same ways like sample above.

References:

Why is my DropDownList's SelectedItem not working?

C# dropdown selected item null when list of strings/objects bound to the Data source

Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61
0

In the latter statement, it can really only be that testLabel is null or ddlProvince is null. I don't see testLabel defined in the page code you posted; are you sure it has an instance? If you pause the debugger on these lines of code, you'll be able to see what variable is claimed to be null. It won't be because ddlProvince has a foreign word selected

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
  • testLabel is defined I tested this label by fetching data from another dropddownlist which hadn't include non-english data and everything worked fine. – Elham Kohestani Aug 08 '17 at 05:50
  • When I want to fetch non-english items the exception arises. – Elham Kohestani Aug 08 '17 at 05:51
  • Can you give an example of something that fails? It sounds more like a bug that should be reported to Microsoft, if there tut is a value you can put in a ListItem that causes its hosting control to crash if you call .SelectedValue when that value is selected – Caius Jard Aug 08 '17 at 05:55
  • – Elham Kohestani Aug 08 '17 at 05:56
  • When I get data of this dropdownlist into testLabel everything works fine. – Elham Kohestani Aug 08 '17 at 05:57
  • Ok but can you give a block of HTML just like that, with non English words in the listitem text/value fields that causes the program to crash wen getting .selectedvalue? – Caius Jard Aug 08 '17 at 05:58