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. 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." 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.