0

I'm using a Windows Forms app (which I'm building) and I'm trying to execute a SQL query (using the SqlCe class), and although I get a message that a row has been affected (insert), when I try to view the table data it's empty.

Here's my code:

string ifNotExists = String.Format("SELECT * FROM users WHERE username='{0}' OR email_addr='{1}' OR phone_num='{2}'", username, email_addr, phone_num);

using (SqlCeCommand cmd = new SqlCeCommand(ifNotExists, s))
{
    SqlCeDataReader reader = cmd.ExecuteReader();

    if (!reader.Read())
    {
        //can proceed
        string reg_cmd = String.Format("INSERT INTO users(username, password, email_addr, phone_num) VALUES('{0}', '{1}', '{2}', '{3}')", username, password, email_addr, phone_num);

        using (SqlCeCommand reg_cmd_cmd = new SqlCeCommand(reg_cmd, s))
        {
            int a = reg_cmd_cmd.ExecuteNonQuery();

            if (a == 1)
            {
                MessageBox.Show("It went through successfully!");
            }
            else
            {
                MessageBox.Show("Some error occured");
            }
        }
    }
} 

What am I doing wrong?

Thanks in advance!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
iliyaitsme
  • 71
  • 5
  • is your database located in the same location where your executable is? – DrewJordan Apr 17 '15 at 19:42
  • Of course, I have this covered @RahulTripathi – iliyaitsme Apr 17 '15 at 19:44
  • 1
    After the insert line runs, have you opened a query tool of some sort and checked the contents of the table `users`? After running successfully, they table should now have your data. If you don't see it, you are not connecting to where you think your code is connected . – StarPilot Apr 17 '15 at 19:54
  • 3
    Watch out for [SQL injection](http://en.wikipedia.org/wiki/SQL_injection) in this code. – Joe Farrell Apr 17 '15 at 19:55
  • @StarPilot Yeah I use the visual studio db editor.. weird, but I double checked i'm checking the right thing – iliyaitsme Apr 17 '15 at 22:05
  • @JoeFarrell Yeah of course, will add it eventually – iliyaitsme Apr 17 '15 at 22:05
  • so, it says it succeeded but you can't see the changes either manually or in your program... it really does sound like your insert it connecting to a different DB than you think. Have you checked all folders for a second DB file? If you manually insert rows from VS DB editor, will they show up in your application? – DrewJordan Apr 19 '15 at 13:48
  • You need to tell us your connection string and what “thing” you “double checked”. Ensure you are looking in your [deployed data](http://stackoverflow.com/a/19254771/22437) and not your [source data](http://stackoverflow.com/a/13313075/22437). – Dour High Arch Sep 18 '15 at 21:14

0 Answers0