Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Ideally you would just make TagID an identity field by changing the table definition. If you can't do that, next best would be:</p> <pre><code>CREATE PROCEDURE [dbo].[InsertTagProcdure] @Value nvarchar(200), @TagCount nvarchar(200) AS BEGIN BEGIN TRANSACTION DECLARE @TagID int; SELECT @TagID = coalesce((select max(TagID) + 1 from Tag), 1) COMMIT INSERT INTO Tag (TagID,Value,TagCount) VALUES (@TagID,@Value,@TagCount) END </code></pre> <p>The transaction ensures that you don't end up with unique TagIDs and the coalesce handles the special case where the table is empty and gives an initial value of 1.</p> <p><strong>EDIT:</strong></p> <p>Based on the change to your original question, the table already has an identity column so your stored procedure should be:</p> <pre><code>CREATE PROCEDURE [dbo].[InsertTagProcdure] @Value nvarchar(200), @TagCount nvarchar(200) AS BEGIN INSERT INTO Tag (Value,TagCount) VALUES (@Value,@TagCount) END </code></pre> <p>and your C# code should be</p> <p>int TagID = int.Parse(txtTagID.Text); //This should fall away so auto increment. String Value = txtValue.Text; int TagCount = int.Parse(txtCount.Text); </p> <pre><code> using (var conn = new SqlConnection(Properties.Settings.Default.DBConnectionString)) using (var cmd = conn.CreateCommand()) { conn.Open(); cmd.CommandText = "InsertTagProcdure"; cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@Value", Value); cmd.Parameters.AddWithValue("@TagCount", TagCount); cmd.ExecuteNonQuery(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload