0

I have searched through all of the questions I have found here, so I will list the things I have checked: I have LINQ to SQL and a local SQL database. It is saving and showing the entry to the datagrid, but when I close the program and look in the actual database table it isn't saving to the database. I see a primary key, and I have checked that the database is not being saved in the bin or any other temp folder. (Solved that one earlier, thank you to another StackOverflow question!)

This was working earlier when I was just doing a single class, but I have switched to an abstract class (Ingredient) with two sub-classes (HealthyIngredient and RegularIngredient). I am following the tutorials I find, but it isn't saving to the database. The message box is showing 'Success', and I am not getting any errors. I am fairly new to this so I am not sure where to look for what is going wrong. Any help would be greatly appreciated.

 public void SaveRegularIngredient()
    {
        using (RecipeClassesDataContext dbContext = new RecipeClassesDataContext())
        {
            int amountNumber = 0;
            int.TryParse(txtRegAmount.Text, out amountNumber);

            int caloriesNumber = 0;
            int.TryParse(txtRegCalories.Text, out caloriesNumber);

            int fatNumber = 0;
            bool fatValid = int.TryParse(txtRegFat.Text, out fatNumber);

            int carbsNumber = 0;
            bool carbsValid = int.TryParse(txtRegCarbs.Text, out carbsNumber);

            int proteinNumber = 0;
            bool proteinValid = int.TryParse(txtRegProtein.Text, out proteinNumber);

            

            RegularIngredient regularIngredient = new RegularIngredient();

            regularIngredient.Name = txtRegIngrName.Text;
            regularIngredient.Units = listRegUnits.SelectedItem.ToString();
            regularIngredient.Amount = amountNumber;
            regularIngredient.Calories = caloriesNumber;
            regularIngredient.Fat = fatNumber;
            regularIngredient.Carbs = carbsNumber;
            regularIngredient.Protein = proteinNumber;
            regularIngredient.HealthyVariant1 = txtHlthyVar1.Text;
            regularIngredient.HealthyVariant2 = txtHlthyVar2.Text;

            dbContext.Ingredients.InsertOnSubmit(regularIngredient);              
            dbContext.SubmitChanges();
            MessageBox.Show("Recourd is saved", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);

            txtRegIngrName.Text = "";
            txtRegAmount.Text = "1";
            txtRegCalories.Text = "";
            txtRegFat.Text = "";
            txtRegCarbs.Text = "";
            txtRegProtein.Text = "";
            txtHlthyVar1.Text = "";
            txtHlthyVar2.Text = "";
            ResetPage();
        }
    }

ANSWER: When I looked into my App.config, there were two Connections strings. They looked like they went to the same database. The first one was the one I had created before I started using LINQ to SQL, the second I think it added.

    <add name="HealthierRecipes.Properties.Settings.RecipeDB" 
             connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\T\source\repos\Old\HealthierRecipes\HealthierRecipes\RecipeDB.mdf;Integrated Security=True"
            providerName="System.Data.SqlClient" />

<add name="HealthierRecipes.Properties.Settings.RecipeDBConnectionString"
            connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\RecipeDB.mdf;Integrated Security=True"
            providerName="System.Data.SqlClient" />

When I went into the RecipeClass.designer.cs, which is the page that LINQ to SQL uses for mapping everything, I found it using the connections string in only one place, so I tried replacing it with my old one, commenting out the original just in case. Now everything is working perfectly.

public RecipeClassesDataContext() : 
            //base(global::HealthierRecipes.Properties.Settings.Default.RecipeDBConnectionString, mappingSource)
base(global::HealthierRecipes.Properties.Settings.Default.RecipeDB, mappingSource)

Thank you for sending me to check connection strings, that got me looking it the right places. Doing it this way means I don't have to do it each time!

TBaildon
  • 33
  • 1
  • 5
  • Try specifying the connection string when you create the `RecipeClassesDataContext` – Slugart Feb 05 '21 at 22:27
  • It is the same one that I use to retrieve the data for displaying in the datagridview, and all of the tutorials use the same string for retrieval and save/update/delete, but I will try that. Thank you. – TBaildon Feb 05 '21 at 22:42
  • Does this answer your question? [what's the issue with AttachDbFilename](https://stackoverflow.com/questions/11178720/whats-the-issue-with-attachdbfilename) – Charlieface Feb 06 '21 at 21:20
  • 1
    @TBaildon, you can post an reply here and accept it as an answer, which can help others to solve the similar issue. – Jack J Jun Feb 08 '21 at 01:59

0 Answers0