Çözüldü 'System.Data.SqlClient.SqlException' hatası

Bu konu çözüldü olarak işaretlenmiştir. Çözülmediğini düşünüyorsanız konuyu rapor edebilirsiniz.

altinkadeh

Centipat
Katılım
7 Ocak 2023
Mesajlar
9
NameValueType
$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?
 
C#:
public partial class Form1 : Form
{
    private ProductDal _productDal = new ProductDal();

    public Form1()
    {
        InitializeComponent();
    }

    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
{
    private const string CONNECTION_STRING = @"server=(localdb)\mssqllocaldb;initial catalog=ETrade;integrated security=true";
    private SqlConnection _connection = new SqlConnection(CONNECTION_STRING);

    public List<Product> GetAll()
    {
        Connect();
        SqlCommand command = new SqlCommand("SELECT * FROM Products", _connection);
        SqlDataReader reader = command.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;
    }

    public void Add(Product product)
    {
        Connect();
        SqlCommand command = new SqlCommand("INSERT INTO Products (Name, UnitPrice, StockAmount) VALUES (@name, @unitprice, @stockamount)", _connection);
        command.Parameters.AddWithValue("@name", product.Name);
        command.Parameters.AddWithValue("@unitprice", product.UnitPrice);
        command.Parameters.AddWithValue("@stockamount", product.StockAmount);
        command.ExecuteNonQuery();
        _connection.Close();
    }

    private void Connect()
    {
        if (_connection.State == ConnectionState.Closed)
        {
            _connection.Open();
        }
    }
}

Bu şekilde dener misiniz?
 

Bu konuyu görüntüleyen kullanıcılar

Technopat Haberler

Yeni konular

Yeni mesajlar

Geri
Yukarı