Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So far I am only able to do just one line, I want to be able to multiple lines. I want it to take each line and compare it to the text, so if the program locates Last Name:, First Name:, Sex:, Race: it will take the characters after these and apply it to a specific sql database cell. so far it will only allocate just the Last Name:, but I have the first name: trying to pull different information. Also I would like to make it where the text does not have to be case sensitive.

here is what I have:

Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load


        RichTextBox1.Text = "last Name: Samson
First Name: Jeff
Race: W
Sex: M"

        Try

            Dim sqlcon As New SqlConnection("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=c:\users\jj\documents\visual studio 2015\Projects\WindowsApplication3\WindowsApplication3\Database1.mdf;Integrated Security=True")
            Dim sqladapt = New SqlDataAdapter("Select * from " + "[" + Form1.TreeView1.SelectedNode.Text.ToString, sqlcon)

            sqlcon.Open()
            Dim cmd As SqlClient.SqlCommand
            Dim sql As String = "insert into " + "[" + Form1.TreeView1.SelectedNode.Text.ToString + "]" + "values(@id,@Last,@First,@Sex,@Race)"
            cmd = New SqlClient.SqlCommand(sql, sqlcon)

            ' Make sure that all tables have the same type of information that can be entered, if not you will recieve an error.
            cmd.Parameters.AddWithValue("@id", Form1.IdTextBox.Text)


            cmd.Parameters.AddWithValue("@Last", RichTextBox1.Text.Replace("last Name: ", RichTextBox1.Text.Substring(11)))




            cmd.Parameters.AddWithValue("@First", RichTextBox1.Text.Replace("First Name: ", RichTextBox1.Text.Substring(12)))
            cmd.Parameters.AddWithValue("@Sex", Form1.SexTextBox.Text())
            cmd.Parameters.AddWithValue("@Race", Form1.RaceTextBox.Text)



            cmd.ExecuteNonQuery()
            sqlcon.Close()

            MessageBox.Show("New Record Added")
        Catch ex As Exception
            MsgBox(ex.Message)

            End Try



    End Sub


What I have tried:

I have tried just to use the substring function to take me to specific text. also, I have tried to do separate if statements but it keeps telling me that I have to assign a value.
Posted
Updated 19-Jan-17 4:27am

1 solution

Extracting meaningful values from free-form text is notoriously difficult.

If you're absolutely certain that each value will be on its own line, that the line will start with the correct label, and the label and value will be separated with a colon, then you might be able to use a Regular Expression[^] to extract the values.
VB.NET
Dim lastName As String = Nothing, firstName As String = Nothing, race As String = Nothing, sex As String = Nothing

Dim pattern As New Regex("^\s*(?<label>[^:]+)\s*:\s*(?<value>.+)\s*\r?$", RegexOptions.Multiline)

Dim matches As MatchCollection = pattern.Matches(RichTextBox1.Text)

For Each match As Match In matches
    Dim label As String = match.Groups("label").Value
    If String.Equals(label, "Last Name", StringComparison.OrdinalIgnoreCase) Then
        lastName = match.Groups("value").Value
    Else If String.Equals(label, "First Name", StringComparison.OrdinalIgnoreCase) Then
        firstName = match.Groups("value").Value
    Else If String.Equals(label, "Race", StringComparison.OrdinalIgnoreCase) Then
        race = match.Groups("value").Value
    Else If String.Equals(label, "Sex", StringComparison.OrdinalIgnoreCase) Then
        sex = match.Groups("value").Value
    End If
Next
 
Share this answer
 
Comments
Richard Deeming 19-Jan-17 13:31pm    
You'll need to add the following line to the top of your code file:
Imports System.Text.RegularExpressions
Member 11856456 19-Jan-17 13:43pm    
Richard, It seems like this code works. I am needing to add an auto-increment to the mix for the ID column to make for sure. Quick question, will this go to the next row after a break such as a space between the information?
for example-

First Name: steve
Last name: Woods
Race: W
Sex: M

First Name: john (this would start a new row)
Last name: Stockton
Race: B
Sex: M
Richard Deeming 19-Jan-17 13:50pm    
No, because that wasn't part of the original question.

You'd need to split the text into the groups, and process each one individually. For example:
Dim rows() As String = Regex.Split(RichTextBox1.Text, "^\r?$", RegexOptions.Multiline)
Dim pattern As New Regex("^\s*(?<label>[^:]+)\s*:\s*(?<value>.+)\s*\r?$", RegexOptions.Multiline)

For Each row As String In rows
    Dim lastName As String = Nothing, firstName As String = Nothing, race As String = Nothing, sex As String = Nothing
    Dim matches As MatchCollection = pattern.Matches(row)
    For Each match As Match In matches
        ' Same code as answer...
    Next
    
    ' Process the row values here...
Next
Member 11856456 19-Jan-17 14:01pm    
thanks, this gives me a great place to start.

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