1

I am pretty new to C#. I am trying to create a WinForms app where you can enter personal info and it gets stored in a SQL table. The code runs without a hitch, but afterwards the new entry is nowhere to be found. I created the database in Visual Studio.

I ran the debugger and found another .mdf file in the debug folder. When I copied it and opened it in SSMS I find my new entry, but not in the original DB. How do get the entries to be stored in the proper db file?

public partial class Form3 : Form
{
    SqlConnection cnConnection = new SqlConnection(Properties.Settings.Default.VotingDbConnectionString);

    private SqlCommand cmd;

    private void Button1_Click(object sender, EventArgs e)
    {
        if (checkBox.Checked)
        {
            var pId = pIdTextBox.Text;
            var fName = fNameTextBox.Text;
            var lName = lNameTextBox.Text;
            var dob = dobTexbox.Text;
            var gender = genderTexbox.Text;
            cnConnection.Open();

            cmd = new SqlCommand("INSERT INTO tbl_People(pId, fName, lName, dob, gender) VALUES('"+pId+"', '"+fName+ "', '"+lName+ "', '"+dob+"', '"+gender+"')", cnConnection);
            cmd.ExecuteNonQuery();
            MessageBox.Show("Data has been saved.");
            cnConnection.Close();
        }
GSerg
  • 76,472
  • 17
  • 159
  • 346
CSP
  • 53
  • 10
  • 2
    `cmd.ExecuteNonQuery();` returns the number of rows affected. see if its returning 1 or 0. – Milind Anantwar Jul 19 '19 at 08:11
  • During debugging? – CSP Jul 19 '19 at 08:19
  • `int recordsAffetcted = cmd.ExecuteNonQuery()` Like this – Milind Anantwar Jul 19 '19 at 08:20
  • It says DbCommand.ExecuteNonQuery() = 1 – CSP Jul 19 '19 at 08:22
  • 1
    Please see https://stackoverflow.com/q/332365/11683 before you proceed. – GSerg Jul 19 '19 at 08:22
  • 1
    As for the original problem, examine your `VotingDbConnectionString`. – GSerg Jul 19 '19 at 08:27
  • Okay, i will fix the injection issue. The connection string is: "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\VotingDb.mdf;Integrated Security=True" – CSP Jul 19 '19 at 08:34
  • So far, from the code provided, the only problematic thing is the SQL injection. – Cleptus Jul 19 '19 at 08:49
  • Yes i have been told, haha. The issue seems that VS creates a copy of the original db-file at runtime and inserts the values, but they dont end up in the original file. – CSP Jul 19 '19 at 08:53
  • Indeed, it treats the db file as a resource which gets copied to the build output folder. – Julia Hayward Jul 19 '19 at 08:56
  • So how do i save the entries so the next time i run the program i can access the new rows? – CSP Jul 19 '19 at 08:59
  • Possible duplicate of [All data from database is deleted when closing the application](https://stackoverflow.com/questions/36245933/all-data-from-database-is-deleted-when-closing-the-application) – GSerg Jul 19 '19 at 09:23

1 Answers1

0

I figured it out. If i changed my ConnectionString from:

"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\VotingDb.mdf;Integrated Security=True"

To:

Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\WHERE THE FILE IS LOCATED\VotingDb.mdf;Integrated Security=True

Thanks for the help people. Especially @GSerg and @Julia Hayward for pointing me in the right direction and confirming my suspicion.

CSP
  • 53
  • 10