Insert or Update Null Values for DateTime Column of SQL Server through ASP.NET


Insert or Update Null Values for DateTime Column of SQL Server through ASP.NET. There are times there is a need to insert the null value in the datetime field in insert or update command text in asp.net that time we can use SqlDateTime.Null;

Learn how to Insert or Update Null Values for DateTime Column in SQL Server through ASP.NET


Insert or Update Null Values for DateTime Column of SQL Server through ASP.NET
there are times there is a need to insert the null value in the datetime field in insert or update command text in asp.net that time we can use SqlDateTime.Null;

Passing a nulll parameter to the Update/Insert command text in asp.net using c# code:

Datepage.aspx
DatePage
NameSpace used


using System.Data.SqlTypes;



protected void btnUpdate_Click(object sender, EventArgs e)
{
SqlConnection sCon =null;
SqlCommand sCmd =null;
try
{

sCon = new SqlConnection("server=localhost;database="";uid=sa;pwd=;");
sCmd = new SqlCommand("insert into TableName (Name,Date) Values(@Name,@Date) ", sCon );
sCmd.Parameters.Add("@Name", SqlDbType.Varchar,20).Value = txt1.Text;
if (txt1.Text != "")
sCmd.Parameters.Add("@Date", SqlDbType.SmallDateTime).Value = Convert.ToDateTime(txt2.Text);
else
sCmd.Parameters.Add("@Date", SqlDbType.SmallDateTime).Value = SqlDateTime.Null;

sCmd.CommandTimeout = 30;
sCmd.ExecuteNonQuery();
catch (Exception ex)
{
MsgMsg(ex.Message);
}
finally
{
if (sCon.State == ConnectionState.Open)
{
sCon.Close();
sCon.Dispose();
}
}
}


Comments



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: