Adding row to database question
-
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.
-
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.
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 SQLTry cmd.Connection.Open() cmd.ExecuteNonQuery() Catch ex As OleDb.OleDbException msgbox(ex) finally connection.close() end try
i hope it can be help :) -
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.
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.SqlClientto 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 SubUsing 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 -
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.SqlClientto 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 SubUsing 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... ;Pkulazfuk, 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.
-
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.
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. :)