I have a web from that has a few text boxes which are visible only under certain conditions. These textboxes are bound to calender extenders.However whenever not visible these pass null string to the database. Their corresponding datatype in the table is datetime. While storing these null strings they are converted to 1/1/1900 12:00:00 AM. How can i have a null value stored here instead ?
Do i need to change the data type to some thing else ??
I’m using datetime because i need to perform some comparison operations later.
Relevant code is here.
public class Asset
{
public string dbStatus { get; set; }
public string wtyEndDate { get; set; }
public string amcEndDate { get; set; }
public Asset()
{
dbStatus = default(String);
wtyEndDate = default(String);
amcEndDate = default(String);
}
}
protected void btnSaveAsset_Click(object sender, EventArgs e)
{
Asset a = new Asset();
a.wtyEndDate = txtWtyEndDate.Text;
a.amcEndDate = txtAmcEndDate.Text;
string msg = "";
if (a.dbStatus == "INSERT")
{
try
{
msg = (ab.EXE_Assets_Master(a).Rows.Count>0) ? "Record Inserted"; : "Error"
}
catch (Exception)
{
msg = "Record with this service tag already exists in the database";
}
}
}
protected DataTable _EXE_Asset_Details(Asset a)
{
Parameters.Clear();
AddParameter("@wtyEndDate", a.wtyEndDate);
AddParameter("@amcEndDate",a.amcEndDate);
AddParameter("@DBStatus", a.dbStatus);
return ExecuteDataSet("[Asset_Details]").Tables[0];
}
// Asset_Details
CREATE PROCEDURE [Asset_Details]
@wtyEndDate datetime = NULL,
@amcEndDate datetime =null,
@dbStatus nvarchar(50)
AS
IF(@DBStatus='INSERT')
BEGIN
INSERT INTO [assetsMaster]
([warrantyEndDate],[amcEndDate])
VALUES
( @wtyEndDate,@amcEndDate)
SELECT @@rowcount
END
Only add the optional
DateTimeparameters if they have data in them.For example: