Results 1 to 12 of 12

Thread: Adding rows to a table

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2004
    Posts
    24

    Adding rows to a table

    I know this question is very basic but I couldn't find it in my help or any of the forums on this site (Yeah, I searched first). Basically, all I want to know is how to add rows to a table using VB.net. I'm certain it requires a dataset but not sure. You don't have to explain how to do add but just point me in the right direction. Thanks!

  2. #2
    Lively Member Neoharuo's Avatar
    Join Date
    Aug 2005
    Posts
    100

    Re: Adding rows to a table

    I think this is what you are looking for.

    VB Code:
    1. Dim newCol As New DataColumn("New Column", GetType(Integer))
    2.         newCol.DefaultValue = 0
    3.         newCol.ReadOnly = False
    4.         oTempTbl.Columns.Add(newCol)



    --EDIT--

    No its not, hold on, let me fix this...sorry

  3. #3
    Lively Member Neoharuo's Avatar
    Join Date
    Aug 2005
    Posts
    100

    Re: Adding rows to a table

    THIS is what you want...

    VB Code:
    1. Dim newRow As DataRow
    2.         newRow.Item(0) = "Whatever"
    3.         newRow.Item(1) = "Something Else"
    4.         oTempTbl.Rows.Add(newRow)

    You just have to make sure that the items you are adding correspond to the correct value types in the table columns. Also if the table comes for a database, you may or may not have issues with your primary key field.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Sep 2004
    Posts
    24

    Re: Adding rows to a table

    Quote Originally Posted by Neoharuo
    THIS is what you want...

    VB Code:
    1. Dim newRow As DataRow
    2.         newRow.Item(0) = "Whatever"
    3.         newRow.Item(1) = "Something Else"
    4.         oTempTbl.Rows.Add(newRow)

    You just have to make sure that the items you are adding correspond to the correct value types in the table columns. Also if the table comes for a database, you may or may not have issues with your primary key field.
    Neoharuo, thank you for replying but where did the oTempTbl object come from? Did you create it from a data source? (I Havea TestDataSet from adding a database named Test with a table named Links, if that helps). This is so much more convoluted than php :\

  5. #5
    Lively Member Neoharuo's Avatar
    Join Date
    Aug 2005
    Posts
    100

    Re: Adding rows to a table

    I'll clarify, the oTempTbl is well just a table that was filled from a database. You can use a data set as well.
    VB Code:
    1. TestDataSet.Tables("Links").Rows.Add(newRow)

    --EDIT--

    Do you say you already filled a dataset or do you need to know how to do that as well?

    Also, by adding a row to the Data set, you aren't adding it to the actual database until you do an update on your data adapter. If you need more info let me know.

  6. #6
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: Adding rows to a table

    here's the way to add rows to your datatable.
    VB Code:
    1. 'dt is a datatable
    2. dim dr as datarow=dt.newrow
    3. dr(0)="thanks"<---referring to the first column of your datatable
    4. dt.rows.add(dr)

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Sep 2004
    Posts
    24

    Re: Adding rows to a table

    Hrmm, did I mention that this is done with vb.net 2005 express? I guess I should have.

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Sep 2004
    Posts
    24

    Re: Adding rows to a table

    Quote Originally Posted by Neoharuo
    I'll clarify, the oTempTbl is well just a table that was filled from a database. You can use a data set as well.
    VB Code:
    1. TestDataSet.Tables("Links").Rows.Add(newRow)

    --EDIT--

    Do you say you already filled a dataset or do you need to know how to do that as well?

    Also, by adding a row to the Data set, you aren't adding it to the actual database until you do an update on your data adapter. If you need more info let me know.
    I appreciate your help, but I think I should go to Borders and just open a book on ADO.net and hope that its similar for .net 2.0 :|

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Sep 2004
    Posts
    24

    Re: Adding rows to a table

    Alright, I'm at a lost; I can't find any books for ado.net for vb.net 2k5 (But plenty for 2k3). BUT! I read the "Introducing VB.net 2k5" and I read the ado.net part and it says that the 2k5 version is just like the 2k3 version (for legacy code and to lessen the learning curve for 2k3 developers). However, I can't get 2k3 code to work! Here's what I understand so far for ado.net for 2k5...

    1) Connect to a db (sqlexpress) using a sqladapter (done for me using wizard)
    2) Create a tableAdapter to fill a dataSet using the fill method (I can do this)

    Now how do I add records to the dataset? Well this is what I THINK...

    3) Create a datarow from the dataset defined in step 2 and fill it with data
    4) Add the row to the dataset (I'm sure I went wrong here)
    5) Update the dataSet to the database using the tableAdapter.

    Can someone post a dummy project or code doing this within a button click event so I can learn this already? THANK!

  10. #10
    Lively Member
    Join Date
    Sep 2005
    Location
    Glasgow, Scotland
    Posts
    77

    Re: Adding rows to a table

    Daticus, I'm using VS2002, and I tend to use the wizards for creating DataAdaptors and Datasets. It makes life 100 times easier, even if the DataAdaptor you build isn't the finished article.

    What I generally do is run the wizard so that at least the code gets written "on the fly". Then once it's finished, I go into the properties for the DataAdaptor and modify the SQL statements where necessary - I almost always build DA's that use Stored Procedures, rather than having naked SQL code hard-wired into the application.

    Once you've got the DataAdaptor properly configured, and you can browse it's contents by testing it, you can then go ahead and build the DataSet object and bind your fields to it. I tend to bind the fields programmatically too, so that if I add more fields, or change field bindings, I have a single subroutine that I can go to for that.

    HTH

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Sep 2004
    Posts
    24

    Re: Adding rows to a table

    I've been reading a book on ado.net 1.1 and discovered that you can execute sql commands against the SqlCommand object to do whatever you want to the database. I've got it to work and adding records was fine, but they don't save into the database, it seems it only saves into the in memory temporary database, here's the code:



    VB Code:
    1. Dim con As New System.Data.SqlClient.SqlConnection()
    2.   con.ConnectionString = My.Settings.SqlX
    3.         Dim cmd As New System.Data.SqlClient.SqlCommand()
    4.         cmd.CommandText = "insert into Names values('Bob')"
    5.         cmd.Connection = con
    6.         Try
    7.             con.Open()
    8.             Dim i As Integer = cmd.ExecuteNonQuery
    9.  
    10.  
    11.             MsgBox(i.ToString())
    12.         Catch ex As Exception
    13.             MsgBox(ex.ToString())
    14.         Finally
    15.  
    16.             con.Close()
    17.         End Try

    If you're not using a dataAdapter or dataSet do you still have to "update" the database? I don't see why you would have to but I'm a novice.

    Thanks!
    Last edited by daticus; Sep 13th, 2005 at 01:06 PM.

  12. #12
    New Member
    Join Date
    Apr 2006
    Posts
    2

    Re: Adding rows to a table

    Try using the "update" method of DataAdapter.

    Refer to this example:

    your_dataadapter.UPDATE(your_dataset, "your_tablename")

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