Inserting a New Row with an Auto-Incremented Primary Key Using the AseCommand Object

Use the AseCommand object to insert a new row with an auto-incremented primary key.

  1. Connect to the database:

    For C#:

    AseConnection conn = new AseConnection( c_connStr );
    conn.Open();

    For Visual Basic .NET:

    Dim conn As New AseConnection( _
       c_connStr )
    conn.Open()
  2. Create a new AseCommand object to insert new rows into the DataTable. In the following code, the line int id1 = ( int ) parmId.Value; verifies the primary key value of the row:

    For C#:

    AseCommand      cmd = conn.CreateCommand();
    cmd.CommandText = "sp_adodotnet_primarykey";
    cmd.CommandType = CommandType.StoredProcedure;
    AseParameter parmId = new AseParameter(
       “@p_id“, AseDbType.Integer);
    parmId.Direction = ParameterDirection.Output;
    cmd.Parameters.Add( parmId );
    AseParameter parmName = new AseParameter(
       “@p_name”, AseDbType.Char );
    parmName.Direction = ParameterDirection.Input;
    cmd.Parameters.Add( parmName );
    parmName.Value = "R & D --- Command";
    cmd.ExecuteNonQuery(); 
    int id1 = ( int ) parmId.Value; 
    parmName.Value = "Marketing --- Command";
    cmd.ExecuteNonQuery(); 
    int id2 = ( int ) parmId.Value; 
    parmName.Value = "Sales --- Command"; cmd.ExecuteNonQuery(); 
    int id3 = ( int ) parmId.Value; 
    parmName.Value = "Shipping --- Command"; cmd.ExecuteNonQuery(); 
    int id4 = ( int ) parmId.Value;

    For Visual Basic .NET:

    Dim cmd As AseCommand = conn.CreateCommand()
    cmd.CommandText = "sp_adodotnet_primarykey"
    cmd.CommandType = CommandType.StoredProcedure
    Dim parmId As New AseParameter("@p_id", _ 	   AseDbType.Integer)
    parmId.Direction = ParameterDirection.Output
    cmd.Parameters.Add( parmId )
    Dim parmName As New AseParameter("@p_name", _
       AseDbType.Char)
    parmName.Direction = ParameterDirection.Input
    cmd.Parameters.Add(parmName )
    
    parmName.Value = "R & D --- Command"
    cmd.ExecuteNonQuery()
    Dim id1 As Integer = parmId.Value
    parmName.Value = "Marketing --- Command"
    cmd.ExecuteNonQuery()
    Dim id2 As Integer = parmId.Value
    parmName.Value = "Sales --- Command"
    cmd.ExecuteNonQuery()
    Dim id3 As Integer = parmId.Value
    parmName.Value = "Shipping --- Command"
    cmd.ExecuteNonQuery()
    dim id4 As Integer = parmId.Value
  3. Bind the results to the grid on the window, and apply the changes to the database:

    For C#:

    cmd.CommandText = "select * from " +     
       "adodotnet_primarykey"; 
    cmd.CommandType = CommandType.Text; 
    AseDataReader dr = cmd.ExecuteReader(); dataGrid.DataSource = dr;

    For Visual Basic .NET:

    cmd.CommandText = "select * from " + _
       "adodotnet_primarykey"
    cmd.CommandType = CommandType.Text
    Dim dr As AseDataReader = cmd.ExecuteReader()
    dataGrid.DataSource = dr
  4. Close the connection:

    For C#:

    conn.Close();

    For Visual Basic .NET:

    conn.Close()