如何从文本框中获取最后2个字符并将其保存在数据库中 [英] How to get every last 2 characters from textbox and save it on database

查看:90
本文介绍了如何从文本框中获取最后2个字符并将其保存在数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



每当我尝试从文本框中获取每个最后2个字符/数字并将其作为用户ID保存在数据库中时,我总是只获取详细信息来自一个文本框。



例如,如果用户插入

名称=用户名

电子邮件= user @ gmail。 com

contact = 1234567

我需要检索所有最后2位数字/字符,它们看起来像meom67

并保存到数据库,但我目前只从第一个文本框获得最后2个字符。



任何想法都将非常感激。提前谢谢



我尝试了什么:



这是我的代码

string strr = tbUname.Text + tbdept.Text + tbContact.Text;

string sub = strr.Substring(strr.Length - 2,2);

cmd.Parameters.AddWithValue(@ Emp_id,sub);

解决方案

尝试:

  private   string  GetLastTwoCharacters( string  s)
{
string result = ;
int len = s.Length;
if (len > 2 )result = s.Substring(len - 2 );
返回结果;
}

然后只需:

  string  sub = GetLastTwoCharacters( tbUname.Text)+ GetLastTwoCharacters(tbdept.Text)+ GetLastTwoCharacters(tbContact.Text); 


你必须调用子串关于员工ID的所有组件的方法。



  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);


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);

解决方案

Try:

private string GetLastTwoCharacters(string s)
    {
    string result = "";
    int len = s.Length;
    if (len > 2) result = s.Substring(len - 2);
    return result;
    }

Then just:

string sub = GetLastTwoCharacters(tbUname.Text) + GetLastTwoCharacters(tbdept.Text) + GetLastTwoCharacters(tbContact.Text);


You have to call the Substring method on all the components of the employe id.

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);


这篇关于如何从文本框中获取最后2个字符并将其保存在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆