-1

I am working on my simple task in c# with service based database. I have a service-based database where I have table staff with columns id, name and password. I am trying to insert new record into that table, with C# code, but it's not inserting, just telling me "No Record ADDED" even there is no error.

My code is:

private void button5_Click(object sender, EventArgs e)
{
    try
    {
        connetionString = Properties.Settings.Default.testdbConnectionString;
        cnn = new SqlConnection(connetionString);

        cnn.Open();

        SqlCommand command6;
        string sql6 = null;
        sql6 = "insert into staff (name,pwd,id) values(@n,@p,@fid)";

        command6 = new SqlCommand(sql6, cnn);
        command6.Parameters.AddWithValue("@n", "jhon");
        command6.Parameters.AddWithValue("@p", "test");
        command6.Parameters.AddWithValue("@fid", 1);

        int result = command6.ExecuteNonQuery();

        if (result == 0)
        {
            MessageBox.Show("Record ADDED");
        }
        else
        {
            MessageBox.Show("No Record ADDED");
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}

If you need more detail you my ask but please correct my mistake. Thanks

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
jhon Chin
  • 3
  • 2

1 Answers1

1

The result of ExecuteNonQuery is the number of records affected. Your if statement is the wrong way round... if the result doesn't equal zero then something was inserted.

Scott Perham
  • 2,410
  • 1
  • 10
  • 20
  • still same problem, now new row is inserted in table i checked manually – jhon Chin Jan 28 '18 at 18:58
  • But if you're saying that you see "No Record ADDED" from the code above that means the result of ExecuteNonQuery wasn't zero... From MSDN: "Executes a Transact-SQL statement against the connection and returns the number of rows affected" – Scott Perham Jan 28 '18 at 19:01
  • let forget result of messagebox just tell why he is not inserting values ? – jhon Chin Jan 28 '18 at 19:04
  • I'm afraid I can only comment on what is posted, and from the code above and your description of the observed result, a row _should_ have been added. It's not going to be easy to attempt to determine why a row _wasn't_ added when the API you are using is suggesting otherwise. – Scott Perham Jan 28 '18 at 19:08
  • select query is working fine while insert query have this stupid problem, is it problem of storing new row is in bin\debug\testdb.mdf while selecting from project\testdb.mdf – jhon Chin Jan 28 '18 at 19:13
  • Reading from a different database than you are inserting into would definitely result in what you are seeing... – Scott Perham Jan 28 '18 at 19:15
  • follow this link to solve this type of problem https://stackoverflow.com/questions/31604951/sql-command-insert-is-working-but-the-data-not-appear-in-table – jhon Chin Jan 28 '18 at 19:28
  • i appreciate and admit your try for my help – jhon Chin Jan 28 '18 at 19:29