Hi RichardSa,
Before converting to DateTime you need to check weather the column value is null or not.
If not null then use convert function.
HTML
<asp:Label ID="lblShippedDate" runat="server"></asp:Label>
Namespaces
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.PopulateLabel();
}
}
private void PopulateLabel()
{
DataTable dt = new DataTable();
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("SELECT TOP 1 ShippedDate FROM Orders WHERE OrderId = 11060", con))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
sda.Fill(dt);
if(dt.Rows[0]["ShippedDate"] != DBNull.Value)
{
lblShippedDate.Text = Convert.ToDateTime(dt.Rows[0]["ShippedDate"]).ToString("MMM d, yyyy");
}
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.PopulateLabel()
End If
End Sub
Private Sub PopulateLabel()
Dim dt As DataTable = New DataTable()
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand("SELECT TOP 1 ShippedDate FROM Orders WHERE OrderId = 11060", con)
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
sda.Fill(dt)
If Not IsDBNull(dt.Rows(0)("ShippedDate")) Then
lblShippedDate.Text = Convert.ToDateTime(dt.Rows(0)("ShippedDate")).ToString("MMM d, yyyy")
End If
End Using
End Using
End Using
End Sub