im totally stuck here on creating a random number. Ideally not the same number twice as its a customer number.
I then need to display it in my tb_id (textbox) and write the line into a customer file.
Any help here would be great.
private void button1_Click(object sender, EventArgs e)
{
**var rng = new Random();**
// Here I need to write the random number (it's a customer number) to the tb_id text box. so I'm able to write it to their customer file afterwards.
try
{
string fileName = string.Format(tb_surname.Text);
if (tb_firstname.Text == "" || fileName == "" || tb_postcode.Text == "")
{
MessageBox.Show("Missing values from textboxes!");
}
else if (File.Exists(fileName + "Text"))
{
if (MessageBox.Show("Warning: There is already a file with the surname you enter already on the system. Contents will be replaced - do you want to continue?", "File Demo", MessageBoxButtons.YesNo, MessageBoxIcon.Question,
MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly) == DialogResult.Yes)
{
//write lines of text to file
StreamWriter outputStream = File.CreateText(fileName + ".Txt");
outputStream.WriteLine(tb_firstname.Text);
outputStream.WriteLine(tb_surname.Text);
outputStream.WriteLine(tb_surname.Text);
**outputStream.WriteLine(tb_id.Text);**
outputStream.Close();
MessageBox.Show("Text saved to file successfully!");
this.Close();
}
}
}
// ...
}
part 2
// creating a random number here for the customer id
Guid g = Guid.NewGuid();
tb_id.Text = g.ToString();
to write Guid number to a text file.
outputStream.WriteLine(tb_id.Text);
I would not recomend using the random number if you want to get it unique for users there is a high probability of getting different users with same number.
guid is probably better idea