[Solved] GridView SelectedIndex Change Error: String was not recognized as a valid DateTime

akhter
 
on Aug 13, 2021 07:10 AM
Sample_114754.zip
678 Views

I have template field in gridview of Date, which is empty 

<asp:TemplateField HeaderText="Approve_Date" >
    <ItemTemplate>
        <asp:Label ID="App_Date" runat="server" Text='<%#Eval("App_Date","{0:MM/dd/yyyy}")%>'></asp:Label>
    </ItemTemplate>
</asp:TemplateField>

when i am selecting row from gridivew on selectedindexchanged event to populate it into textbox which textmode is Date, then it is giving me error

String was not recognized as a valid DateTime.

on below line;

 txtappdate.Text = Convert.ToDateTime((GVballist.SelectedRow.FindControl("App_Date") as Label).Text).ToString("yyyy-MM-dd").Replace("&nbsp;", "");
Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Aug 16, 2021 07:41 AM

Hi akhter,

Check this example. Now please take its reference and correct your code.

Database

For this example I have used of Northwind database that you can download using the link given below.

Download Northwind Database

HTML

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnSelectedIndexChanged="OnSelectedIndexChanged">
    <Columns>
        <asp:BoundField DataField="EmployeeID" HeaderText="Id" />
        <asp:BoundField DataField="FirstName" HeaderText="Name" />
        <asp:TemplateField HeaderText="Approve_Date">
            <ItemTemplate>
                <asp:Label ID="App_Date" runat="server" Text='<%#Eval("App_Date","{0:MM/dd/yyyy}")%>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:ButtonField Text="Select" CommandName="Select" />
    </Columns>
</asp:GridView>
<hr />
<asp:TextBox runat="server" ID="txtAppDate" />

Namespaces

C#

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

VB.Net

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

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    string query = "SELECT EmployeeID,FirstName,CONVERT(VARCHAR(50),BirthDate,101) App_Date FROM Employees";
    using (SqlConnection con = new SqlConnection(conString))
    {
        SqlCommand cmd = new SqlCommand(query);
        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            cmd.Connection = con;
            sda.SelectCommand = cmd;
            using (DataTable dt = new DataTable())
            {
                sda.Fill(dt);
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
        }
    }
}

protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
    txtAppDate.Text = DateTime.ParseExact((GridView1.SelectedRow.FindControl("App_Date") as Label).Text, "MM/dd/yyyy",
        System.Globalization.CultureInfo.InvariantCulture).ToString("yyyy-MM-dd").Replace("&nbsp;", "");
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Dim query As String = "SELECT EmployeeID,FirstName,CONVERT(VARCHAR(50),BirthDate,101) App_Date FROM Employees"
    Using con As SqlConnection = New SqlConnection(conString)
        Dim cmd As SqlCommand = New SqlCommand(query)
        Using sda As SqlDataAdapter = New SqlDataAdapter()
            cmd.Connection = con
            sda.SelectCommand = cmd
            Using dt As DataTable = New DataTable()
                sda.Fill(dt)
                GridView1.DataSource = dt
                GridView1.DataBind()
            End Using
        End Using
    End Using
End Sub

Protected Sub OnSelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
    txtAppDate.Text = DateTime.ParseExact(TryCast(GridView1.SelectedRow.FindControl("App_Date"), Label).Text, "MM/dd/yyyy",
                                            System.Globalization.CultureInfo.InvariantCulture).ToString("yyyy-MM-dd").Replace("&nbsp;", "")
End Sub

Screenshot