简体   繁体   English

无法将参数值从字符串转换为DateTime的Int32

[英]Failed to convert parameter value from a String to a Int32 for DateTime

I have a C# form linked to an SQL database, and for one of the columns I wish to insert the current date each time a new row is added. 我有一个链接到SQL数据库的C#表单,对于其中一列,我希望每次添加新行时都插入当前日期。 I am baffled because at one point this code was doing exactly what I wanted, and then after changing a few things it now keeps throwing the error "Failed to convert parameter value from a String to a Int32." 我不知所措,因为这段代码在某一时刻确实在执行我想要的操作,然后在更改了几处内容之后,现在仍然抛出错误“无法将参数值从String转换为Int32”。 and I don't know what happened. 而且我不知道发生了什么。 Here is my table definition. 这是我的表定义。 I know the value types are probably a bit arbitrary, but all I care about is that the date is stored as a string. 我知道值类型可能有点随意,但是我只关心日期存储为字符串。

CREATE TABLE [dbo].[InvTable] (
[ID]       INT           IDENTITY (1, 1) NOT NULL,
[Item]     NVARCHAR (50) NULL,
[QuantRem] INT           NULL,
[Type]     NCHAR (10)    NULL,
[DateOrd]  NCHAR(10)    NULL,
[QuantOrd] INT           NULL,
[Received] BIT           NULL,
[DateRec]  NCHAR(10)    NULL,
PRIMARY KEY CLUSTERED ([ID] ASC)
);

And the code on the form is below. 表单上的代码如下。 The second date will be defined differently later, but my primary concern is getting rid of the error in the title. 第二个日期的定义将在以后有所不同,但我主要关心的是消除标题中的错误。

    private void Submitbutton1_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrWhiteSpace(textBox1.Text))
        {
            MessageBox.Show("Cannot Submit Empty Request");
            return;
        }
        if (comboBox1.SelectedItem == null)
        {
            MessageBox.Show("Please Use Drop Down Menu To Select Item Category");
            return;
        }
        SqlDataAdapter da = new SqlDataAdapter();
        da.InsertCommand = new SqlCommand("INSERT INTO InvTable VALUES(@Item, @QuantRem, @Type, @DateReq, @QuantOrd, @Received, @DateRec)", con);
        da.InsertCommand.Parameters.Add("@Item", SqlDbType.NChar).Value = textBox1.Text;
        da.InsertCommand.Parameters.Add("@QuantRem", SqlDbType.Int).Value = textBox2.Text;
        da.InsertCommand.Parameters.Add("@Type", SqlDbType.NChar).Value = comboBox1.Text;
        da.InsertCommand.Parameters.Add("@DateReq", SqlDbType.NChar).Value = DateTime.Today.ToString("d");
        da.InsertCommand.Parameters.Add("@QuantOrd", SqlDbType.Int).Value = 0;
        da.InsertCommand.Parameters.Add("@Received", SqlDbType.Bit).Value = 0;
        da.InsertCommand.Parameters.Add("@DateRec", SqlDbType.NChar).Value = DateTime.Today.ToString("d");
        con.Open();
        da.InsertCommand.ExecuteNonQuery();
        da.SelectCommand = new SqlCommand("SELECT * FROM InvTable WHERE Received=0", con);
        con.Close();
        ds.Clear();
        da.Fill(ds);
        invTableDataGridView.DataSource = ds.Tables[0];
        textBox1.Clear();
    }

So this code used to work at one point, giving me a string "07/20/2015", but now it just gives me an error. 因此,这段代码曾经在某个时候起作用,给了我一个字符串“ 07/20/2015”,但现在却给了我一个错误。

You are a victim of choosing the wrong data type . 您是选择错误数据类型的受害者。

Change your DateOrd and DateRec column types to date type and pass your DateTime.Today directly to your parameterized query (since you keep only date part of a DateTime ). DateOrdDateRec列类型更改为date类型 ,并将DateTime.Today直接传递给参数化查询(因为仅保留DateTime date部分)。

[DateOrd]  date    NULL,
...
...
[DateRec]  date   NULL,

and

da.InsertCommand.Parameters.Add("@DateReq", SqlDbType.Date).Value = DateTime.Today;
da.InsertCommand.Parameters.Add("@DateRec", SqlDbType.Date).Value = DateTime.Today;

Also for QuantRem column, looks like you need to parse textBox2.Text value to int before you pass it as a parameter. 同样对于QuantRem列,看起来您需要将textBox2.Text值解析为int,然后再将其作为参数传递。

da.InsertCommand.Parameters.Add("@QuantRem", SqlDbType.Int).Value = int.Parse(textBox2.Text);

Don't forget to use using statement to dispose your connections, commands and adapters automatically instead of calling Close methods manually. 不要忘记使用using语句来自动处理连接,命令和适配器,而不是手动调用Close方法。

As others have said, don't use text when you could/should be using an actual date type ... it will save you a lot of hassle now and in the future. 正如其他人所说,在可能/应该使用实际date类型时不要使用文本...这样可以为您现在和将来节省很多麻烦。

But your actual error is more likely to be this line: da.InsertCommand.Parameters.Add("@QuantRem", SqlDbType.Int).Value = textBox2.Text; 但是您的实际错误更可能是以下一行: da.InsertCommand.Parameters.Add("@QuantRem", SqlDbType.Int).Value = textBox2.Text;

Where you are taking text and trying to set an integer parameter. 您在哪里获取文本并尝试设置整数参数。 Convert that text to an integer, and then set your integer parameter. 将该文本转换为整数,然后设置您的integer参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将参数值从Int32转换为DateTime - Failed to convert parameter value from a Int32 to a DateTime 无法将参数值从字符串转换为Int32错误 - Failed to convert parameter value from a String to a Int32 error 无法将参数值从字符串转换为Int32 - Failed to convert parameter value from a String to a Int32 无法将参数值从字符串转换为Int32 - Failed To Convert Parameter Value From A String To A Int32 无法将参数值从字符串转换为Int32,并且我的存储过程具有相同的参数 - Failed to convert parameter value from a String to a Int32 and my stored procedure is with the same parameters 在执行存储过程时,无法将参数值从String转换为Int32 - Failed to convert parameter value from a String to a Int32 while executing stored procedure 无法从字符串转换为int32 - Failed to convert from a string to a int32 将特定值从数据库乘以文本框值时,无法将参数值从字符串转换为Int32 - Failed to convert parameter value from a String to a Int32 while multiplying the specific value from database to textbox value “无法将参数值从int64转换为int32” - “Failed to convert parameter value from int64 to int32” 运行代码时出现“无法将参数值从DataRowView转换为Int32”的消息 - “Failed to convert parameter value from a DataRowView to a Int32” message on running a code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM