I have a SQL command to add data from a collection to my SQL database. Everything seems to be working fine except when I try to pass non number characters into my Description field on the DB table. Seems like it's not taking string values.
My Collection class
public class LotData
{
public string Lot;
public string Description { get; set; }
public int PO { get; set; }
public string MfgPart { get; set; }
}
My SQL Command
private const string strConn = "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\Users\\Matthew\\QCast.mdf;Integrated Security=True;Connect Timeout=30";
private void Button_Click_1(object sender, RoutedEventArgs e)
//send data from DataGrid to database
{
using (var conn = new SqlConnection(strConn))
{
conn.Open();
try
{
foreach (var lotData in lot)
{
using (var command = new SqlCommand("INSERT into LotData Values (@LOTnum, @WOnum, @POnum, @Description, @MFGpartNO)", conn))
{
command.Parameters.AddWithValue("LOTnum", lotData.Lot);
command.Parameters.AddWithValue("WOnum", "50-50-100");
command.Parameters.AddWithValue("POnum", lotData.PO);
command.Parameters.Add("Description",SqlDbType.Text , lotData.Description);
command.Parameters.AddWithValue("MFGpartNO", lotData.MfgPart);
command.ExecuteNonQuery();
}
}
}
finally
{
conn.Close();
}
}
I have an issue with this line -
command.Parameters.Add("Description",SqlDbType.Text , lotData.Description);
The error I get 'cannot covert from stirng to int'. I don't want an int I want a string
I have also tried:
command.Parameters.AddWithValue("Description",lotData.Description);
Then I get the runtime exception - Operand type clash: int is incompatible with text'
My SQL table setup is this:
CREATE TABLE [dbo].[LotData] (
[TransID] INT IDENTITY (1, 1) NOT NULL,
[WOnum] NCHAR (10) NULL,
[LOTnum] NVARCHAR (50) NULL,
[Description] TEXT NULL,
[POnum] INT NULL,
[MFGpartNO] NCHAR (10) NULL,
CONSTRAINT [PK_LotData] PRIMARY KEY CLUSTERED ([TransID] ASC)
How do I resolve this?
insert into t (col1, col2) values ('x1', 'x2')INSERTstatement would be a good start. i.e.INSERT into LotData ([LOTnum], [WOnum], [POnum], [Description], [MFGpartNO]) Values (@LOTnum, @WOnum, @POnum, @Description, @MFGpartNO)