Click here to Skip to main content
15,890,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
here is my code:

Dim sqlconn As New SqlConnection("sqlconnectionstring")
            Dim da As New SqlDataAdapter(String.Empty, sqlconn)

            Dim sql As New StringBuilder("insert into [")
            sql.Append(Form1.TreeView1.SelectedNode.Text.Replace("]", "\]"))
            sql.Append("] ([Id], ")

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

            If Not String.IsNullOrEmpty(IDTextBox.Text) Then
                da.SelectCommand.Parameters.AddWithValue("@Id", 
            Form1.DataGridView1.Rows.Count + 1)
            End If

            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
sql.AppendFormat(sbOn.ToString + ")")
            da.SelectCommand.CommandText = sql.ToString()
            Dim dt As New DataTable
            da.Fill(dt)
            DataGridView1.DataSource = dt


when this is used the sql string comes out right, but the datagridview that its connected to turns gray and shows no data, but if I restart the program it will show the new data.

What I have tried:

tried to change the da.selectcommand to a da.insertcommand. Also tried to change things around a use a new sql command to correspond to the Dataadaptor, but that didn't work either.
Posted
Updated 22-Aug-17 7:07am

1 solution

I ran into this issue too, you can solve it by setting DataGridView1.DataSource to Nothing first.
If that does not suffice, add DataGridView.Rows.Clear()
 
Share this answer
 
v3
Comments
Member 11856456 22-Aug-17 14:02pm    
worked well for my update statement, but not the insert. it first show which ever records I selected and acts like an update statement. When I go and restart the program it shows the new information
Member 11856456 22-Aug-17 14:32pm    
It still didn't work, so I just decided to put an additional SQL statement in that references back to the table after the new add.

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