I am working with a ASP.NET & C#
application residing in an windows server
with windows server 2008 R2
Operating System, the server I am using is a VM
. I changed my windows datetime format to "en-US"
and my SQL server datetime format, regional settings datetime format is showing as "MM/dd/yyyy"
but withing my .NET application developed with c# is showing datetime format of DateTime.Now
as "dd/MM/yyyy"
in debug mode so i can't save the data to SQL database, it is throwing exception "invalid datetime format"
. I will be highly obliged if anybody can help me with the idea how to change the datetime format of DatetTime.Now within my application.
Here is the code snippet - calling section -
saveCustomerInfo.saveInfo(txtCustomerId.Text, userNmae, objProperties.ProposalNumber.ToString(), "", "", Total_Amount, SERVICE_TAX, STAMP_DUTY, AgentID, DateTime.Now, null, null, "", "", "", product_code, MODE)
called function signature is -
public void saveInfo(string CustomerID, string UserName, string ProposalNo, string PaymentID, string PolicyNumber, int amount, double serviceTax, double stampduty, string Agent_ID, DateTime? ProposalDate, DateTime? PaymentDate, DateTime? PolicyDate, string ClaimNo, string ClaimAmount, string ClaimStatus, int ProductCode, string mode)
The sql Code section is:
public void saveInfo(string CustomerID, string UserName, string ProposalNo, string PaymentID, string PolicyNumber, int amount, double serviceTax, double stampduty, string Agent_ID, DateTime? ProposalDate, DateTime? PaymentDate, DateTime? PolicyDate, string ClaimNo, string ClaimAmount, string ClaimStatus, int ProductCode, string mode)
{
string connection = SetConnectionString(SPContext.Current.Site);
SqlConnection con = new SqlConnection(connection);
SqlCommand com = new SqlCommand("Select count(ProposalNo) from [aspnetdb].[dbo].[user_info_log] where ProposalNo = '" + ProposalNo + "'", con);
con.Open();
Int32 count = (Int32)com.ExecuteScalar();
if (count == 0)
{
//string strFormattedProposalDate = ProposalDate.ToString().Split('/')[1] + "/" + ProposalDate.ToString().Split('/')[0] + "/" + ProposalDate.ToString().Split('/')[2];
com.Dispose();
com = new SqlCommand("insert into user_info_log(CustomerID,UserName,ProposalNo,PaymentID,PolicyNo,TotalAmount,ServiceTax,Stampduty,Agent_ID,ProposalDate,PaymentDate,PolicyDate,ClaimNumber,ClaimAmount,ClaimStatus,ProductCode,Mode) VALUES('" + CustomerID + "','" + UserName + "','" + ProposalNo + "','" + PaymentID + "','" + PolicyNumber + "','" + amount + "','" + serviceTax + "','" + stampduty + "','" + Agent_ID + "','" + ProposalDate + "','" + System.DBNull.Value + "','" + System.DBNull.Value + "','" + ClaimNo + "','" + ClaimAmount + "','" + ClaimStatus + "','" + ProductCode + "','" + mode + "')", con);
com.ExecuteNonQuery();
}
con.Close();
com.Dispose();
}
The error code is: The conversion of a varchar data type to a datetime data type resulted in an out-of-range value. The statement has been terminated.