Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Adding row to database question

Adding row to database question

Scheduled Pinned Locked Moved Visual Basic
databasequestioncsharpsql-server
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    ranro2006
    wrote on last edited by
    #1

    I have a question that I was hoping someone could help me with. I am using VB.net 2005 express edition with SQL Server 2005 Express edition. I have a database that has a table in it called Folder. It consists of two fields. One is called folderId and folderName. I want to add a new row to this folder by calling a second form from my main form. For example, say I open up a second form from my main form. This second form consists of a text box and an button called OK. The user will enter the new folder name into the text box. How do I make it so the name the user enters into the text box is added to the folder table.

    G D 2 Replies Last reply
    0
    • R ranro2006

      I have a question that I was hoping someone could help me with. I am using VB.net 2005 express edition with SQL Server 2005 Express edition. I have a database that has a table in it called Folder. It consists of two fields. One is called folderId and folderName. I want to add a new row to this folder by calling a second form from my main form. For example, say I open up a second form from my main form. This second form consists of a text box and an button called OK. The user will enter the new folder name into the text box. How do I make it so the name the user enters into the text box is added to the folder table.

      G Offline
      G Offline
      ghost181
      wrote on last edited by
      #2

      Hi, I think u need to use sql command to done it.. Example: public sub OK_Click (byval sender as object, ByVal e As System.EventArgs)handles OK.click Dim cmd As New Data.OleDb.OleDbCommand cmd.Connection = mssql 'Sql command Save.... example: cmd.CommandText = "INSERT INTO [Folder] (FolderID, FolderName) " cmd.CommandText &= "VALUES ('" & me.txtFolderID.text.trim & "','" & me.txtFolderName.text.trim & "', " Execute SQL Try cmd.Connection.Open() cmd.ExecuteNonQuery() Catch ex As OleDb.OleDbException msgbox(ex) finally connection.close() end try i hope it can be help :)

      1 Reply Last reply
      0
      • R ranro2006

        I have a question that I was hoping someone could help me with. I am using VB.net 2005 express edition with SQL Server 2005 Express edition. I have a database that has a table in it called Folder. It consists of two fields. One is called folderId and folderName. I want to add a new row to this folder by calling a second form from my main form. For example, say I open up a second form from my main form. This second form consists of a text box and an button called OK. The user will enter the new folder name into the text box. How do I make it so the name the user enters into the text box is added to the folder table.

        D Offline
        D Offline
        Dave Sexton
        wrote on last edited by
        #3

        Best way to do it IMHO is through making use of datasets and table adapters. First you need to create a dataset. To do this 1. Right click over your solution name and select "Add new item..." 2. Click on "Dataset" & give the dataset a meaningful name (in this example I'm using the name dsFolder). 3. The dataset will be created and a wizard will open to help you configure your table adapter. 4. Configure your table adapter to use the same connection string (saved in the My.Settings file), click "Next" & use the query builder to add your table to the table adapter. 5. Add the fields from your table by checking their checkboxes. Remember to include the primary key from your table so that the table adapter can automatically generate update statements for you. 6. Click "Next" & "Finish". In your main form add this code to whatever event it is that opens your form:

        Dim frm2 as New Form2
        frm2.Show()
        'use ShowDialog() if you don't want users to close
        'the form before completing process.

        Then in your second form add

        Option Strict On
        Imports System.Data
        Imports System.Data.SqlClient

        to the very top of the code editor. Add the code below to the OK button's click event (the naming in your code will probably be different but you get the idea),

        Private Sub btnOK_Click(...)Handles btnOK.Click
        'create new datasets and table adapters
        Dim dsFolder1 As New dsFolder
        Dim daFolder As New dsFolder.FolderTableAdapter

        'create a new row to add to the table
        Dim dr As DataRow = dsFolder1.Folder.NewRow()

        'try updating the database
        Try
        dr("folderName") = txtFolder.Text

        'add the datarow to the dataset
        dsFolder1.Folder.Rows.Add(dr)

        'update the database
        daFolder.Update(dsFolder1)
        Catch ex As Exception
        Messagebox.Show(ex.Message)
        End Try
        End Sub

        Using this kinda practice is better because the datasets and table adapters add type safety to your code, make it more readable as well as gives you objects that you can reuse later on in your code. This comes in really useful when you work on large databases. Also. make use of Try...Catch...End Try blocks. The structured error handling ability of .NET is tool worth using. You could go even further by implementing TraceListeners to write out performance data & errors from your code to a log file, which can only help you write better code. Sorry, for the long-winded explanation, I've had WAAAAY too much coffee this morning... ;P

        R 1 Reply Last reply
        0
        • D Dave Sexton

          Best way to do it IMHO is through making use of datasets and table adapters. First you need to create a dataset. To do this 1. Right click over your solution name and select "Add new item..." 2. Click on "Dataset" & give the dataset a meaningful name (in this example I'm using the name dsFolder). 3. The dataset will be created and a wizard will open to help you configure your table adapter. 4. Configure your table adapter to use the same connection string (saved in the My.Settings file), click "Next" & use the query builder to add your table to the table adapter. 5. Add the fields from your table by checking their checkboxes. Remember to include the primary key from your table so that the table adapter can automatically generate update statements for you. 6. Click "Next" & "Finish". In your main form add this code to whatever event it is that opens your form:

          Dim frm2 as New Form2
          frm2.Show()
          'use ShowDialog() if you don't want users to close
          'the form before completing process.

          Then in your second form add

          Option Strict On
          Imports System.Data
          Imports System.Data.SqlClient

          to the very top of the code editor. Add the code below to the OK button's click event (the naming in your code will probably be different but you get the idea),

          Private Sub btnOK_Click(...)Handles btnOK.Click
          'create new datasets and table adapters
          Dim dsFolder1 As New dsFolder
          Dim daFolder As New dsFolder.FolderTableAdapter

          'create a new row to add to the table
          Dim dr As DataRow = dsFolder1.Folder.NewRow()

          'try updating the database
          Try
          dr("folderName") = txtFolder.Text

          'add the datarow to the dataset
          dsFolder1.Folder.Rows.Add(dr)

          'update the database
          daFolder.Update(dsFolder1)
          Catch ex As Exception
          Messagebox.Show(ex.Message)
          End Try
          End Sub

          Using this kinda practice is better because the datasets and table adapters add type safety to your code, make it more readable as well as gives you objects that you can reuse later on in your code. This comes in really useful when you work on large databases. Also. make use of Try...Catch...End Try blocks. The structured error handling ability of .NET is tool worth using. You could go even further by implementing TraceListeners to write out performance data & errors from your code to a log file, which can only help you write better code. Sorry, for the long-winded explanation, I've had WAAAAY too much coffee this morning... ;P

          R Offline
          R Offline
          ranro2006
          wrote on last edited by
          #4

          kulazfuk, Thanks you so very much for your help. I was playing with something like this last night, but you clued me in on some other ideas. Thank you very much. It still is not working for me though. I could not create a table adapter with the line Dim daFolder As New dsFolder.FolderTableAdapter I used Dim daFolder As New FolderTableAdapters.FolderTableAdapter Everything else is the same, but it does not work.

          D 1 Reply Last reply
          0
          • R ranro2006

            kulazfuk, Thanks you so very much for your help. I was playing with something like this last night, but you clued me in on some other ideas. Thank you very much. It still is not working for me though. I could not create a table adapter with the line Dim daFolder As New dsFolder.FolderTableAdapter I used Dim daFolder As New FolderTableAdapters.FolderTableAdapter Everything else is the same, but it does not work.

            D Offline
            D Offline
            Dave Sexton
            wrote on last edited by
            #5

            Well, like I said before your naming conventions will probably be different so what you gotta look at is creating a table adapter for the dataset that you previously created. it'll follow a Dim _VariableName_ As New _DatasetTableAdapter.TableName_ type pattern. That should sort you out. :)

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups