title: Auto increment value on adding a row using C# WPF and SQL
body: <p>I'm working with C# WPF in Visual Studio Express 2012.  I've created a local service-based database (<code>.mdf</code> database).</p>
  
  <p>I've created a table with</p>
  
  <pre><code>    CREATE TABLE [dbo].[Scenes] (
           [id]              INT           IDENTITY (1, 1) NOT NULL,
           [Planning_period] NVARCHAR (50) NOT NULL,
           [Scene_name]      NVARCHAR (50) NOT NULL,
           PRIMARY KEY CLUSTERED ([id] ASC)
      );
  </code></pre>
  
  <p>I want to be able to add a new row but the following code doesn't work because I'm leaving id as null.  How do I get it to increment automatically when I add data?</p>
  
  <pre><code>    SqlConnection cn = new SqlConnection(global::WaterfowlModel.Properties.Settings.Default.WAMConnectionString);
      try
      {
          string sql = "INSERT INTO Scenes (Planning_period, Scene_name) Values ('" + working_plan + "','" + Create_plan_txt.Text +"')";
          SqlCommand cmd = new SqlCommand(sql, cn);
          cn.Open();
          cmd.ExecuteNonQuery();
      }
      catch (Exception ex)
      {
          MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK);
      }
      finally
      {
          cn.Close();
      }
  </code></pre>
  
  <p>Thanks!</p>
  
  <p>EDIT</p>
  
  <p>I've attempted this with a stored procedure as well and received the same error.</p>
  
  <pre><code>CREATE PROCEDURE [dbo].[Add_Scene]
      @param1 nvarchar(50),
      @param2 nvarchar(50)
  AS
      INSERT INTO Scenes(Planning_period, Scene_name)
      VALUES(@param1, @param2)
  </code></pre>
  
  <p>and code:</p>
  
  <pre><code>        string sql = "Add_Scene";
          SqlCommand cmd = new SqlCommand(sql, cn);
          SqlParameter planname = new SqlParameter("@param1", working_plan);
          SqlParameter scenename = new SqlParameter("@param2", Create_plan_txt.Text);
  
          cmd.CommandType = CommandType.StoredProcedure;
          cmd.Parameters.Add(planname);
          cmd.Parameters.Add(scenename);
          cn.Open();
          cmd.ExecuteNonQuery();
  </code></pre>