Results 1 to 6 of 6

Thread: Insert and Editing records through SQL Dataadapter

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    Insert and Editing records through SQL Dataadapter

    Hello, I'm using this:
    VB Code:
    1. Private dacustomers As New SqlClient.SqlDataAdapter
    2.     Private selectcom As New SqlClient.SqlCommand("SELECT * FROM Customers")
    3.     Private ds As New DataSet("Northwind")
    4.     Private SQLCount As String = "SELECT Count(CustomerID) FROM Customers"
    5.     Private RecordCount As Integer
    6.     Private bmCustomers As BindingManagerBase
    7.     Private InsBuilder As New StringBuilder
    8.     Private EDBuilder As New StringBuilder
    9.  
    10.    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
    11.  
    12.         If btnAdd.Text = "ADD" Then
    13.             btnAdd.Text = "Finish"
    14.             TextBox1.Text = ""
    15.             TextBox2.Text = ""
    16.             TextBox3.Text = ""
    17.         Else
    18.             btnAdd.Text = "ADD"
    19.             'SqlConnection1.Open()
    20.  
    21.             InsBuilder.Append("INSERT INTO Customers (CompanyName,CustomerID,ContactName) ")
    22.             InsBuilder.Append("VALUES (")
    23.             InsBuilder.Append(TextBox1.Text)
    24.             InsBuilder.Append(",")
    25.             InsBuilder.Append(TextBox2.Text)
    26.             InsBuilder.Append(",")
    27.             InsBuilder.Append(TextBox3.Text)
    28.             InsBuilder.Append(")")
    29.             Dim InsComm As New SqlClient.SqlCommand("InsBuilder")
    30.             dacustomers.InsertCommand = InsComm
    31.             dacustomers.InsertCommand.ExecuteNonQuery()
    32.         End If
    33.     End Sub
    34.  
    35.     Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
    36.         If btnEdit.Text = "EDIT" Then
    37.             btnAdd.Text = "Update"
    38.             TextBox1.Text = ""
    39.             TextBox2.Text = ""
    40.             TextBox3.Text = ""
    41.         Else
    42.             btnAdd.Text = "EDIT"
    43.             EDBuilder.Append("UPDATE Customers SET ")
    44.             EDBuilder.Append("CompanyName = ")
    45.             EDBuilder.Append(TextBox1.Text)
    46.             EDBuilder.Append(",")
    47.             EDBuilder.Append("CustomerID = ")
    48.             EDBuilder.Append(TextBox2.Text)
    49.             EDBuilder.Append(",")
    50.             EDBuilder.Append("ContactName = ")
    51.             EDBuilder.Append(TextBox3.Text)
    52.  
    53.             dacustomers.SelectCommand.ExecuteNonQuery()
    54.         End If
    55.     End Sub

    to try add and edit records, but The compiler keeps throwing me out, complaining about a table not specified

    How can I fix this, to add and edit records at run time?

  2. #2
    Frenzied Member Asgorath's Avatar
    Join Date
    Sep 2004
    Location
    Saturn
    Posts
    2,036

    Re: Insert and Editing records through SQL Dataadapter

    You are updating the entire table Customers - is that what you want , no where clause. Modified it slighly...

    VB Code:
    1. Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
    2.         If btnEdit.Text = "EDIT" Then
    3.             btnAdd.Text = "Update"
    4.             TextBox1.Text = ""
    5.             TextBox2.Text = ""
    6.             TextBox3.Text = ""
    7.         Else
    8.             btnAdd.Text = "EDIT"
    9.             EDBuilder.Append("UPDATE Customers SET ")
    10.             EDBuilder.Append("CompanyName =' ")
    11.             EDBuilder.Append(TextBox1.Text)
    12.             EDBuilder.Append("',")
    13.             EDBuilder.Append("CustomerID = ")
    14.             EDBuilder.Append(TextBox2.Text)
    15.             EDBuilder.Append(",")
    16.             EDBuilder.Append("ContactName =' ")
    17.             EDBuilder.Append(TextBox3.Text)
    18.             EBBuilder.Append("'")
    19.  
    20.             dacustomers.SelectCommand.ExecuteNonQuery()
    21.         End If
    22.     End Sub

    Regards
    Jorge
    "The dark side clouds everything. Impossible to see the future is."

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    Re: Insert and Editing records through SQL Dataadapter

    Ah... the single quotes

    Thanx buddy!
    Can you perhaps see what's wrong with my Add sub (it's alos probably something silly, but I can't see what I'm doing wrong

  4. #4
    Frenzied Member Asgorath's Avatar
    Join Date
    Sep 2004
    Location
    Saturn
    Posts
    2,036

    Re: Insert and Editing records through SQL Dataadapter

    Same problem when your dealing with chars in sql you need to separate them the quotes.

    VB Code:
    1. Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
    2.  
    3.         If btnAdd.Text = "ADD" Then
    4.             btnAdd.Text = "Finish"
    5.             TextBox1.Text = ""
    6.             TextBox2.Text = ""
    7.             TextBox3.Text = ""
    8.         Else
    9.             btnAdd.Text = "ADD"
    10.             'SqlConnection1.Open()
    11.  
    12.             InsBuilder.Append("INSERT INTO Customers (CompanyName,CustomerID,ContactName) ")
    13.             InsBuilder.Append("VALUES ('")
    14.             InsBuilder.Append(TextBox1.Text)
    15.             InsBuilder.Append("',")
    16.             InsBuilder.Append(TextBox2.Text)
    17.             InsBuilder.Append(",'")
    18.             InsBuilder.Append(TextBox3.Text)
    19.             InsBuilder.Append("')")
    20.             Dim InsComm As New SqlClient.SqlCommand("InsBuilder")
    21.             dacustomers.InsertCommand = InsComm
    22.             dacustomers.InsertCommand.ExecuteNonQuery()
    23.         End If
    24.     End Sub

    Regards
    Jorge
    "The dark side clouds everything. Impossible to see the future is."

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    Re: Insert and Editing records through SQL Dataadapter

    Got it Thank you!

    BTW. I've modified my Edit code
    VB Code:
    1. Dim sqlCommand As System.Data.SqlClient.SqlCommand
    2.         Dim CompanyNameStr As String
    3.         Dim NewCustName As String
    4.  
    5.         Try
    6.             If btnEdit.Text = "EDIT" Then
    7.                 CompanyNameStr = InputBox("Enter Company Name To Change")
    8.                 NewCustName = InputBox("Enter New Name")
    9.                 btnEdit.Text = "Update"
    10.             Else
    11.                 btnEdit.Text = "EDIT"
    12.  
    13.                 EDBuilder.Append("UPDATE Customers SET ")
    14.                 EDBuilder.Append("CompanyName = '")
    15.                 EDBuilder.Append(NewCustName)
    16.                 EDBuilder.Append("',")
    17.                 EDBuilder.Append("CustomerID = '")
    18.                 EDBuilder.Append(TextBox2.Text)
    19.                 EDBuilder.Append("',")
    20.                 EDBuilder.Append("ContactName = '")
    21.                 EDBuilder.Append(TextBox3.Text)
    22.                 EDBuilder.Append("'")
    23.                 EDBuilder.Append(" WHERE CompanyName = '")
    24.                 EDBuilder.Append(CompanyNameStr)
    25.                 EDBuilder.Append("'")
    26.  
    27.                 sqlCommand = New System.Data.SqlClient.SqlCommand(EDBuilder.ToString(), SqlConnection1)
    28.                 SqlConnection1.Open()
    29.                 sqlCommand.ExecuteNonQuery()
    30.             End If
    31.         Catch ex As Exception
    32.             MsgBox(ex.Message.ToString())
    33.         Finally
    34.             SqlConnection1.Close()
    35.  
    36.  
    37.         End Try

    The program runs, doesn't throw errors, BUT, the info doesn't get updated when I click "Update" - Is there something else I should include, like refreshing the Dataset, what should I do ?

  6. #6
    Frenzied Member Asgorath's Avatar
    Join Date
    Sep 2004
    Location
    Saturn
    Posts
    2,036

    Re: Insert and Editing records through SQL Dataadapter

    If you are displaying the information in a datagrid just clear the dataset, fill it again and bound the datagrid with the updated datable.

    Regards
    Jorge
    "The dark side clouds everything. Impossible to see the future is."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width