How to store null value if time field is empty in asp.net.

Last Reply 8 days ago By pandeyism

Posted 11 days ago

Hello,

How to store null value if time field is empty in asp.net.

Dailyschedule Ds = new Dailyschedule
{
EmpID = box_section.SelectedValue,
Date = DateTime.Parse(txtDate.Value),
Time = DateTime.Parse(field_time.Text),
Remarks = field_remarks.Text,
};
return new AdminUsersDAL().SaveDailySchedule(Ds);

 

Thanks

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

Hi vail,

Please refer below sample.

HTML

Date:
<asp:TextBox runat="server" ID="txtDate" />
<asp:Button ID="Button1" Text="Insert" runat="server" OnClick="Insert" />
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="DateTest" HeaderText="Date" DataFormatString="{0:dd/MM/yyyy}"
            NullDisplayText="Null" />
    </Columns>
</asp:GridView>

Namespaces

C#

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

VB.Net

Imports System.Data.SqlClient
Imports System.Data

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT * FROM TestDemo", con))
            {
                using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                {
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    this.GridView1.DataSource = dt;
                    this.GridView1.DataBind();
                }
            }
        }
    }
}

protected void Insert(object sender, EventArgs e)
{
    DateTime? date;
    if (!string.IsNullOrEmpty(txtDate.Text.Trim()))
    {
        date = DateTime.Parse(txtDate.Text);
    }
    else
    {
        date = null;
    }
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("INSERT INTO TestDemo VALUES(@Date)", con))
        {
            if (string.IsNullOrEmpty(date.ToString()))
            {
                cmd.Parameters.AddWithValue("@Date", SqlDbType.DateTime).Value = DBNull.Value;
            }
            else
            {
                cmd.Parameters.AddWithValue("@Date", date);
            }
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
        Using con As SqlConnection = New SqlConnection(constr)
            Using cmd As SqlCommand = New SqlCommand("SELECT * FROM TestDemo", con)
                Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
                    Dim dt As DataTable = New DataTable()
                    da.Fill(dt)
                    Me.GridView1.DataSource = dt
                    Me.GridView1.DataBind()
                End Using
            End Using
        End Using
    End If
End Sub

Protected Sub Insert(ByVal sender As Object, ByVal e As EventArgs)
    Dim date1 As DateTime?

    If Not String.IsNullOrEmpty(txtDate.Text.Trim()) Then
        date1 = DateTime.Parse(txtDate.Text)
    Else
        date1 = Nothing
    End If

    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString

    Using con As SqlConnection = New SqlConnection(constr)
        Using cmd As SqlCommand = New SqlCommand("INSERT INTO TestDemo VALUES(@Date)", con)
            If String.IsNullOrEmpty(date1.ToString()) Then
                cmd.Parameters.AddWithValue("@Date", SqlDbType.DateTime).Value = DBNull.Value
            Else
                cmd.Parameters.AddWithValue("@Date", date1)
            End If

            con.Open()
            cmd.ExecuteNonQuery()
            con.Close()
        End Using
    End Using
End Sub

Screenshot