Share via

Error generating invoice number.

ELIRDFUL 185 Reputation points
2023-12-03T14:32:42.5933333+00:00

Hello everyone

 

I am migrating from vb.net to C Sharp.

 

I am trying to generate the invoice number, but I am getting several errors.

using (var cnx = new SqlConnection(VariablesGlobales.CadenaConexion))                 {                     cnx.Open();                      using (SqlCommand cmd = cnx.CreateCommand;                     {                         string maximo;                         var Agregar = new SqlCommand();                          Agregar.Connection = cnx;                         Agregar.CommandType = CommandType.Text;                         Agregar.CommandText = "select Max(CodigoFactura) As maximo FROM TblaFactura2";                          object objeto = Agregar.ExecuteScalar;                         if (objeto is DBNull)                         {                             maximo = 1.ToString();                             txtCodigoFactura.Text = maximo;                         }                          else                         {                             maximo = (Convert.ToInt64(Agregar.ExecuteScalar) + 1L).ToString();                             txtCodigoFactura.Text = maximo;                          }                     }                 } 

User's image

User's image

User's image

User's image

You already know I count on your help.

Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments

Answer accepted by question author

P a u l 10,766 Reputation points
2023-12-03T15:21:24.58+00:00

Unlike in VB you need to use parenthesis to call a method (e.g. cnx.CreateCommand should be cnx.CreateCommand()), like this:

using (SqlCommand cmd = cnx.CreateCommand())
{
	// ...
}
object objeto = Agregar.ExecuteScalar();

Also you're creating two SqlCommand instances (cmd and Agregar) but you're only using one of them (Agregar) so you could get rid of cmd all together.

Was this answer helpful?

1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.