【问题标题】:passing datetime parameters in C#在 C# 中传递日期时间参数
【发布时间】:2018-10-25 10:08:00
【问题描述】:

我有以下代码,它是将客户详细信息添加到 Windows 窗体并将数据传输到 SQL 的整体代码的一部分。我只是编码新手,我不确定如何修复以下错误:

  Customers customer = new Customers(GlobalVariables.selectedCustomerID,
                           lbCategoryID.Item[cbCategory.SelectedIndex].ToString(),
                           txtFirstName.Text, txtLastName.Text, cbGender.Text,
                           txtAddress.Text,
                           txtSuburb.Text, cbState.Text,
                           int.Parse(txtPostcode.Text).ToString()), dtpBirthdate.Value);

我在“值”下得到一条红线 - 状态为“;预期”

还有客户(在新的之后) - 声明“没有给出与客户所需的形式参数“生日”相对应的参数。客户(int,int,string,string,string,string,string,string,int , 日期时间)'。”

我不确定我需要做什么来解决这个问题。 Birthdate 在客户类中采用 DateTime 格式。

任何帮助将不胜感激。

【问题讨论】:

  • 你的括号太多了...
  • int.Parse(txtPostcode.Text).ToString(), dtpBirthdate.Value);还有一个)
  • int.Parse(txtPostcode.Text).ToString() 后多余的括号需要去掉
  • 问题:你为什么要把string解析成int,然后再转换成stringint.Parse(txtPostcode.Text).ToString()
  • @SeM 让我们一个接一个地处理错误,好吗? ;-)

标签: c# sql datetime


【解决方案1】:

你可以试试这个:

Customers customer = new Customers(GlobalVariables.selectedCustomerID,           lbCategoryID.Item[cbCategory.SelectedIndex],                       txtFirstName.Text, txtLastName.Text, cbGender.Text,                  txtAddress.Text, txtSuburb.Text, cbState.Text, int.Parse(txtPostcode.Text), dtpBirthdate.Value);

已删除 int.Parse(txtPostcode.Text) 和 lbCategoryID.Item[cbCategory.SelectedIndex]; 的额外 .ToString();

【讨论】:

    【解决方案2】:

    至于具体错误:去掉int.Parse(txtPostcode.Text).ToString())后面的流浪括号。编译器正确地认为语句应该在此处结束,但是缺少分号。

    之后,您将不得不处理所有其他错误: 显然,构造函数在前两个参数中需要两个ints。您没有提供两个ints,但第二个参数是string

    另外,您没有为邮政编码提供int

    所以你的所有参数根本不匹配所需的类型。

    【讨论】:

      【解决方案3】:

      看起来您在第二个参数lbCategoryID.Item[cbCategory.SelectedIndex].ToString(), 中发送一个字符串,并且根据错误消息,您应该发送整数。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多