Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 如何在webservice中获取系统日期时间_Asp.net_Sql - Fatal编程技术网

Asp.net 如何在webservice中获取系统日期时间

Asp.net 如何在webservice中获取系统日期时间,asp.net,sql,Asp.net,Sql,我正在尝试在我的Web服务中获取系统日期和时间。但我不知道怎么做 这是我的代码: public void InsertStudentTransaction(string Name, string CLass, string NRIC, string StallNo, string AmountSpent, DateTime Date) { using (SqlConnection conn = new SqlConnection(System.Configuratio

我正在尝试在我的Web服务中获取系统日期和时间。但我不知道怎么做

这是我的代码:

 public void InsertStudentTransaction(string Name, string CLass, string NRIC, string StallNo, string AmountSpent, DateTime Date)
     {
        using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["nConnectionString2"].ConnectionString))
     {
        int StallNo1 = int.Parse(StallNo);
        float AmountSpent1 = float.Parse(AmountSpent);
        Date = DateTime.Now;

        SqlCommand command = new SqlCommand("INSERT Into StudentTransactions (Name, CLass,NRIC,StallNo,AmountSpent, TimeDate) VALUES (@Name, @CLass, @NRIC, @StallNo, @AmountSpent, @Date)", conn);

        command.Parameters.AddWithValue("@Name", Name);
        command.Parameters.AddWithValue("@CLass", CLass);
        command.Parameters.AddWithValue("@NRIC", NRIC);
        command.Parameters.AddWithValue("@StallNo",StallNo1);
        command.Parameters.AddWithValue("@AmountSpent", AmountSpent1);
        command.Parameters.AddWithValue("@TimeDate", Date);

        conn.Open();
        command.ExecuteNonQuery();
     }
  }

从错误响应中,我假设问题不在代码中,而在该方法的寻址中。您可能正在尝试将字符串值解析为
DateTime
格式,有点像这样:

DateTime date = DateTime.Parse(dateAsString);
如果dateAsString变量不包含有效的日期,则将抛出您描述的ArgumentException

您可以通过尝试找出要解析的字符串日期的错误来解决此问题。很可能是一个空字符串导致了问题。要帮助您找到麻烦制造者,请使用抛出新异常并提供一些有用信息:

DateTime date;

if (!DateTime.TryParse(dateAsString, out date)
{
   throw new Exception("Invalid date: " + dateAsString);
}

你已经有了它
DateTime。现在
问题到底是什么?你试过调试你的代码吗?是的,我试过了。我正试着从我的web服务读取系统日期时间并插入到我的数据库中。我试过最下面的代码,但它给了我以下错误:system.ArgumentException:无法转换为system.DateTime。参数名称:类型-->System.FormatException:字符串未被识别为有效的DateTime.System.ArgumentException:无法转换为System.DateTime。参数名称:type-->System.FormatException:字符串未被识别为有效的DateTime嘿,我尝试运行它,但出现以下错误:“System.ArgumentException:无法转换为System.DateTime。参数名称:type-->System.FormatException:字符串未被识别为有效的DateTime。”