Click here to Skip to main content
15,877,574 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

whenever i try to get every last 2 characters/digits from textbox and save it on database as User ID ,i am always getting the details only from one textbox.

for example if user insert
Name=username
Email= user@gmail.com
contact=1234567
Than i need to retrieve all the last 2 digits/char which will look like "meom67"
and save it to database,but i am currently getting last 2 character only from first textbox.

Any idea will be much appreciated. Thank you in advance

What I have tried:

Here's my code
string strr = tbUname.Text + tbdept.Text + tbContact.Text;
string sub = strr.Substring(strr.Length - 2,2);
cmd.Parameters.AddWithValue("@Emp_id", sub);
Posted
Updated 26-May-16 22:01pm
Comments
glen205 27-May-16 4:09am    
Despite the post of 2 solutions, I worry for the problem you're trying to solve.

Making keys that should be unique in the data domain, out of real-world data that may well not be unique, can cause conflicts. In your example, if someone else already has the same last-2 characters in name (Joan == Alan), email domain (AbsolutelyAnything.com == AbsolutelyAnythingElse.com) and ending digits of phone number, then they can't use your system. A person can't actually change any of these things at will to accommodate your restrictions. Unlikely scenarios happen more often than we'd like to think.

Consider adding an additional numeric sequence to the end to keep it unique for Joan and Alan (although even this has limits, do you use 2 additional chars and limit yourself to 99 Joans/Alans?)

Also, make sure you're not using "meom67" as the primary key on your users database table. Google "Surrogate Key vs Natural Key" for reasons why. Store "meom67" but also have a seeded incremental integer ID column on the table or things will get sticky.

Good luck,
Glen
Matt T Heffron 27-May-16 17:23pm    
Virtual +5
BillWoodruff 27-May-16 4:27am    
I agree with Glen's comments that you are making a mistake in generating a unique user id this way. I suggest you calculate your own unique identifiers, and then map that to user-names, etc.

Try:
C#
private string GetLastTwoCharacters(string s)
    {
    string result = "";
    int len = s.Length;
    if (len > 2) result = s.Substring(len - 2);
    return result;
    }
Then just:
C#
string sub = GetLastTwoCharacters(tbUname.Text) + GetLastTwoCharacters(tbdept.Text) + GetLastTwoCharacters(tbContact.Text);
 
Share this answer
 
You have to call the Substring method on all the components of the employe id.

C#
string nam = tbUname.Text;
string dep = tbdept.Text;
string con = tbContact.Text;
nam = nam.Substring(nam.Length-2);
dep = dep.Substring(dep.Length-2);
con = con.Substring(con.Length-2);
cmd.Parameters.AddWithValue("@Emp_id", nam+dep+con);
 
Share this answer
 
Comments
Kasar_7 27-May-16 4:36am    
Thank you everyone for awesome suggestion.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900