DOB(生年月日)と(参加日)の2つのフィールドがあります。ユーザーが日付を入力した場合、データベースに挿入->日付、そうでない場合は挿入->null
プロパティを次のように定義しました
public DateTime DOB
{
get;
set;
}
public DateTime DOJ
{
get;
set;
}
今 save_click イベントで
IFormatProvider provider = new System.Globalization.CultureInfo("en-CA", true);
String datetime = txtDOB.Text.Trim();
DateTime date = DateTime.Parse(datetime, provider, System.Globalization.DateTimeStyles.NoCurrentDateDefault);
objEmp.DOB = date;
provider = new System.Globalization.CultureInfo("en-CA", true);
datetime = txtDOJ.Text.Trim();
date = DateTime.Parse(datetime, provider, System.Globalization.DateTimeStyles.NoCurrentDateDefault);
objEmp.DOJ = date;
class.cs
param[7] = new Service.SqlParameter();
param[7].ParameterName = "@DOB";
param[7].Value = DOB;
param[7].SqlDbType = Service.SqlDbType.DateTime;
param[8] = new Service.SqlParameter();
param[8].ParameterName = "@DOJ";
param[8].Value = DOJ;
param[8].SqlDbType = Service.SqlDbType.DateTime;
ユーザーが日付を挿入しなかった場合は、null を挿入する必要があります...しかし、このコードでは、 ストア プロシージャのエラーが発生しています
@FK_EmployeeTypeID Numeric(18,0),
@EmployeeName NVARCHAR(50),
@CellNo NVARCHAR(50),
@PhNo NVARCHAR(50),
@Address NVARCHAR(Max),
@Email NVARCHAR(50),
@DOB DATETIME,
@DOJ DATETIME,
@DOR NVARCHAR(12),
@Status NVARCHAR(50),
@Remarks NVARCHAR(Max)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
BEGIN TRY
BEGIN TRANSACTION Employee_Insert
INSERT
INTO
Employee
(
FK_EmployeeTypeID,
EmployeeName,
CellNo,
PhNo,
Address,
Email,
DOB,
DOJ,
DOR,
Status,
Remarks
)
VALUES
(
@FK_EmployeeTypeID,
@EmployeeName,
@CellNo,
@PhNo,
@Address,
@Email,
@DOB,
@DOJ,
@DOR,
@Status,
@Remarks
)
COMMIT TRANSACTION Employee_Insert
SELECT '1'
END TRY
BEGIN CATCH
SELECT 'Error : ' + ERROR_MESSAGE()
ROLLBACK TRANSACTION Employee_Insert
END CATCH
ウェブサービス
[WebMethod]
public string InsUpdDel(string Conn, string ProcName, SqlParameter[] p)
{
try
{
using (SqlConnection cn = new SqlConnection(Conn))
{
if (cn.State == ConnectionState.Open || cn.State == ConnectionState.Broken || cn.State == ConnectionState.Connecting || cn.State == ConnectionState.Executing || cn.State == ConnectionState.Fetching)
cn.Close();
cn.Open();
SqlCommand cmd = new SqlCommand(ProcName, cn);
cmd.CommandType = CommandType.StoredProcedure;
foreach (SqlParameter param in p)
{
cmd.Parameters.Add(param);
}
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
if (dr[0].ToString() == "1")
{
return "1";
}
else
{
return dr[0].ToString();
}
dr.Close();
cn.Close();
}
}
catch (Exception ex)
{
return "Error : " + ex.Message.ToString();
}