I need run auto number with function max or it’s work.
I’m use Import file excel to datagridview and Generate field ‘refId’ for not duplicate.
Code:
int count = 1;
sb = new StringBuilder();
sb.Append("SELECT COUNT(*) FROM tableAset");
string sql = sb.ToString();
cmd.CommandText = sql;
cmd.CommandType = CommandType.Text;
cmd.Connection = Conn;
count = (int)cmd.ExecuteScalar();
int newCount = count;
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
newCount = newCount + 1;
dataGridView1.Rows[i].Cells[0].Value = cbType.Text + "000" + newCount;
dataGridView1.Rows[i].Cells[8].Value = cbType.Text;
dataGridView1.Rows[i].Cells[9].Value = "000" + newCount;
}
cmd.ExecuteNonQuery();
Result now.
refId | typeId | autoNum
M0005 | M | 5
M0006 | M | 5
T0001 | T | 1
T0002 | T | 2
Y0003 | Y | 3
Y0004 | Y | 4
But I need result:
Ex.
- Import First time.
refId | typeId | autoNum
M0001 | M | 1
M0002 | M | 2
T0001 | T | 1
T0002 | T | 2
Y0001 | Y | 1
Y0002 | Y | 2
- Import Second time.
refId | typeId | autoNum
M0003 | M | 3
M0004 | M | 4
T0003 | T | 3
T0004 | T | 4
Y0003 | Y | 3
Y0004 | Y | 4
this is summarize in database.
refId | typeId | autoNum
M0001 | M | 1
M0002 | M | 2
M0003 | M | 3
M0004 | M | 4
T0001 | T | 1
T0002 | T | 2
T0003 | T | 3
T0004 | T | 4
Y0001 | Y | 1
Y0002 | Y | 2
Y0003 | Y | 3
Y0004 | Y | 4
I’m sorry question frequently.
thanks you so much for you time. :))
1 Answer