[Solved] ASP.Net Error: Column name or number of supplied values does not match table definition

micah
 
on Oct 05, 2021 06:05 AM
558 Views

Column name or number of supplied values does not match table definition

Server Error in '/' Application. Column name or number of supplied values does not match table definition. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Column name or number of supplied values does not match table definition.

Source Error:


Line 187:         cmd.Parameters.AddWithValue("@Quantity", qty);
Line 188:         con.Open();
Line 189:         i = cmd.ExecuteNonQuery();
Line 190:         con.Close();
Line 191:     }

Database table

CREATE TABLE [dbo].[ItemOrdered](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[Date] [date] NULL,
	[Store] [nvarchar](50) NULL,
	[SellsPerson] [nvarchar](50) NULL,
	[Receipt] [float] NULL,
	[Item] [nvarchar](100) NULL,
	[Price] [numeric](18, 0) NULL,
	[Quantity] [float] NULL,
 CONSTRAINT [PK_ItemOrdered] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[ItemOrdered] ADD  CONSTRAINT [DF_ItemOrdered_Date_Sold]  DEFAULT (getdate()) FOR [Date]
GO

 

private int Insert(string item, string price, string qty)
{
    int i = 0;
    string constr = ConfigurationManager.ConnectionStrings["DB"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        string query = "INSERT INTO ItemOrdered VALUES (@Store,@SellsPerson,@Receipt,@Item, @Price, @Quantity)";
        using (SqlCommand cmd = new SqlCommand(query))
        {
            cmd.Connection = con;
            cmd.Parameters.AddWithValue("@Store", Department.SelectedItem.Text);
            cmd.Parameters.AddWithValue("@SellsPerson", HttpContext.Current.User.Identity.Name);
            cmd.Parameters.AddWithValue("@Receipt", 1);
            cmd.Parameters.AddWithValue("@Item", item);
            cmd.Parameters.AddWithValue("@Price", price);
            cmd.Parameters.AddWithValue("@Quantity", qty);
            con.Open();
            i = cmd.ExecuteNonQuery();
            con.Close();
        }
    }
 
    return i;
}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
arjunv
 
on Oct 05, 2021 06:34 AM

Refer below code. You need to specify the column name in the insert query that you want to insert.

private int Insert(string item, string price, string qty)
{
    int i = 0;
    string constr = ConfigurationManager.ConnectionStrings["DB"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        string query = "INSERT INTO ItemOrdered (Store,SellsPerson,Receipt,Item,Price,Quantity) VALUES (@Store,@SellsPerson,@Receipt,@Item, @Price, @Quantity)";
        using (SqlCommand cmd = new SqlCommand(query))
        {
            cmd.Connection = con;
            cmd.Parameters.AddWithValue("@Store", Department.SelectedItem.Text);
            cmd.Parameters.AddWithValue("@SellsPerson", HttpContext.Current.User.Identity.Name);
            cmd.Parameters.AddWithValue("@Receipt", 1);
            cmd.Parameters.AddWithValue("@Item", item);
            cmd.Parameters.AddWithValue("@Price", price);
            cmd.Parameters.AddWithValue("@Quantity", qty);
            con.Open();
            i = cmd.ExecuteNonQuery();
            con.Close();
        }
    }

    return i;
}