0

I just start learning programming and am trying to input data to sql table however, No data is inputted to ms sql for my code in C#, heres my code:

private void button5_Click(object sender, EventArgs e)
        {
            using (SqlConnection conn = new SqlConnection())
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    try
                    {
                        conn.ConnectionString = "Data Source=DIT-NB1625963\\DIT1625963;" +
                        "database=ApplicationDevelopmentDB;" +
                        "integrated security=true";
                        conn.Open();
                        cmd.Connection = conn;
                        cmd.CommandText = "INSERT INTO game_table (PlayerScore, DealerScore)" +
                        "Values (23,21) ";
                        cmd.ExecuteNonQuery();
                        richTextBox1.Text = "You have created a new record." + Environment.NewLine;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        richTextBox1.Text = "Something wrong has occurred." + Environment.NewLine;
                    }
                    finally
                    {
                        conn.Close();
                        Console.WriteLine("Closed the connection");
                    }
                }



            }
        }
Jason
  • 27
  • 2
  • 3
    I bet the error message is "Something wrong has occurred." – Ňɏssa Pøngjǣrdenlarp Aug 10 '17 at 22:44
  • Are you getting any error? – Akash KC Aug 10 '17 at 22:44
  • Hahahah @Plutonix love question without the actuall error :D – Kenion Aug 10 '17 at 22:46
  • Do a` try{} Catch(Exception ex) {throw ex}` trace it and see whats the ex's value – Masoud Andalibi Aug 10 '17 at 22:46
  • ...or just read the console output – Ňɏssa Pøngjǣrdenlarp Aug 10 '17 at 22:47
  • @Valkyriee you should never do `throw ex;`, you should just do `throw;` if you do include the `ex` it resets the stack trace on you. Also to Jason, you will get a lot more useful information by doing `ex.ToString()` instead of just `ex.Message` you may want to print that to the console instead. – Scott Chamberlain Aug 10 '17 at 22:47
  • Not solution but you can remove `conn.Close();` if you are using `using` statement as it close and dispose connection as soon as it goes out of scope. – Akash KC Aug 10 '17 at 22:49
  • Have a look at this: https://stackoverflow.com/questions/12142806/how-to-insert-records-in-database-using-c-sharp-language – CodexNZ Aug 10 '17 at 22:52
  • heres the error: Cannot insert explicit value for identity column in table 'game_table' when IDENTITY_INSERT is set to OFF. – Jason Aug 10 '17 at 22:58
  • one of the fields has auto increment, can you check column properties? – derloopkat Aug 10 '17 at 23:08
  • yes my PlayerScore column has auto increment – Jason Aug 10 '17 at 23:10
  • If that field has autoincrement, value will be set automatically 1,2,3,4...N, so db engine is not expecting you to pass the value in the insert statement, typically we omit field name and value – derloopkat Aug 10 '17 at 23:14
  • 1
    `INSERT INTO game_table (PlayerScore, DealerScore) VALUES ...` where `PlayerScore` is an identity column obviously fails. I suggest you using third column which serves as autoincrement or just use single column instead: `INSERT INTO game_table (DealerScore) VALUES ...` – Tetsuya Yamamoto Aug 11 '17 at 01:32

0 Answers0