[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 263
  • Last Modified:

Using .net 2.0 to insert a single row of data

I need to add a row of data to a table. In my attempt below I receive an error message saying that I need a valid insertcommand. However, even if this would work, it looks a bit inefficient. Also, what I want to do is to add ONE row of data as efficient as possible and avoid the use of sql. (i.e. insert into test (test1,test2) values ("text1","text2")...)



        Dim sqlConn As New System.Data.SqlClient.SqlConnection(myConnection)
        Dim da As New SqlClient.SqlDataAdapter("Select * from Test", sqlConn)
        Dim ds As New DataSet

        sqlConn.Open()
        da.AcceptChangesDuringFill = True
        da.Fill(ds, "Test")

        Dim r As DataRow = ds.Tables("Test").NewRow()
        r("Test1") = "text1"
        r("Test2") = "text2"

        ds.Tables("Test").Rows.Add(r)

        da.Update(ds, "Test")

        sqlConn.Close()
0
Contestoas
Asked:
Contestoas
1 Solution
 
Fernando SotoRetiredCommented:
Hi Contestoas;

Try adding this line of code after you create the SqlDataAdapter.

Dim cb As SqlCommandBuilder = New SqlCommandBuilder(da)

Fernando
0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now