I have a dilemma with a webpage project that I am doing. You see, it is a record of certain products sold, creating invoice type, then I need a folio. The folio that will be assigned by default to each user will be 1, the number will be saved in the table SVenta that contains ALL the sales data by users, and when you have your first record in that table, count 1, 2, 3 will begin. 4, 5 ... But there is the possibility that constantly that Folio is updated and is where I do not know how to do it. Well, if you update the folio of a user, you must start at that number, save it in the SVenta table and re-count from there. All this is done from a button that saves and updates the folio. The method I use is:
protected void CamFactura()
{
OdbcConnection cn = new OdbcConnection("Dsn=Conexion server;uid=sa;pwd=xxxx");
DataTable dtr = new DataTable();
OdbcCommand cm = new OdbcCommand("SELECT SlpCode FROM Vendedores WHERE SlpName = '" + Request.QueryString["id"] + "'", cn); //Obtiene el ID del usuario que ingreso
OdbcDataAdapter da = new OdbcDataAdapter(cm);
da.Fill(dtr);
cn.Open();
DataRow fila1 = dtr.Rows[0];
if (dtr.Rows.Count == 1)
{
DataTable dt2 = new DataTable();
OdbcCommand cmd2 = new OdbcCommand("SELECT count(noFactura) FROM Factura WHERE SlpCode = '" + Convert.ToString(fila1["SlpCode"]) + "'", cn); //verifica primero si hay un registro en la tabla Factura (el campo 'noFactura' esta en null para todos)
int count = Convert.ToInt32(cmd2.ExecuteScalar());
if (count == 0)//no se encontró registro
{
OdbcCommand cmd3 = new OdbcCommand("SELECT count(noFactura) FROM SVenta WHERE SlpCode = '" + Convert.ToString(fila1["SlpCode"]) + "'", cn); //verifica si se encontró registro dentro de la tabla SVenta
int coun = Convert.ToInt32(cmd3.ExecuteScalar());
if (coun == 0)//no se encontró registro
{
label2.Text = "1"; //le da valor de 1 al folio, éste se guarda como el valor del primer registro
}
else //si hay registros
{
OdbcCommand cmdd = new OdbcCommand("SELECT (max(noFactura) +1) FROM SVenta WHERE SlpCode ='" + Convert.ToString(fila1["SlpCode"]) + "'", cn); //suma uno al último registro encontrado
label2.Text = Convert.ToString(cmdd.ExecuteScalar()); //imprime el valor en el label y lo muestra
}
}
else //si se encontró un registro en la tabla Factura
{
OdbcCommand cmd4 = new OdbcCommand("SELECT (max(noFactura) +1) FROM Factura WHERE SlpCode ='" + Convert.ToString(fila1["SlpCode"]) + "'", cn); //Le suma uno al valor que se encuentra en la tabla
label2.Text = Convert.ToString(cmd4.ExecuteScalar()); //lo imprime en el label
int coun = Convert.ToInt32(cmd4.ExecuteScalar()); //¿De qué manera le digo que me guarde el folio y después tome el de la tabla SVenta
if (coun == 1)
{
//Aquí no se cómo hacer para que guarde ese número y comience a contar de nuevo por el último registro de la tabla SVenta
}
}
}
cn.Close();
}
Also identify when your folio number is updated and redo the same process.
EXAMPLE: User1 with Folio 1 in its first record // update to User1 with Folio 1234 User1 with Folio 1235 in its last record // the Folios are saved consecutively in the SVenta table
PROBLEM: The Code works when in the table INVOICE column noFactura is null, when you add a sheet which is saved the first time, but does not take the last record again, because I have not placed the code that says do it, since it does not I know in what way to do it. Also, I would only take the first update of the FOLIO, but not the others, which is what I want.