Hi akhter.
FindByText is not finding an item in DropDownList and you are trying to use the Selected property on a null object.
So It is always important when using methods like FindByValue() or FindByText() to check and ensure that a particular item exists prior to using it.
Check this example. Now please take its reference and correct your code.
HTML
<table>
<tr>
<td>Item Name:</td>
<td><asp:TextBox ID="txtName" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>UOM :</td>
<td>
<asp:DropDownList ID="ddlUOM" runat="server">
<asp:ListItem Value="0">Select UOM</asp:ListItem>
<asp:ListItem Value="kg">Kilogram</asp:ListItem>
<asp:ListItem Value="m">Meter</asp:ListItem>
<asp:ListItem Value="s">Second</asp:ListItem>
<asp:ListItem Value="A">Ampere</asp:ListItem>
<asp:ListItem Value="K">Kelvin</asp:ListItem>
<asp:ListItem Value="mol">Mole</asp:ListItem>
<asp:ListItem Value="cd">Candela</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
</table>
<br />
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
DataKeyNames="CodeItem" OnSelectedIndexChanged="OnSelectedIndexChanged">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="CodeItem" HeaderText="Code" ReadOnly="True" />
<asp:BoundField DataField="Description" HeaderText="Description" />
<asp:BoundField DataField="NameUOM" HeaderText="UOM" />
</Columns>
</asp:GridView>
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
System.Data.DataTable dt = new System.Data.DataTable();
dt.Columns.AddRange(new System.Data.DataColumn[] {
new System.Data.DataColumn("CodeItem", typeof(int)),
new System.Data.DataColumn("Description", typeof(string)),
new System.Data.DataColumn("NameUOM",typeof(string)) });
dt.Rows.Add(501, "Big Test 501", "");
dt.Rows.Add(503, "Big Test 503", "Second");
dt.Rows.Add(502, "Big Test 502", null);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
txtName.Text = GridView1.SelectedRow.Cells[2].Text.Trim();
string umo = GridView1.SelectedRow.Cells[3].Text.Trim();
ddlUOM.ClearSelection();
// Check item exists in DropDownList or not.
if (ddlUOM.Items.FindByText(umo) != null)
{
ddlUOM.Items.FindByText(umo).Selected = true;
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As System.Data.DataTable = New System.Data.DataTable()
dt.Columns.AddRange(New System.Data.DataColumn() {
New System.Data.DataColumn("CodeItem", GetType(Integer)),
New System.Data.DataColumn("Description", GetType(String)),
New System.Data.DataColumn("NameUOM", GetType(String))})
dt.Rows.Add(501, "Big Test 501", "")
dt.Rows.Add(503, "Big Test 503", "Second")
dt.Rows.Add(502, "Big Test 502", Nothing)
GridView1.DataSource = dt
GridView1.DataBind()
End If
End Sub
Protected Sub OnSelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
txtName.Text = GridView1.SelectedRow.Cells(2).Text.Trim()
Dim umo As String = GridView1.SelectedRow.Cells(3).Text.Trim()
ddlUOM.ClearSelection()
' Check item exists in DropDownList or not.
If ddlUOM.Items.FindByText(umo) IsNot Nothing Then
ddlUOM.Items.FindByText(umo).Selected = True
End If
End Sub
Screenshot