0

I am trying to insert an integer value into a SQL Server database as below when I run the program there are no any errors, but the table doesn't get updated with values. I have searched on the internet and I am doing the same can anyone help to find what I am doing wrong.

Note: I already defined "connectionString" as a string on the form class

private void btnUpdate_Click(object sender, EventArgs e)
{
        int totalincome=600;
        int totaldeductions = 10;

        connectionString = ConfigurationManager.ConnectionStrings["BudgetApp.Properties.Settings.MainDataBaseConnectionString"].ConnectionString;

        con = new SqlConnection(connectionString);

        con.Open();

        cmd = new SqlCommand("INSERT INTO Totals(TotalIncome, TotalDeductions) VALUES (@TotalIncome, @TotalDeductions)", con);
        cmd.Parameters.AddWithValue("@TotalIncome", totalincome);
        cmd.Parameters.AddWithValue("@TotalDeductions", totaldeductions);

        cmd.ExecuteNonQuery();

        MessageBox.Show("Done !!");
}
7
  • 1
    are you able to see the Message Done !! at the end of execution? Commented Oct 8, 2018 at 20:02
  • yes Done !! Message appeared Commented Oct 8, 2018 at 20:03
  • Any chance there's a TransactionScope in play? Commented Oct 8, 2018 at 20:06
  • 2
    cmd.ExecuteNonQuery(); returns the number of rows affected. If it's greater than zero, it worked. If the database doens't show it, you are looking in the wrong database. Commented Oct 8, 2018 at 20:06
  • What does your connection string look like?? Commented Oct 8, 2018 at 20:09

3 Answers 3

1

The whole AttachDbFileName= approach is flawed - at best! When running your app in Visual Studio, it will be copying around the .mdf file (from your App_Data directory to the output directory - typically .\bin\debug - where you app runs) and most likely, your INSERT works just fine - but you're just looking at the wrong .mdf file in the end!

If you want to stick with this approach, then try putting a breakpoint on the myConnection.Close() call - and then inspect the .mdf file with SQL Server Mgmt Studio Express - I'm almost certain your data is there.

The real solution in my opinion would be to

  1. install SQL Server Express (and you've already done that anyway)

  2. install SQL Server Management Studio Express

  3. create your database in SSMS Express, give it a logical name (e.g. MainDataBase)

  4. connect to it using its logical database name (given when you create it on the server) - and don't mess around with physical database files and user instances. In that case, your connection string would be something like:

    Data Source=.\\SQLEXPRESS;Database=MainDataBase;Integrated Security=True
    

    and everything else is exactly the same as before...

Also see Aaron Bertrand's excellent blog post Bad habits to kick: using AttachDbFileName for more background info.

Sign up to request clarification or add additional context in comments.

1 Comment

Sql Server Express is kind of overkill, though, if he just wants a local datastore for a traditional desktop app.
0

Code Seems correct,Perhaps you are checking the wrong DB?. I would add a Try/catch for exceptions. And remember to close connection after executing query. Regards

Comments

0

check your database column datatype,use try catch.

and try to replace cmd.Parameters.AddWithValue("@TotalIncome", totalincome); to cmd.Parameters.Add("@Number", SqlDbType.Int).Value = totalincome;

try
{
       int totalincome=600;
        int totaldeductions = 10;
        connectionString = ConfigurationManager.ConnectionStrings["BudgetApp.Properties.Settings.MainDataBaseConnectionString"].ConnectionString;
        con = new SqlConnection(connectionString);
        con.Open();
        cmd = new SqlCommand(@"INSERT INTO Totals(TotalIncome, TotalDeductions) VALUES (@TotalIncome, @TotalDeductions)", con);

        cmd.Parameters.Add("@Number", SqlDbType.Int).Value = totalincome;
        cmd.Parameters.Add("@Number", SqlDbType.Int).Value = totaldeductions;
        //cmd.Parameters.AddWithValue("@TotalIncome", totalincome);
        //cmd.Parameters.AddWithValue("@TotalDeductions", totaldeductions);

        cmd.ExecuteNonQuery();
}
catch(Exception ex)
{
  MessageBox.Show(ex.ToString());
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.