Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So, I am trying to make a update statement using string builder for first name, last name and middle name. problem that I am having is that its not including the comma for the values and the columns. What am I missing to complete this statement to make it work appropriately?

 Using sqlconn As New SqlConnection("SQLstatement")
            Using da As New SqlDataAdapter(String.Empty, sqlconn)
                Dim sql As New StringBuilder("insert into [")
                sql.Append(Form1.TreeView1.SelectedNode.Text.Replace("]", "\]"))
                sql.Append("] ( ")

                Dim sbOn As New StringBuilder(") values( ")
                Dim andRequired As Boolean = False

                If Not String.IsNullOrEmpty(First_NameTextBox.Text) Then
                    sql.Append(" [First_name]")
                    sbOn.Append("@Firstname")
                    If andRequired Then
                        sbOn.Append(", ")
                    End If
                    da.SelectCommand.Parameters.AddWithValue("@Firstname", 
                    First_NameTextBox.Text)
                End If

                    If Not String.IsNullOrEmpty(Last_NameTextBox.Text) Then
                    sql.Append(" [Last_name]")
                    sbOn.Append("@Lastname")
                    If andRequired Then
                        sbOn.Append(", ")
                    End If
                    da.SelectCommand.Parameters.AddWithValue("@Lastname", 
                    Last_NameTextBox.Text)
                End If


                If Not String.IsNullOrEmpty(Middlenametextbox.Text) Then
                    sql.Append(" [Middle_name]")
                    sbOn.Append("@Middlename")
                    If andRequired Then
                        sbOn.Append(", ")
                    End If

                    da.SelectCommand.Parameters.AddWithValue("@Middlename", 
                    Middlenametextbox.Text)
                End If

sql.AppendFormat(sbOn.ToString + ")")


This is what comes up when I run it with a messagebox:

"insert into [table] ([First_name] [Last_name] [Middle_name]) values(@First_name@Last_name@Middle_name)"

as you can see there is no comma, so this is an invalid sql statement.

What I have tried:

I have tried to use a boolean in the statement to help, but it's not activating the commas.
Posted
Updated 18-Aug-17 8:23am

1 solution

Try something like this:
VB.NET
If Not String.IsNullOrEmpty(First_NameTextBox.Text) Then
    If andRequired Then
        sql.Append(", ")
        sbOn.Append(", ")
    End If
    
    sql.Append("[First_name]")
    sbOn.Append("@Firstname")
    
    da.SelectCommand.Parameters.AddWithValue("@Firstname", First_NameTextBox.Text)
    andRequired = True
End If

If Not String.IsNullOrEmpty(Last_NameTextBox.Text) Then
    If andRequired Then
        sql.Append(", ")
        sbOn.Append(", ")
    End If
    
    sql.Append("[Last_name]")
    sbOn.Append("@Lastname")
    
    da.SelectCommand.Parameters.AddWithValue("@Lastname", Last_NameTextBox.Text)
    andRequired = True
End If

If Not String.IsNullOrEmpty(Middlenametextbox.Text) Then
    If andRequired Then
        sql.Append(", ")
        sbOn.Append(", ")
    End If
    
    sql.Append("[Middle_name]")
    sbOn.Append("@Middlename")
    
    da.SelectCommand.Parameters.AddWithValue("@Middlename", Middlenametextbox.Text)
    andRequired = True
End If
 
Share this answer
 
Comments
Member 11856456 18-Aug-17 14:46pm    
I was just a little off. I used messagebox.show to help me put the string together. Thanks Richard!!

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