I found lot of questions regarding this matter and I tried everything but couldn't succeed yet.
I need to pass date to my SQL Server database. my code is as follows:
public bool CreatePatient()
{
IDbConnection connection = database.CreateOpenConnection();
IDbTransaction transaction = database.BeginTransaction(connection);
try
{
GenerateRegNo();
SQL_STATEMENT = "INSERT INTO TblPatientRegistration(RegistrationNo, HospitalID, NIC, AdmitDate, BHTNo, WardNo)
VALUES(@RegNo, @HospiatlNo, @NIC, @Admitdate, @BHTNo, @WardNo)";
IDbCommand com = database.CreateCommandTransaction(SQL_STATEMENT, connection, transaction);
com.Parameters.Add(database.CreateParameter("@RegNo", PatientRegistration.RegistrationNo));
com.Parameters.Add(database.CreateParameter("@HospiatlNo", PatientRegistration.HospitalID));
com.Parameters.Add(database.CreateParameter("@NIC", PatientRegistration.NIC));
com.Parameters.Add(database.CreateParameter("@Admitdate", PatientRegistration.AdmitDate));
com.Parameters.Add(database.CreateParameter("@BHTNo", PatientRegistration.BHTNo));
com.Parameters.Add(database.CreateParameter("@WardNo", PatientRegistration.WardNo));
if (com.ExecuteNonQuery() > 0)
{
string updStatement = "Update TblControl set RegNo=RegNo+1";
IDbCommand com2 = database.CreateCommandTransaction(updStatement, connection, transaction);
com2.ExecuteNonQuery();
transaction.Commit();
return true;
}
else
{
transaction.Rollback();
return false;
}
}
catch (Exception)
{
transaction.Rollback();
return false;
}
}
What I have tried:
As mentioned above Link
com.Parameters.Add(database.CreateParameter("@Admitdate", PatientRegistration.AdmitDate.ToString("dd/MM/yyyy")));
and this link
com.Parameters.Add(database.CreateParameter("@Admitdate", "'"+PatientRegistration.AdmitDate+"'"));
then I tried with this link
String UrDate = PatientRegistration.AdmitDate.ToString("dd/MM/yyyy");
System.Globalization.DateTimeFormatInfo dateInfo = new System.Globalization.DateTimeFormatInfo();
dateInfo.ShortDatePattern = "dd/MM/yyyy";
DateTime validDate = Convert.ToDateTime(UrDate, dateInfo);
com.Parameters.Add(database.CreateParameter("@Admitdate", validDate));
These methods throw this exception :
Operand type clash: int is incompatible with date
Value I received PatientRegistration.AdmitDate
from is : {2/25/2013 12:00:00 AM}
I tried with these all methods but couldn't able to save it. please help me to figure out the problem in here.
Thanks...
EDIT :
My database schema :
CREATE TABLE [dbo].[TblPatientRegistration](
[RegistrationNo] [char](10) NOT NULL,
[HospitalID] [char](10) NOT NULL,
[NIC] [char](10) NOT NULL,
[AdmitDate] [date] NOT NULL,
[BHTNo] [varchar](10) NOT NULL,
[WardNo] [varchar](10) NOT NULL,
[ReleaseDate] [date] NOT NULL,
CONSTRAINT [PK_TblPatientRegistration_1] PRIMARY KEY CLUSTERED
(
[RegistrationNo] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
This question may already have an answer here (close vote): -->i tried with the answer that have suggested in there (see option 2), but it seems not working with my problem
Answer: after several investigation i found this problem occurs due to my mistake in database.
i have not passed value to ReleaseDate field and it set to default value
in database. that default value is the one says incompatible with date. So that is the cause for this problem.
any how I'm able to pass my value as mentioned by Ashok kumar