altinkadeh
Centipat
- Katılım
- 7 Ocak 2023
- Mesajlar
- 9
| Name | Value | Type | |
|---|---|---|---|
| ◢ | $exception | {"Column name or number of supplied values does not match table definition."} | System.Data.SqlClient.SqlException |
Hatası alıyorum.
Kod:
public partial class Form1 : Form
{
public object productDal;
public Form1()
{
InitializeComponent();
}
ProductDal _productDal = new ProductDal();
private void Form1_Load(object sender, EventArgs e)
{
dgwProducts.DataSource = _productDal.GetAll();
}
private void btnAdd_Click(object sender, EventArgs e)
{
_productDal.Add(new Product
{
Name =tbxName.Text,
UnitPrice = Convert.ToDecimal(tbxUnitPrice.Text),
StockAmount = Convert.ToInt32(tbxStockAmount.Text)
});
MessageBox.Show("Product added!");
}
}
public class ProductDal
{
SqlConnection _connection = new SqlConnection(@"server =(localdb)\mssqllocaldb;initial catalog=ETrade;integrated security=true");
public List<Product> GetAll()
{
ConnectionControl();
SqlCommand sqlCommand = new SqlCommand("Select * from Products", _connection);
SqlDataReader reader = sqlCommand.ExecuteReader();
List<Product> products = new List<Product>();
while (reader.Read())
{
Product product = new Product
{
Id = Convert.ToInt32(reader["Id"]),
Name = (reader["Name"].ToString()),
StockAmount = Convert.ToInt32(reader["StockAmount"]),
UnitPrice = Convert.ToDecimal(reader["UnitPrice"])
};
products.Add(product);
}
reader.Close();
_connection.Close();
return products;
}
private void ConnectionControl()
{
if (_connection.State == ConnectionState.Closed)
{
_connection.Open();
}
}
public void Add(Product product)
{
ConnectionControl();
SqlCommand command = new SqlCommand("Insert into Products values(@name,@unitprice,@stockamount)",_connection);
command.Parameters.AddWithValue("[USER=156989]@Name[/USER]" ,product.Name);
command.Parameters.AddWithValue("@unitprice", product.UnitPrice);
command.Parameters.AddWithValue("@stockamount", product.StockAmount);
command.ExecuteNonQuery();
_connection.Close();
}
Amacım Datagridwiew'a database'e yeni bir ürün ekleyebilmek. Fakat executenonquery hatası alıyorum, yardımcı olabilir misiniz?