开发者

vb asp.net string manipulation

Updated Code

VB CODEBEHIND

 Protected Sub cmdclick_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdclick.Click
    Dim strConnString As String = WebConfigurationManager.ConnectionStrings("orca").ConnectionString
    Dim con As New SqlConnection(strConnString)
    Dim cmd As New SqlCommand("usp_validatecard", con)
    cmd.CommandType = Data.CommandType.StoredProcedure


    Dim GetTrack1 As String = ""
    开发者_如何学CDim GetTrack2 As String = ""


    Dim SplitTrack As String = txtTrack.Text

    If SplitTrack.Length.ToString = 19 Then
        GetTrack1 = Mid(SplitTrack, 2, 6)
        GetTrack2 = Mid(SplitTrack, 10, 9)
        cmd.Parameters.Add("@track1", Data.SqlDbType.NVarChar).Value = GetTrack1
        cmd.Parameters.Add("@track2", Data.SqlDbType.NVarChar).Value = GetTrack2

        Try
            con.Open()
            Dim Result As Integer = cmd.ExecuteNonQuery()
            Dim reader As SqlDataReader = cmd.ExecuteReader()
            If reader.Read() Then
                lblmemname.Text = Convert.ToString(reader(0))
                lblmemnum.Text = Convert.ToString(reader(1))
                lbltrack1.Text = Convert.ToString(reader(2))
                lbltrack2.Text = Convert.ToString(reader(3))
                lblmessage.Text = Convert.ToString(reader(4))
            End If
        Catch ex As SqlException
            errmessage.Text = "Error"
        Finally
            con.Close()
        End Try

    ElseIf SplitTrack.Length.ToString = 8 Then
        GetTrack1 = Mid(SplitTrack, 2, 6)
        cmd.Parameters.Add("@track1", Data.SqlDbType.NVarChar).Value = GetTrack1
        Try
            con.Open()
            Dim Result As Integer = cmd.ExecuteNonQuery()
            Dim reader As SqlDataReader = cmd.ExecuteReader()
            If reader.Read() Then
                lblmemname.Text = Convert.ToString(reader(0))
                lblmemnum.Text = Convert.ToString(reader(1))
                lbltrack1.Text = Convert.ToString(reader(2))
                lbltrack2.Text = Convert.ToString(reader(3))
                lblmessage.Text = Convert.ToString(reader(4))
            End If
        Catch ex As SqlException
            errmessage.Text = "Error"
        Finally
            con.Close()
        End Try

    ElseIf SplitTrack.Length.ToString = 11 Then
        GetTrack2 = Mid(SplitTrack, 2, 9)
        cmd.Parameters.Add("@track2", Data.SqlDbType.NVarChar).Value = GetTrack2
        Try
            con.Open()
            Dim Result As Integer = cmd.ExecuteNonQuery()
            Dim reader As SqlDataReader = cmd.ExecuteReader()
            If reader.Read() Then
                lblmemname.Text = Convert.ToString(reader(0))
                lblmemnum.Text = Convert.ToString(reader(1))
                lbltrack1.Text = Convert.ToString(reader(2))
                lbltrack2.Text = Convert.ToString(reader(3))
                lblmessage.Text = Convert.ToString(reader(4))
            End If
        Catch ex As SqlException
            errmessage.Text = "Error"
        Finally
            con.Close()
        End Try

    Else
        lblmessage.Text = "Could Not Find Any Tracks"
    End If

MSSQL STORED PROCEDURE

    ALTER Procedure [dbo].[usp_validatecard](
@memnum nvarchar(50) = '-',
@memname nvarchar(50) = '-',
@track1 nvarchar(50) = '-',
@track2 nvarchar(50) = '-',
@msgtrack1 nvarchar(50) = 'Track 1 is Blank',
@msgtrack2 nvarchar(50) = 'Track 2 is Blank',
@message nvarchar(100)= '-')
 As
--select @cardid = '%128255? ;282556587?'
--select @track1 = '128255'
--select @track2 = '282556587'
 
-- track1 good, track2 good
If exists(select * from dbo.clubmembers where FAMILYID = @track1 and (CLUBCARD1 = @track2 or CLUBCARD2 = @track2))
begin
   select @memname = lastname + ', ' + firstname, @memnum = MEMBERSHIPID from dbo.clubmembers where FAMILYID = @track1 and (CLUBCARD1 = @track2 or CLUBCARD2 = @track2)
   if exists(select * from dbo.clubmembers where membershipid = @memnum and status = 'ACTIVE')
            select @msgtrack1 = 'Track 1 is GOOD', @msgtrack2 = 'Track 2 is GOOD', @message = 'Card is GOOD'
   else
        select @msgtrack1 = 'Track 1 is GOOD but In-active', @msgtrack2 = 'Track 2 is GOOD but In-active', @message = 'Check Member Status in Membership'
end
-- track1 good and track2 bad
Else
If exists(select * from dbo.clubmembers where FAMILYID = @track1 and (CLUBCARD1 != @track2 and CLUBCARD2 != @track2))
begin
   select @memname = lastname, @memnum =  FAMILYID from dbo.clubmembers where FAMILYID = @track1 and (CLUBCARD1 != @track2 and CLUBCARD2 != @track2)
   if exists(select * from dbo.clubmembers where FAMILYID = @track1 and (CLUBCARD1 != @track2 and CLUBCARD2 != @track2) and status = 'ACTIVE')
        select @msgtrack1 = 'Track 1 is GOOD', @msgtrack2 = 'Track 2 is BAD', @message = 'Please re-encode card'
   else
        select @msgtrack1 = 'Track 1 is GOOD but In-Active', @msgtrack2 = 'Track 2 is BAD', @message = 'Please re-encode card and Check Member Status in Membership'
end
else
-- track1 bad, track2 good
If exists(select * from dbo.clubmembers where (CLUBCARD1 = @track2 or CLUBCARD2 = @track2) and FAMILYID != @track1)
begin
   select @memname = lastname + ', ' + firstname, @memnum = MEMBERSHIPID from dbo.clubmembers where (CLUBCARD1 = @track2 or CLUBCARD2 = @track2)
   If exists(select * from dbo.clubmembers where (CLUBCARD1 = @track2 or CLUBCARD2 = @track2) and FAMILYID != @track1 and STATUS = 'ACTIVE')
         select @msgtrack1 = 'Track 1 is BAD', @msgtrack2 = 'Track 2 is GOOD', @message = 'Please re-encode card'
   else
         select @msgtrack1 = 'Track 1 is BAD', @msgtrack2 = 'Track 2 is GOOD but In-active', @message = 'Please re-encode card and Check member status in Membership'
end
  
select @memname, @memnum, @msgtrack1, @msgtrack2, @message


If you check if sp has two elements or not by doing

If sp.Length = 2 Then
...
End If

and inside the If block, you can do

Dim track1 as String = sp(0)
Dim track2 as String = sp(1)


C# is more to my liking but this is a rough cut of a method that will split your string, test for correct number of elements and then create database connection (assumed sql server but any ole db should work) and call your stored procedure.

Public Sub ProcessSwipe(ByVal value As String)
    Dim splitValues() As String
    Dim separator As Char = ";"
    Dim connectionString As String = String.Empty
    Dim storedProcName As String = String.Empty

    ' Edit these to sane values
    connectionString = "Provider=SQLOLEDB;BData Source=localhost;Initial Catalog=msdb;Integrated Security=SSPI;"
    storedProcName = "dbo.SaveSwipe"

    If String.IsNullOrEmpty(value) Then
        MsgBox("No value provided from swipe")
        Return
    End If

    Try
        splitValues = value.Split(separator)
        If splitValues.Length <> 2 Then
            MsgBox(String.Format("Splitting of swipe data did not result in 2 values. Swiped value is {0}", value))
            Return
        End If

        ' At this point, we should have an array with 2 elements in it
        ' Open a connection
        Using connection As New OleDb.OleDbConnection(connectionString)
            connection.Open()

            Using Command As New OleDb.OleDbCommand
                Command.CommandText = storedProcName
                Command.CommandType = CommandType.StoredProcedure
                Command.Connection = connection

                ' Assumes your proc takes 2 parameters, named swipe1 and swipe2
                ' Update to sane values
                Command.Parameters.AddWithValue("@swipe1", splitValues(0))
                Command.Parameters.AddWithValue("@swipe2", splitValues(1))

                ' Make the actual call to your stored procedure
                Command.ExecuteNonQuery()
            End Using
        End Using

    Catch ex As Exception
        MsgBox(String.Format("Attempt to split {0} failed. {1}", value, ex))
    End Try
End Sub

Sub Main()
    Dim shorty As String
    Dim works As String
    Dim longun As String
    Dim nuthin As String

    shorty = "%128565?"
    works = "%128565?;229584115?"
    longun = "%128565?;229584115?%128565?;229584115?"
    nuthin = Nothing

    ProcessSwipe(shorty)
    ProcessSwipe(works)
    ProcessSwipe(longun)
    ProcessSwipe(nuthin)
End Sub
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜