Click here to Skip to main content
15,901,426 members
Home / Discussions / Visual Basic
   

Visual Basic

 
AnswerRe: backup Pin
Dave Sexton9-Oct-06 0:57
Dave Sexton9-Oct-06 0:57 
GeneralRe: backup Pin
fatidarya9-Oct-06 19:05
fatidarya9-Oct-06 19:05 
QuestionCan not create web applicaion Pin
nkjha18-Oct-06 21:15
nkjha18-Oct-06 21:15 
AnswerRe: Can not create web applicaion Pin
Tom John8-Oct-06 22:21
Tom John8-Oct-06 22:21 
QuestionImport Excel sheets to MSSQL serever through Vb programming Pin
Neha_rakesh8-Oct-06 20:10
Neha_rakesh8-Oct-06 20:10 
QuestionAdding row to database question Pin
ranro20068-Oct-06 19:11
ranro20068-Oct-06 19:11 
AnswerRe: Adding row to database question Pin
ghost1818-Oct-06 19:42
ghost1818-Oct-06 19:42 
AnswerRe: Adding row to database question Pin
Dave Sexton8-Oct-06 21:17
Dave Sexton8-Oct-06 21:17 
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... Poke tongue | ;-P
GeneralRe: Adding row to database question Pin
ranro20069-Oct-06 5:07
ranro20069-Oct-06 5:07 
GeneralRe: Adding row to database question Pin
Dave Sexton10-Oct-06 22:43
Dave Sexton10-Oct-06 22:43 
Questionhide show clock taskbar properties on run time? Pin
ghost1818-Oct-06 18:52
ghost1818-Oct-06 18:52 
AnswerRe: hide show clock taskbar properties on run time? Pin
UltraCoder9-Oct-06 17:51
UltraCoder9-Oct-06 17:51 
AnswerRe: how to set connetion from vb6 to SQL Pin
Guffa8-Oct-06 19:20
Guffa8-Oct-06 19:20 
AnswerRe: how to set connetion from vb6 to SQL Pin
Guffa9-Oct-06 3:16
Guffa9-Oct-06 3:16 
QuestionWhat code can i use ? Pin
A*****8-Oct-06 16:52
A*****8-Oct-06 16:52 
AnswerRe: What code can i use ? Pin
Christian Graus8-Oct-06 17:16
protectorChristian Graus8-Oct-06 17:16 
AnswerRe: What code can i use ? Pin
UltraCoder9-Oct-06 17:09
UltraCoder9-Oct-06 17:09 
AnswerRe: What code can i use ? Pin
manish2411-Oct-06 1:07
manish2411-Oct-06 1:07 
QuestionLoading treeview with database Pin
ranro20068-Oct-06 10:00
ranro20068-Oct-06 10:00 
Questioninsert images in to textbox with vb.net Pin
fajrif8-Oct-06 8:36
fajrif8-Oct-06 8:36 
AnswerRe: insert images in to textbox with vb.net Pin
Christian Graus8-Oct-06 8:47
protectorChristian Graus8-Oct-06 8:47 
GeneralRe: insert images in to textbox with vb.net Pin
fajrif11-Oct-06 2:57
fajrif11-Oct-06 2:57 
QuestionSendMessage WM_KEYDOWN,WM_KEYUP [modified] Pin
Kenneth Broendum8-Oct-06 4:22
Kenneth Broendum8-Oct-06 4:22 
Questionms installer Pin
steve_rm7-Oct-06 23:40
steve_rm7-Oct-06 23:40 
AnswerRe: ms installer Pin
Christian Graus7-Oct-06 23:53
protectorChristian Graus7-Oct-06 23:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.