0

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user3811284
  • 123
  • 2
  • 7
  • 1
    Can you show your connection string too? – Salah Akbari Jan 07 '16 at 06:17
  • If it is remote SQL server, You will not be able to find in your machine. Check the connection string. – Venkatesh Jan 07 '16 at 06:18
  • Show your connection string – Mateen -ul-haq Jan 07 '16 at 06:22
  • Run your program in debug or add a println(query) after each query so that you get the actual queries passed to your server. Then you can test them manually and see it you get any error. If you don't, then most likely your connection string is wrong. – Thibault Witzig Jan 07 '16 at 06:24
  • First you should use debug mode to operate addbutton_Click method, but I think the connection should be opened before it called by the SqlCommand – iamatsundere181 Jan 07 '16 at 06:43
  • Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Cookbook.mdf;Integrated Security=True is the connectionstring.@user2946329 @Mateen_-ul-haq – user3811284 Jan 07 '16 at 17:34

0 Answers0