Reputation: 253
I keep getting the above error in the title line and it makes no sense, because I am using a sample table with only 5 records and each record has a value as per the drop down menu.
This is my code used to declare the drop down list. I have joined two tables in my SQL data source to reflect what I want populated in the grid view and have hidden columns as necessary. I am using the same data source to populate the drop down list any help would be most appreciated
<asp:DropDownList ID="DropDownListCurrency" runat="server"
CausesValidation="True" DataSourceID="GridView"
DataTextField="Currency" DataValueField="Currency_ID"
AppendDataBoundItems="True">
<asp:ListItem Value="0" Text="<Select>" Enabled="True"></asp:ListItem>
</asp:DropDownList>
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
GridViewRow row = GridView1.SelectedRow;
AccountNumber.Text = (string)row.Cells[0].Text;
....
DropDownListCurrency.SelectedValue = (string)row.Cells[8].Text;
}
catch (Exception ex)
{
Console.WriteLine("{0} Exception caught.", ex);
}
}
Upvotes: 10
Views: 79975
Reputation: 1
I have used nvarchar(20) as datatype in db because in char datatype space comes so there was difference in text including space. Trim() can be also used.
Upvotes: 0
Reputation: 739
#region by zia for if item not exist in dropdownlist
string qlf = dsEmp.Tables["tblEmp"].Rows[0]["Group"].ToString();
ListItem selLqli = ddlGroup.Items.FindByText(qlf.Trim());
if (selLqli != null)
{
ddlGroup.ClearSelection();
}
else
{
ddlGroup.SelectedIndex = 0;
}
#endregion
Upvotes: 0
Reputation: 170
Setting Text
property of DropDownList
in ASPX to value missing from ItemList
causes the same error.
In my case what happened was that I have added by mistake meta-resourcekey
tag where Text
property was set to value missing in ItemList
.
The resources values were:
table {
border-collapse: collapse;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}
table th, table td {
border: solid 1px darkgray;
padding: 0 5px;
}
table.tr:first(){
}
<table css="table">
<tr>
<th>Name</th>
<th>Value</th>
<th>Comment</th>
</tr>
<tr>
<td>Template.Text</td>
<td>Template</td>
<td></td>
</tr>
<tr>
<td>Template.ToolTip</td>
<td>template of standard outgoing email settings</td>
<td></td>
</tr>
</table>
and ASPX control was:
<asp:DropDownList runat="server" ID="ddlTemplate" CssClass="form-control dropdownlist" meta:resourcekey="Template" >
<asp:ListItem Selected="True" meta:resourcekey="TemplateEmpty" Value="EMPTY" />
<asp:ListItem Selected="False" meta:resourcekey="TemplatePOP3" Value="POP3" />
<asp:ListItem Selected="False" meta:resourcekey="TemplatePOP3Secured" Value="POP3S" />
<asp:ListItem Selected="False" meta:resourcekey="TemplateIMAP" Value="IMAP" />
<asp:ListItem Selected="False" meta:resourcekey="TemplateIMAPSecured" Value="IMAPS" />
</asp:DropDownList>
Removing the meta:resourcekey
did the trick for me.
Upvotes: 0
Reputation: 61
it's so simple just use this
Dropdown.SelectedValue = null;
Dropdown.DataBind();
Upvotes: 6
Reputation: 1
You can use the Trim() method
ddlname.Text = dt.Rows[0][3].ToString().Trim();
Upvotes: -1
Reputation: 1081
This can happen if the rows in the drop down list are in a related table and referential integrity is not being used in the database. In my case, I use referential integrity but I add a boolean to the drop down list record to say "Disabled" - i.e. I no longer want users to be able to select this value in the drop down list. So I filter the drop down list to show only values that are not "Disabled", but then the problem then is that existing data might already contain the value, leading to the error message above when Edit is pressed on the grid.
The way I handle this situation, is as follows:
Add the following code in the code behind:
protected void StaffTypeGridView_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.DataRow ||
e.Row.RowIndex != StaffTypeGridView.EditIndex) return;
var staffType = (StaffType)e.Row.DataItem;
var appCode = staffType.AppCode;
var ddl = (DropDownList) e.Row.FindControl(ddlName);
if (!string.IsNullOrEmpty(value) &&
ddl.Items.FindByValue(value) == null)
{
ddl.Items.Add(new ListItem
{
Value = value,
Text = value + " (Deleted)"
});
}
ddl.SelectedValue = value;
}
Don't forget to write code either in the DropDownList_OnSelectedIndexChanged or GridViewOnRowUpdating to update the value back into the data source (as its an unbound field).
Add a custom validator to the EditItemTemplate to ensure that data that has been deleted cannot be entered, i.e. the user MUST change the value in the drop down list in order to save.
This sounds quite complicated to explain but is quite a straightforward way of providing this functionality, unless anybody has any better ideas...
Upvotes: 0
Reputation: 9
Dropdownlist values are different from the values in the column of the database.
Example: The dropdown show Emma, but in the database exist Emma and Emma1.
The dropdownlist cannot find the value Emma1.
Upvotes: 0
Reputation: 34846
Attempt to find the value in the drop down list before attempting to set the SelectedValue, like this:
if (DropDownListCurrency.Items.FindByValue(row.Cells[8].Text.ToString().Trim()) != null)
{
DropDownListCurrency.SelectedValue = row.Cells[8].Text.ToString().Trim();
}
Note: The Trim()
call will remove any leading or trailing spaces in your text box text, which could be a cause for a match not being found.
So your full code should be this:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
GridViewRow row = GridView1.SelectedRow;
AccountNumber.Text = (string)row.Cells[0].Text;
....
if (DropDownListCurrency.Items.FindByValue(row.Cells[8].Text.ToString().Trim()) != null)
{
DropDownListCurrency.SelectedValue = row.Cells[8].Text.ToString().Trim();
}
}
catch (Exception ex)
{
Console.WriteLine("{0} Exception caught.", ex);
}
}
Upvotes: 15
Reputation: 207
Hi maybe in that cell from your gridview have white space or dropdownlist have white space for example isn't the same this
Dolar__ = Dolar
or
Dolar = Dolar__
use a Trim in code behind to clear white spaces in SQL Server don't use Rtrim this isn't good practices
Upvotes: 2