Results 1 to 5 of 5

Thread: Simple SQL Database Issues!!!

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2004
    Posts
    12

    Simple SQL Database Issues!!!

    Hi Guys,

    I've been working the this problem for two days now and I am starting to get really frustrated!! All I need to do is to be able to add a new row into a database table with some new data in it! But even the following example code does not work for me...

    Code:
        Private Sub Button2_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button2.Click
            MsgBox("Attempting to update a DataTable that is Nothing...")
            Dim cn As New SqlConnection("Server=vcomp;" & _
                                        "integrated security=true;database=northwind")
            Dim da As New SqlDataAdapter("Select * From Customers", cn)
            Dim cb As New SqlCommandBuilder(da)
            Dim ds As New DataSet
            Dim dr As DataRow
    
            da.Fill(ds, "Customers")
    
            dr = ds.Tables("Customers").NewRow
    
            ds.Tables(0).Rows(0)!ContactName = "New_Contact"
            ds.Tables(0).Rows.Add(dr)
    
            da.Update(ds, "Customers")
    
            cn.Close()
            cn = Nothing
        End Sub
    This example is from the Microsoft Knowledgebase with some lines added in to insert a row and add 'New_Contact' in the ContactName field.

    I get the following error on the da.Update(ds, "Customers") line...

    An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in system.data.dll

    Additional information: System error.
    What is wrong?? What am I missing?? Please help me
    soulcode

  2. #2
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,374
    I haven't tried that method of inserting rows but give this a try:

    I am assuming that the Customers table has 2 fields - CustomerID (identity field) and ContactName.

    VB Code:
    1. Dim myConnection As SqlClient.SqlConnection = New SqlClient.SqlConnection("Server=vcomp;" & _
    2.                                     "integrated security=true;database=northwind")
    3.  
    4. SQL = "INSERT INTO Customers(ContactName) VALUES('New_Contact')"
    5.  
    6. Dim myCommand As SqlClient.SqlCommand = New SqlClient.SqlCommand(SQL)
    7.  
    8. myConnection.Open()
    9.  
    10. myCommand.Connection = myConnection
    11.  
    12. myCommand.ExecuteNonQuery()

  3. #3
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Here's how I'd tackle a database insert with ADO.Net:
    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, _
    2. ByVal e As System.EventArgs) Handles Button1.Click
    3.        
    4.         Dim cnnADONet As SqlClient.SqlConnection = New SqlClient.SqlConnection
    5.         Dim cmdSQL As SqlClient.SqlCommand = new SqlClient.SqlCommand
    6.  
    7.     MsgBox("Attempting to update a DataTable that is Nothing...")
    8.  
    9.     cnnADONet.ConnectionString = ("Server=vcomp;" & _
    10.         "integrated security=true;database=northwind")
    11.     cnnADONet.Open()
    12.  
    13.     If (cnnADONet.State = ConnectionState.Open) then
    14.         cmdSQL.Connection = cnnADONet
    15.         cmdSQL.CommandText = "INSERT INTO Customers(ContactName)" & _
    16.         "VALUES('New_Contact')"
    17.         cmdSQL.CommandType = CommandType.Text
    18.  
    19.         Msgbox (cmdSQL.ExecuteNonQuery & " rows affected")
    20.     End If
    21.  
    22.     cmdSQL.Dispose
    23.         cmdSQL = nothing
    24.  
    25.     cnnADONet.Close
    26.     cnnADONet.Dispose
    27.     cnnADONet = nothing
    28.  
    29.     MsgBox("Finished")
    30. End Sub
    Last edited by alex_read; Feb 25th, 2004 at 03:18 AM.

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  4. #4
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Damn, beat me to it

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2004
    Posts
    12
    Well, with all your help and alot of searching on the internet, I came to the solution. In the end I modified the following code from this link:: http://msdn.microsoft.com/library/de...singadonet.asp


    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim oAdapter As SqlClient.SqlDataAdapter
            Dim oBuild As SqlClient.SqlCommandBuilder
            Dim sqlConn As New SqlConnection
            Dim oDR As DataRow
            Dim strSQL As String
            Dim moDS = New DataSet
    
            sqlConn = New SqlConnection("Data Source = LocalHost; Initial Catalog = vTest; User Id=sa")
            sqlConn.Open()
            oAdapter = New SqlDataAdapter("SELECT * FROM Customer", sqlConn)
            oAdapter.Fill(moDS, "Customer")
    
            oDR = moDS.Tables("Customer").NewRow()
            oDR.BeginEdit()
    
            oDR("CustID") = "TestID"
            oDR("CustName") = "TestCustName"
    
            oDR.EndEdit()
            moDS.Tables("Customer").Rows.Add(oDR)
    
    
            strSQL = "SELECT * FROM Customer "
            oAdapter = New SqlClient.SqlDataAdapter(strSQL, sqlConn)
            oBuild = New SqlClient.SqlCommandBuilder(oAdapter)
    
            oAdapter.InsertCommand = oBuild.GetInsertCommand()
    
            oAdapter.Update(moDS, "Customer")
            moDS.AcceptChanges()
    
        End Sub

    I didnt expect that something as simple as adding a new row to a database could take me so long to find a solution for!!!

    But, hope the above example helps the next guy thats has a similar problem to solve.

    Cheers guys.


    .
    soulcode

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