0

Hi I am trying to get the latest added records id by using below code: This code developed as per This Link

var con = db.OpenConnection();
try
 {
  var refund = "";
  if (rbRef.Checked)
  {
   refund = "Refundable";
  }
  else
  {
   refund = "Non-Refundable";
  }
  var cmd = new SqlCeCommand();
  const string sqlAddNew = @"INSERT INTO Income(iAmount,iByHand,iRecDate,iDesc,pId,iRefundable)"+"OUTPUT INSERTED.iId values (@amount,@byHand,@recDate,@description,@pId,@iRefundable)";

  var amount = txtInAmount.Text.Trim();
  var byHand = txtRecByHand.Text.Trim();
  var recDate = dtpExpenceDate.Value;
  var descrip = txtInDesc.Text.Trim();
  var pId = cmbPayee.SelectedValue;

  cmd=new SqlCeCommand(sqlAddNew,con);
  cmd.Parameters.AddWithValue("@amount", SqlDbType.Float).Value = amount;
  cmd.Parameters.AddWithValue("@byHand", SqlDbType.NVarChar).Value = byHand;
  cmd.Parameters.AddWithValue("@recDate", SqlDbType.DateTime).Value = recDate;
  cmd.Parameters.AddWithValue("@description", SqlDbType.NVarChar).Value = descrip;
  cmd.Parameters.AddWithValue("@pId", SqlDbType.Int).Value = pId;
  cmd.Parameters.AddWithValue("@iRefundable", SqlDbType.NVarChar).Value=refund;
  var count = cmd.ExecuteNonQuery();
  var id = (int) cmd.ExecuteScalar();
  MessageBox.Show(id.ToString());
  if (count > 0)
  {
   MessageBox.Show(@"Record inserted into Income");
   db.CloseConnection(con);
  }
  else
  {
   db.CloseConnection(con);
  }
 }
 catch (Exception e)
 {
  MessageBox.Show(e.ToString());
 }

When I run this code I am getting below error:

There was an error parsing the query. 
[ Token line number = 1,Token line offset = 67,Token in error = OUTPUT ]

I am not getting why I am getting this error and also is this the simplest way to do this or there is any another way to do this.

Community
  • 1
  • 1
Ashok
  • 1,868
  • 6
  • 36
  • 70
  • 1
    See my reply here: http://stackoverflow.com/questions/21514341/error-parsing-the-query-while-getting-identity-from-sql-server-ce/21514589#21514589 – ErikEJ Feb 03 '14 at 07:55
  • Answered by http://stackoverflow.com/questions/21514341/error-parsing-the-query-while-getting-identity-from-sql-server-ce/21514589#21514589 – Ashok May 24 '14 at 12:02

0 Answers0