Enter Null Values for DateTime Column of SQL Server

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 = 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();
}    source : http://www.c-sharpcorner.com/UploadFile/sd_patel/EnterNullValuesForDateTime11222005015742AM/EnterNullValuesForDateTime.aspx

 

 

 

 

 

 

Bir cevap yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir

one + 1 =

This site uses Akismet to reduce spam. Learn how your comment data is processed.