I got a datagridview, when user select a few roll records and insert to another table. So it can insert multiple record at a time.
So I'm provide 2 field with auto generated number to be primary of table.
1 is LoadingNo and another is RecNo.
Therefore, when a batch records(different PO number) insert in one time, so that they have same LoadingNo but different RecNo. The LoadingNo format is yy/MM/0001 and RecNo is 1. and second record can be LoadingNo:11/03/0001 RecNo : 2.
I'm already have LoadingNo Code. Now I want to know how to make the record have same auto generate LoadingNo but different RecNo.
Below are the code for Loading No
private void Form2_Load(object sender, EventArgs e)
{
OleDbConnection conAuthor;
ConnectionStringSettings conSettings = ConfigurationManager.ConnectionStrings["MyDBConnectionString_Loading"];
string name = conSettings.ProviderName;
string providerName = conSettings.ProviderName;
string ConnectionString = conSettings.ConnectionString;
conAuthor = new OleDbConnection(ConnectionString);
OleDbCommand command = new OleDbCommand();
command.Connection = conAuthor;
command.CommandText = "SELECT MAX(LoadingNo) AS LoadingNo FROM LoadItem";
command.CommandType = CommandType.Text;
conAuthor.Open();
OleDbDataReader dr = command.ExecuteReader();
while (dr.Read())
{
textBox1.Text = dr["LoadingNo"].ToString();
}
dr.Close();
conAuthor.Close();
string record = textBox1.Text;
record = CalculateNewRecordNumber(record);
//let textbox display date format
textBox2.Text = DateTime.Now.ToShortDateString();
string strFormat = "dd/MM/yy";
textBox2.Text = DateTime.Now.ToString(strFormat);
textBox2.Text = record;
}
private string CalculateNewRecordNumber(string record)
{
string[] data = record.Split('/');
DateTime currentDate = DateTime.Today;
int recordYear = int.Parse(data[0]);
recordYear = int.Parse("20" + recordYear);
int recordMonth = int.Parse(data[1]);
if (currentDate.Year == recordYear)
{
if (currentDate.Month == recordMonth)
{
//year and month are the same, we only have to increment the number:
int number = int.Parse(data[2]);
//do the increment of the record number:
number++;
//create new record:
record = recordYear + "/" + recordMonth + "/";
string _recNumberOnly = number.ToString();
//loop to create 4 digits number!
for (int i = 0; i < 4; i++)
{
if (_recNumberOnly.Length == 4)
break;
else
_recNumberOnly = "0" + _recNumberOnly;
}
record += _recNumberOnly;
}
else
{
//there is a new month!
//increment a month (year stays the same) and starts with number 0001:
recordMonth++;
//record = recordYear + "/" + recordMonth + "/0001";
//year and month are the same, we only have to increment the number:
int number = int.Parse(data[2]);
//do the increment of the record number:
number++;
//create new record:
record = string.Format("{0:yy}", DateTime.Now) + "/" + string.Format("{0:MM}", DateTime.Now) + "/";
string _recNumberOnly = number.ToString();
//loop to create 4 digits number!
for (int i = 0; i < 4; i++)
{
if (_recNumberOnly.Length == 4)
break;
else
_recNumberOnly = "0" + _recNumberOnly;
}
record += _recNumberOnly;
}
}
else
{
//there is a new year!
//increment a year and start from month 1 and starts with number 0001:
recordYear++;
//record = recordYear + "/01/0001";
//year and month are the same, we only have to increment the number:
int number = int.Parse(data[2]);
//do the increment of the record number:
number++;
//create new record:
record = string.Format("{0:yy}", DateTime.Now) + "/" + string.Format("{0:MM}", DateTime.Now) + "/";
string _recNumberOnly = number.ToString();
//loop to create 4 digits number!
for (int i = 0; i < 4; i++)
{
if (_recNumberOnly.Length == 4)
break;
else
_recNumberOnly = "0" + _recNumberOnly;
}
record += _recNumberOnly;
}
return record;
}
}