生成像“50VIABC001”这样的代码,如果值“ABC”再次出现而不是追加“002”,“003”等等。

问题描述 投票:0回答:1

当用户点击任何名称时,我有一个包含许多名称的网格,它将该名称的详细信息绑定到文本框,下拉列表等,

在这里我有一个文本框,为该名称生成代码。

我正在生成这样的代码。前4位“50VI”将保持相同,然后是每个'firstName','MiddleName'和'LastName'的1个字母。最后,它应该有从'001','002'开始的3位数字,依此类推。

最后的3位数应根据第一个中间名和姓氏的3个字母决定。

示例:50VIFMS001其中F = FirstName M = MiddleName S = SirName ..如果其他人具有相似的名称格式,则代码应为“50VIFMS002”,依此类推。

我已经在Vb.NET中编写了代码,但现在我得到的值是“50VIFMS000”,其中“000”在任何地方都是相同的。所以我坚持使用最后3位数部分。

Public Function GenNewCode()

    Dim code As String = "50VI" & String.Concat(txtFName.Text.Trim, 
          txtMName.Text.Trim, txtLName.Text.Trim).ToUpper().Substring(0, 3)

    Dim exec As New ExecuteQuery

    Dim CNT As String = exec.ExecuteScalar("SELECT COUNT(*) FROM ADMDOCTMST WHERE DOCTCODE LIKE '" & code & "%'")

    txtNewCode.Text = code & CNT.PadLeft(2, "0")

    Return code

End Function
vb.net gridview data-binding
1个回答
1
投票

首先,打开Option Strict。

Public Function GenNewCode()

函数具有返回类型。

Dim code As String = "50VI" & String.Concat(txtFName.Text.Trim,  txtMName.Text.Trim, txtLName.Text.Trim).ToUpper().Substring(0, 3)

检查括号。 .ToUpper和.Substring作用于连接字符串;不是每个字符串都是单独的,所以你得到第一个名字中的前三个字符。

txtNewCode.Text = code & CNT.PadLeft(2, "0")

您正在001开始序列,因此如果您的查询返回计数3.那将是001,002,003。您需要递增计数以获得新值。此外,.PadLeft最多只能工作9.检查增量值的长度,然后相应地填充。

Return code

您没有更改自原始Dim代码以来的代码值,因此您无法获得预期的值。将代码&...设置为文本框不会更改代码的值。

Public Function GenNewCode() As String
        Dim FName As String = "Mary"
        Dim MName As String = "ruth"
        Dim LName As String = "Smith"
        Dim FInitial As String = FName.Substring(0, 1).ToUpper
        Dim MInitial As String = MName.Substring(0, 1).ToUpper
        Dim LInitial As String = LName.Substring(0, 1).ToUpper
        Dim code As String = "50VI" & FInitial & MInitial & LInitial
        Dim exec As New ExecuteQuery
        Dim CNT As Integer = CInt(exec.ExecuteScalar("SELECT COUNT(*) FROM ADMDOCTMST WHERE DOCTCODE LIKE '" & code & "%'"))
        CNT += 1
        Dim cntStr As String = CStr(CNT)
        Dim Padding As Integer = 3 - cntStr.Length 'You want a total length of 3 so subtract existing length from 3
        code &= cntStr.PadLeft(Padding, CChar("0"))
        Return code
End Function
© www.soinside.com 2019 - 2024. All rights reserved.