Handle Null value of DropDownList selection using FindByText in ASP.Net

Last Reply 29 days ago By pandeyism

Posted one month ago

I have null values in gridview cell, when select row from gridview which have some null values, then when it get load into dropdown list then my application get crashed.

so how to handle null values which is in cell of gridview...for dropdown list.

mentioned below query to load data into dropdown list on gridview selectedinexchanged

string SesnId3 = dm.GetData("select TID from dbo.Item_Type where TName='" + GVitem.SelectedRow.Cells[5].Text.Trim() + "'").Rows[0][0].ToString();
DDPack.SelectedValue = SesnId3;

 

You are viewing reply posted by: pandeyism 29 days ago.
Posted 29 days ago

Hi akhter,

Refer below sample.

HTML

<asp:GridView runat="server" AutoGenerateColumns="false" ID="gvCustomers" OnSelectedIndexChanged="gvCustomers_SelectedIndexChanged">
    <Columns>
        <asp:BoundField DataField="CustomerId" HeaderText="Id" />
        <asp:BoundField DataField="Name" HeaderText="Name" />
        <asp:BoundField DataField="Country" HeaderText="Country" />
        <asp:CommandField ShowSelectButton="true" />
    </Columns>
</asp:GridView>
<br />
<asp:DropDownList runat="server" ID="ddlName">
</asp:DropDownList>
<br />
<asp:DropDownList runat="server" ID="ddlCountry">
</asp:DropDownList>

Namespaces

C#

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

VB.Net

Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Code

C#

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            DataTable dt = GetData();
            gvCustomers.DataSource = dt;
            gvCustomers.DataBind();

            ddlCountry.DataSource = dt;
            ddlCountry.DataValueField = "Country";
            ddlCountry.DataTextField = "Country";
            ddlCountry.DataBind();
            ddlCountry.Items.Insert(0, new ListItem(" ", "0"));

            ddlName.DataSource = dt;
            ddlName.DataValueField = "Name";
            ddlName.DataTextField = "Name";
            ddlName.DataBind();
            ddlName.Items.Insert(0, new ListItem(" ", "0"));
        }
    }

    private static DataTable GetData()
    {
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT CustomerId, Name, Country FROM CustomerTest", con))
            {
                using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                {
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    return dt;
                }
            }
        }
    }

    protected void gvCustomers_SelectedIndexChanged(object sender, EventArgs e)
    {
        int index = gvCustomers.SelectedIndex;
        string name = gvCustomers.SelectedRow.Cells[1].Text;
        string country = gvCustomers.SelectedRow.Cells[2].Text;

        if (ddlName.Items.FindByText(name) != null)
        {
            ddlName.ClearSelection();
            ddlName.Items.FindByText(name).Selected = true;
            if (ddlCountry.Items.FindByText(country) != null)
            {
                ddlCountry.ClearSelection();
                ddlCountry.Items.FindByText(country).Selected = true;
            }
            else
            {
                ddlCountry.SelectedIndex = 0;
            }
        }
        if (ddlCountry.Items.FindByText(country) != null)
        {
            ddlCountry.ClearSelection();
            ddlCountry.Items.FindByText(country).Selected = true;
            if (ddlName.Items.FindByText(name) != null)
            {
                ddlName.ClearSelection();
                ddlName.Items.FindByText(name).Selected = true;
            }
            else
            {
                ddlName.SelectedIndex = 0;
            }
        }
    }

VB.Net

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)Handle Me.Load
        If Not Me.IsPostBack Then
            Dim dt As DataTable = GetData()
            gvCustomers.DataSource = dt
            gvCustomers.DataBind()
            ddlCountry.DataSource = dt
            ddlCountry.DataValueField = "Country"
            ddlCountry.DataTextField = "Country"
            ddlCountry.DataBind()
            ddlCountry.Items.Insert(0, New ListItem(" ", "0"))
            ddlName.DataSource = dt
            ddlName.DataValueField = "Name"
            ddlName.DataTextField = "Name"
            ddlName.DataBind()
            ddlName.Items.Insert(0, New ListItem(" ", "0"))
        End If
    End Sub

    Private Shared Function GetData() As DataTable
        Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString

        Using con As SqlConnection = New SqlConnection(constr)

            Using cmd As SqlCommand = New SqlCommand("SELECT CustomerId, Name, Country FROM CustomerTest", con)

                Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
                    Dim dt As DataTable = New DataTable()
                    da.Fill(dt)
                    Return dt
                End Using
            End Using
        End Using
    End Function

    Protected Sub gvCustomers_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim index As Integer = gvCustomers.SelectedIndex
        Dim name As String = gvCustomers.SelectedRow.Cells(1).Text
        Dim country As String = gvCustomers.SelectedRow.Cells(2).Text

        If ddlName.Items.FindByText(name) IsNot Nothing Then
            ddlName.ClearSelection()
            ddlName.Items.FindByText(name).Selected = True

            If ddlCountry.Items.FindByText(country) IsNot Nothing Then
                ddlCountry.ClearSelection()
                ddlCountry.Items.FindByText(country).Selected = True
            Else
                ddlCountry.SelectedIndex = 0
            End If
        End If

        If ddlCountry.Items.FindByText(country) IsNot Nothing Then
            ddlCountry.ClearSelection()
            ddlCountry.Items.FindByText(country).Selected = True

            If ddlName.Items.FindByText(name) IsNot Nothing Then
                ddlName.ClearSelection()
                ddlName.Items.FindByText(name).Selected = True
            Else
                ddlName.SelectedIndex = 0
            End If
        End If
    End Sub

Screenshot