Http://www.c-sharpcorner.com/UploadFile/sd_patel/EnterNullValuesForDateTime11222005015742AM/EnterNullValuesForDateTime.aspx
Intrby Sushila Patel September 26,200 3
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 wocould in most cases have errors:
- 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 wocould 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: