Add a new row to Sql Database

Last post 07-05-2008 4:20 AM by stalzon. 4 replies.

Sort Posts:

  • Add a new row to Sql Database

    07-04-2008, 8:35 PM
    • Loading...
    • stalzon
    • Joined on 07-03-2008, 3:09 AM
    • Posts 23

     

     Hi,

    I trying to add a new row to my sql database with a button control. So far the code is..

     

    1            Dim connectionstring As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
    2            Using myconnection As New SqlConnection(connectionstring)
    3                myconnection.Open()
    4                Const sql As String = "SELECT * FROM [data]"
    5                Dim mycommand As New SqlCommand(sql, myconnection)
    6                Dim myadapter As New SqlDataAdapter(mycommand)
    7                Dim mydataset As New DataSet
    8                myadapter.Fill(mydataset, "data")
    9                mydataset.Tables(0).Rows.Add(....)
    10               myadapter.Update(mydataset, "data")
    11               myconnection.Close()
    12           End Using
    
     

     

    But it gives an error at line 10,  Update requires a valid InsertCommand when passed DataRow collection with new rows. What am I doing wrong? Or am I using a totally wrong way to add a row to the sql database?

  • Re: Add a new row to Sql Database

    07-04-2008, 9:14 PM
    Answer
    • Loading...
    • limno
    • Joined on 06-10-2005, 3:50 PM
    • Iowa, USA
    • Posts 3,441
    • Moderator
      TrustedFriends-MVPs

    Instead of using dataset to add data to database, Try ADO.NET Insert directly.  

    Here is a sample: 

     Dim connectionstring As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
    
    
    Using myconnection As New SqlConnection(connectionstring)
    
    Const Insertsql As String = "INSERT INTO Customers (CustomerID, CompanyName) VALUES (@CustomerID, @CompanyName)"
    
    Dim mycommand As New SqlCommand(Insertsql, myconnection)
    
    mycommand.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID")
    
    mycommand.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName")
    
    myconnection.Open()
    
    mycommand.ExecuteNonQuery()
    
    myconnection.Close()
    
    End Using
    
     
    Limno

  • Re: Add a new row to Sql Database

    07-04-2008, 10:05 PM
    • Loading...
    • stalzon
    • Joined on 07-03-2008, 3:09 AM
    • Posts 23

    Thanks for the answer.

    But I actually need the previous row of the data for a column of the new data, so I tried to get the data to a new dataset, and then update it. I guess first I can get the data to a dataset, get the required value, then again reach the data with Insert Statement; But I think it's a long way to do it. Is it possible to update the dataset first then update the sql database with this dataset?

     Or is there a way to reach the previous row of the sql data, using Insert?  (I hope I could explain the situation clearly..)

  • Re: Add a new row to Sql Database

    07-04-2008, 10:25 PM
    Answer
    • Loading...
    • limno
    • Joined on 06-10-2005, 3:50 PM
    • Iowa, USA
    • Posts 3,441
    • Moderator
      TrustedFriends-MVPs

    Here is a walkthrough from Microsoft Support for update from a DataSet: http://support.microsoft.com/kb/301248

    Limno

  • Re: Add a new row to Sql Database

    07-05-2008, 4:20 AM
    • Loading...
    • stalzon
    • Joined on 07-03-2008, 3:09 AM
    • Posts 23

    Thanks for the link.

    It works now.

Page 1 of 1 (5 items)
Microsoft Communities
Page view counter