I have a very simple (yet weird) problem. I built a simple program that takes in data and stores it in a SQL database. My code is below. I have a listbox on my form to show me the data that I have added via the button, this data stays even after i close my program, restart the computer, and so on. The problem is, I cannot (for the life of me) find where this data is being stored. I checked all under the Database explorer, and nothing new (other than my 3 original manually entered) entries are there. Any suggestions?
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;
namespace Cookbook
{
public partial class frmMain : Form
{
SqlConnection connection;
string connectionString;
public frmMain()
{
InitializeComponent();
connectionString = ConfigurationManager.ConnectionStrings["Cookbook.Properties.Settings.CookbookConnectionString"].ConnectionString;
PopulateRecipes();
}
private void PopulateRecipes()
{
using (connection = new SqlConnection(connectionString))
using (SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Recipe", connection))
{
DataTable recipeTable = new DataTable();
adapter.Fill(recipeTable);
lstRecipes.DisplayMember = "Name";
lstRecipes.ValueMember = "Id";
lstRecipes.DataSource = recipeTable;
}
}
private void PopulateIngredients()
{
string query = "SELECT a.Name FROM Ingredient a " +
"INNER JOIN RecipeIngredient b ON a.Id = b.IngredientId " +
"WHERE b.RecipeId = @RecipeId";
using (connection = new SqlConnection(connectionString))
using (SqlCommand command = new SqlCommand(query, connection))
using (SqlDataAdapter adapter = new SqlDataAdapter(command))
{
command.Parameters.AddWithValue("@RecipeId", lstRecipes.SelectedValue);
DataTable IngredientTable = new DataTable();
adapter.Fill(IngredientTable);
lstIngredients.DisplayMember = "Name";
lstIngredients.ValueMember = "Id";
lstIngredients.DataSource = IngredientTable;
}
}
private void lstRecipes_SelectedIndexChanged(object sender, EventArgs e)
{
PopulateIngredients();
}
private void addbutton_Click(object sender, EventArgs e)
{
string query = "INSERT INTO Recipe VALUES (@RecipeName, 80, 'blah blah')";
using (connection = new SqlConnection(connectionString))
using (SqlCommand command = new SqlCommand(query, connection))
{
connection.Open();
command.Parameters.AddWithValue("@RecipeName", comboBox1.Text);
command.ExecuteScalar();
}
PopulateRecipes();
}
private void frmMain_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'cookbookDataSet.RecipeIngredient' table. You can move, or remove it, as needed.
this.recipeIngredientTableAdapter.Fill(this.cookbookDataSet.RecipeIngredient);
// TODO: This line of code loads data into the 'cookbookDataSet.Recipe' table. You can move, or remove it, as needed.
this.recipeTableAdapter.Fill(this.cookbookDataSet.Recipe);
}
}
}
My "ingredient" table only has lettuce, tomato, cheese in it when I view it through the database explorer -> show table data, but when I look at the data in the form it shows all of the data that I added earlier.