I am trying to insert data into a table using SQL. I am inserting the data into a table called Stock. Most of the data is coming from a table called AllStock, and one field from a table called Portfolio.
The user selects the portfolio name and the stock name to be entered into the table. I then have 2 invisible grid views (not needed?) returning the PortfolioId and StockAllId from the two tables. These are both foreign keys in the Stock table. I then need the rest of the fields in the AllStock table associated with the StockAllId (primary key) in the AllStock table.
This will all be called on a button click.
Any help will be appreciated. Please see the code below for what I have so far.
protected void btn_AddStock_Click(object sender, EventArgs e)
SqlConnection Conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString2"].ConnectionString);
SqlCommand cmd = Conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = @"INSERT INTO Stock(StockName, StockSymbol, StockQuantity,
StockBuyPrice, StockBuyDate, PortfolioId, StockAllId)
SELECT ast.StockName, ast.StockSymbol, @StockQuantity, ast.StockPrice, ast.Date, @PortfolioId, ast.StockAllId,
FROM AllStocks AS ast
WHERE ast.StockAllId = @StockAllId";