I want to automatically generate a host name whenever user log in into the web.When they click the button log in, the system will automatically create a host name to them and the data automatically save in excel for record. But, the problem is, I want to create a host name including company name eg; PEN + department name eg; IT + any 4 of random number. I'am still new in this asp.net and csharp language. Please help me.Please teach me.
Asked
Active
Viewed 144 times
0
-
What have you tried so far? Use `Random` for the 4-digit number. – ProgrammingLlama Nov 21 '17 at 01:31
-
Unfortunately, your question isn't clear. What do you mean by `host name`? `DNS` / networking context? – EdSF Nov 21 '17 at 01:34
-
@demi By the way, make sure you don't exceed maximum hostname lengths. On Windows, I believe it's 15 bytes. – ProgrammingLlama Nov 21 '17 at 01:36
-
Actually it's a desktop name, sir. For example, when user enter their id, select which department they are from and click the button log in, the system will automatically create a desktop name for them. But, i want to a create a desktop name like for example: PENNIT0897. I want the PENN is not change, the department IT is from the form that user had select before. Only the 4 number at the end of the string changed. – demi Nov 21 '17 at 01:41
1 Answers
1
This would seem to be what you're asking for
Random rand = new Random();
string hostName = companyName + departmentName + rand.Next(1000,9999).ToString();
It's better if Random rand = new Random();
is called once and not everytime you create a new hostName, because you want to avoid any possibility that 2 Random objects would have the same seed.

ProgrammingLlama
- 36,677
- 7
- 67
- 86

Jim W
- 4,866
- 1
- 27
- 43
-
can you please show me in the full code for an example. I still not understand. I'm sorry sir. – demi Nov 21 '17 at 01:45
-
@demi What more do you want him to show? It's pretty much all there. – ProgrammingLlama Nov 21 '17 at 01:45
-
@demi not really sure how to do that since it's going to be specific to how your page is setup. If you're stuck, you need to break the problem down again; like how do I read the value of a control in web forms? Go from there. – Jim W Nov 21 '17 at 01:48
-
@demi ??? He wasn't arguing with you. He was simply stating that you need to break your problem down into manageable chunks. If you have another problem, you should research that separately. For this problem, I even linked you to a fiddle demonstrating this code working. SO is **not** a code-writing service. We can't (and won't) write your entire application for you. – ProgrammingLlama Nov 21 '17 at 01:56
-
if i want to change the department name, do i have to change it as a random also? – demi Nov 21 '17 at 01:57
-
@demi You don't, no. You just have to input it. If you _do_ want it to be random, [see here](https://stackoverflow.com/questions/6695187/pick-random-string-from-array) – ProgrammingLlama Nov 21 '17 at 01:58
-
no sir, i did not said that he is arguing with me. I not meant like that. Sorry if my statement is mistakes. i am not perform in my english. – demi Nov 21 '17 at 01:59
-
i do have my own form sir, i just do not know the calculation to generate pc name. Sorry sir. – demi Nov 21 '17 at 02:01