Enter Null Values for DateTime Column of SQL Server

來源:互聯網
上載者:User

http://www.c-sharpcorner.com/UploadFile/sd_patel/EnterNullValuesForDateTime11222005015742AM/EnterNullValuesForDateTime.aspx

 

IntrBy  Sushila Patel September 26, 2003

Inserting a null value to the DateTime Field in SQL Server is one of the most common issues giving various errors. Even if one enters null values the value in the database is some default value as 1/1/1900 12:00:00 AM.

oduction:

Inserting a null value to the DateTime Field in SQL Server is one of the most common issues giving various errors. Even if one enters null values the value in the database is some default value as 1/1/1900 12:00:00 AM.

The Output of entering the null DateTime based on the code would in most cases have errors as:

  • String was not recognized as a valid DateTime.
  • Value of type 'System.DBNull' cannot be converted to 'String'.

Or No Error but DataTime entered in Database would be as 1/1/1900 12:00:00 AM
So lets write the code to enter null values in the DataBase.

The User Interface is as follows:

To begin with Code:

Namespaces used

System.Data.SqlClient/ System.Data.OleDb

  • System.Data.SqlTypes
  • Code for System.Data.SqlClient
  • C#

    string sqlStmt ;
    string conString ;
    SqlConnection cn =null;
    SqlCommand cmd =null;
    SqlDateTime sqldatenull ;
    try
    {
    sqlStmt = "insert into Emp (FirstName,LastName,Date) Values (@FirstName,@LastName,@Date) ";
    conString = "server=localhost;database=Northwind;uid=sa;pwd=;";
    cn = new SqlConnection(conString);
    cmd = new SqlCommand(sqlStmt, cn);
    cmd.Parameters.Add(new SqlParameter("@FirstName", SqlDbType.NVarChar, 11));
    cmd.Parameters.Add(new SqlParameter("@LastName", SqlDbType.NVarChar, 40));
    cmd.Parameters.Add(new SqlParameter("@Date", SqlDbType.DateTime));
    sqldatenull = SqlDateTime.Null;
    cmd.Parameters["@FirstName"].Value = txtFirstName.Text;
    cmd.Parameters["@LastName"].Value = txtLastName.Text;
    if (txtDate.Text == "")
    {
    cmd.Parameters ["@Date"].Value =sqldatenull ;
    //cmd.Parameters["@Date"].Value = DBNull.Value;
    }
    else
    {
    cmd.Parameters["@Date"].Value = DateTime.Parse(txtDate.Text);
    }
    cn.Open();
    cmd.ExecuteNonQuery();
    Label1.Text = "Record Inserted Succesfully";
    }
    catch (Exception ex)
    {
    Label1.Text = ex.Message;
    }
    finally
    {
    cn.Close();
    }

    VB.NET

    Dim sqlStmt As String
    Dim conString As String
    Dim cn As SqlConnection
    Dim cmd As SqlCommand
    Dim sqldatenull As SqlDateTime
    Try
    sqlStmt = "insert into Emp (FirstName,LastName,Date) Values (@FirstName,@LastName,@Date) "
    conString = "server=localhost;database=Northwind;uid=sa;pwd=;"
    cn = New SqlConnection(conString)
    cmd = New SqlCommand(sqlStmt, cn)
    cmd.Parameters.Add(New SqlParameter("@FirstName", SqlDbType.NVarChar, 11))
    cmd.Parameters.Add(New SqlParameter("@LastName", SqlDbType.NVarChar, 40))cmd.Parameters.Add(New SqlParameter("@Date", SqlDbType.DateTime))
    sqldatenull = SqlDateTime.Null
    cmd.Parameters("@FirstName").Value = txtFirstName.Text
    cmd.Parameters("@LastName").Value = txtLastName.Text
    If (txtDate.Text = "") Then
    cmd.Parameters("@Date").Value = sqldatenull
    'cmd.Parameters("@Date").Value = DBNull.Value
    Else
    cmd.Parameters("@Date").Value = DateTime.Parse(txtDate.Text)
    End If
    cn.Open()
    cmd.ExecuteNonQuery()
    Label1.Text = "Record Inserted Succesfully"
    Catch ex As Exception
    Label1.Text = ex.Message
    Finally
    cn.Close()
    End Try

    Code for System.Data.SqlClient.

    C#

    string sqlStmt;
    string conString ;
    OleDbConnection cn =null ;
    OleDbCommand cmd =null ;
    try
    {
    sqlStmt = "insert into Emp (FirstName,LastName,Date) Values (?,?,?) ";
    conString = "Provider=sqloledb.1;user id=sa;pwd=;database=northwind;data source=localhost";
    cn =new OleDbConnection(conString);
    cmd = new OleDbCommand(sqlStmt, cn) ;
    cmd.Parameters.Add(new OleDbParameter("@FirstName", OleDbType.VarChar, 40));
    cmd.Parameters.Add(new OleDbParameter("@LastName", OleDbType.VarChar, 40));
    cmd.Parameters.Add(new OleDbParameter("@Date", OleDbType.Date));
    cmd.Parameters["@FirstName"].Value = txtFirstName.Text;
    cmd.Parameters["@LastName"].Value = txtLastName.Text;
    if ((txtDate.Text == "") )
    {
    cmd.Parameters["@Date"].Value = DBNull.Value;
    }
    else
    {
    cmd.Parameters["@Date"].Value = DateTime.Parse(txtDate.Text);
    }
    cn.Open();
    cmd.ExecuteNonQuery();
    Label1.Text = "Record Inserted Succesfully";
    }
    catch (Exception ex)
    {
    Label1.Text = ex.Message;
    }
    finally
    {
    cn.Close();


    VB.NET

    Dim sqlStmt As String
    Dim conString As String
    Dim cn As OleDbConnection
    Dim cmd As OleDbCommand
    Try
    sqlStmt = "insert into Emp (FirstName,LastName,Date) Values (?,?,?) "
    conString = "Provider=sqloledb.1;user id=sa;pwd=;database=northwind;data source=localhost"
    cn = New OleDbConnection(conString)
    cmd = New OleDbCommand(sqlStmt, cn)
    cmd.Parameters.Add(New OleDbParameter("@FirstName", OleDbType.VarChar, 40))cmd.Parameters.Add(New OleDbParameter("@LastName", OleDbType.VarChar, 40))cmd.Parameters.Add(New OleDbParameter("@Date", OleDbType.Date))cmd.Parameters("@FirstName").Value = txtFirstName.Text
    cmd.Parameters("@LastName").Value = txtLastName.Text
    If (txtDate.Text = "") Then
    cmd.Parameters("@Date").Value = DBNull.Value
    Else
    cmd.Parameters("@Date").Value = DateTime.Parse(txtDate.Text)
    End If
    cn.Open()
    cmd.ExecuteNonQuery()
    Label1.Text = "Record Inserted Succesfully"
    Catch ex As Exception
    Label1.Text = ex.Message
    Finally
    cn.Close()
    End Try

    The Data Entered in DataBase:

     

    相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.