0

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.

4

2 回答 2

1

您需要更改应用程序中使用的文化。将此添加到system.webweb.config 部分:<globalization culture="en-US" /> 此外,如果您不想更改所有应用程序的默认文化,您可以将 en-US 文化传递给DateTime.ToString方法:var str = DateTime.Now.ToString(CultureInfo.GetCultureInfo("en-US"))

除此之外,将 DateTime 值作为字符串发送到数据库是绝对错误的方法。考虑在您的 sql 命令中使用参数:http ://csharp-station.com/Tutorial/AdoDotNet/Lesson06 这样您就不会将 DateTime 转换为字符串

于 2013-04-17T04:48:09.193 回答
0

然后尝试

this.TextBox3.Text = DateTime.Now.ToString("MM.dd.yyyy");

或者试试这个......

string format = "MM/dd/yyyy hh:mm:ss.fff";
DateTime d = DateTime.ParseExact("05/15/2012 10:09:28.650",
                                format,
                                System.Globalization.CultureInfo.InvariantCulture)
于 2013-04-17T05:02:32.383 回答