|
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...
|
|
|
|
|
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.
|
|
|
|
|
Hi,
May i know how to uncheck the show clock in taskbar properties when my system on loading.
|
|
|
|
|
You have to use the Win32 API for that.
|
|
|
|
|
That doesn't look like a complete connection string, but the method might add things to it before it uses it. Look up the documentation for the method to see what it expects.
---
b { font-weight: normal; }
|
|
|
|
|
No, a DSN is a Data Source Name. A DSN can be a User DSN or a Machine DNS, and contains settings for a database connection. You access the data sources from Administrative Tools in the start menu.
---
b { font-weight: normal; }
|
|
|
|
|
Hi I am using vb.net2003,
Does anyone know any code I can use to delete files in a folder as well as the folder the files are located in?
Thanks for any help
|
|
|
|
|
You need to manually delete the files first. Directory.GetFiles will give you a list of files to delete.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
To delete files, use: My.Computer.FileSystem.DeleteFile("C:\File_To_Delete.txt")
You can also select whether you want the file you are deleting to goto the recycle bin or just to be permanantly deleted. Just use:
My.Computer.FileSystem.DeleteFile("C:\File_To_Delete.txt", FileIO.RecycleOption.DeletePermanently)
|
|
|
|
|
try this
imports system.io
Dim objDir As New DirectoryInfo("C:\testdel")
objDir.Delete(True)
|
|
|
|
|
I am using VB.net 2005 express and I have created a dataset with SQL server 2005 express. I want to use that dataset to load the nodes in a treeview control. The dataset consists of two tables one is called the State table and the other is called the City. I want the State table to be parent nodes and the corresponding city table rows to be child nodes beneath it. Does some know how to do this?
Thanks.
|
|
|
|
|
hi, are you guys know how to insert an image into textbox / richtextbox
just like chat application (like this), with vb.net coding?
thanx guys!!!
|
|
|
|
|
Not sure about richtextbox, but a plain textbox, you can't, not without owner drawing, and as a textbox paints itself outside of WM_PAINT, that's a major headache.
So, I'd have to assume the RTB does support it.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
okay, thanx ! i already find out (using clipboard and dataformats)
|
|
|
|
|
I'm trying to control navigation in one application from another application.
I can control almost anything but navigation.
Forexampel the following is completely ignored by the receiving application:
SendMessage hwnd, WM_KEYDOWN, VK_TAB, &HF0001
SendMessage hwnd, WM_CHAR, VK_TAB, &HF0001
SendMessage hwnd, WM_KEYUP, VK_TAB, &HC00F0001
(values obtained with Spy++)
but
SendMessage hwnd, WM_CHAR, Asc("b"), 0
is accepted.
What am I missing ?
Kenneth
-- modified at 11:56 Sunday 8th October, 2006
|
|
|
|
|
Hello,
I found a good solution using clickonce, for detecting if the application is first run. Using the following code:
<br />
If (System.Deployment.Application.ApplicationDeployment.CurrentDeployment.IsFirstRun)) Then Dim setConnection As New ServerSettings() setConnection.Show() Else Dim logMeIn As New Login() logMeIn.Show() End If<br />
However, the customer has just changed their mind. And they they want to specify the location of where they want to install the application. Clickonce only has a default that cannot be changed.
So I using the ms installer. However the above code does not work for it. It seems
I would like the serverSettings to run only on first startup. However, I am not sure about the code for this, as the above does not work.
Does amyone know the synstax for 'first run' when using ms installer?
I think I am destined not to get this finished, with the customer changing their mind.
Many thanks for your help,
Steve
|
|
|
|
|
You could store a key in the registry after you've run the serverSettings, and check it prior, so it only runs when they registry key is not set.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
Hello,
Thanks for your help.
Do you know of any examples that allow you to enter a key into the registery.
Thanks for your help,
Steve
|
|
|
|
|
I'm using Vb.net.
How to make a timer to read mouse cursor position on the form? I know how to use MouseMove event but it won't fire if mouse cursor is still. I would like to have a timer that would constantly report what are the current cordinates of the cursor.
|
|
|
|
|
Cursor.Location is, from memory how to find this out. But, I don't get why you'd do this. When the cursor moves, you can store the value or otherwise interact with it. When the cursor isn't moving, the value isn't changing, so why do you need to keep asking for it ?
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
when i click button of my app then this messege appear in immediate window:
"A first chance exception of type 'System.IO.IOException' occurred in mscorlib.dll"
and the function doesnt work and when sometimes this messege not appear then function work
someone know why it is coming?
|
|
|
|
|
There is an error in some file operation. What does the function do? Does it involve any file operations?
---
b { font-weight: normal; }
|
|
|
|
|
this is the function
Private Function SaveFile() As Boolean
Try
Dim value1 As String = Text1.Text
Dim fName As String
If lblmsg.Text = "IPL NOT LOAD" Then
Save.ShowDialog()
fName = Save.FileName
If Save.FileName <> "" Then
Dim fSave As New StreamWriter(fName)
fSave.WriteLine("# File Created By ABC")
fSave.WriteLine(value1)
fSave.Flush()
fSave.Close()
lblmsg.Text = fName
lblmsg.TextAlign = ContentAlignment.MiddleLeft
lblmsg.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!)
savechk = False
End If
Else
fName = lblmsg.Text
Dim fSave As New StreamWriter(fName)
If InStr(Text1.Text, "# File Edited By ABC") = True Then
fSave.WriteLine(value1)
fSave.Flush()
fSave.Close()
lblmsg.Text = fName
lblmsg.TextAlign = ContentAlignment.MiddleLeft
lblmsg.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!)
savechk = False
ElseIf InStr(Text1.Text, "# File Created By ABC") = True Then
fSave.WriteLine(value1)
fSave.Flush()
fSave.Close()
lblmsg.Text = fName
lblmsg.TextAlign = ContentAlignment.MiddleLeft
lblmsg.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!)
savechk = False
Else
fSave.WriteLine("# File Edited By ABC")
fSave.WriteLine(value1)
fSave.Flush()
fSave.Close()
lblmsg.Text = fName
lblmsg.TextAlign = ContentAlignment.MiddleLeft
lblmsg.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!)
savechk = False
End If
End If
Catch
End Try
End Function
|
|
|
|
|
You are catching the exception and ignoring it. Never do that! (Notice the exclamation mark that I used. I use less than a dozen per year, so when I do, they mean something.) If you don't know what to do with it, just throw it again, so that some higher level of the program can catch it.
Also, your error handling doesn't make sure that the files are closed if an error occurs. I believe that the error message that you see comes from some other part of your program when it tries to use a file that has been left open by the function.
Use Try and Finally to make sure that the files are always closed:
Dim fSave
Try
fSave = As New StreamWriter(fName)
fSave.WriteLine("# File Created By ABC")
fSave.WriteLine(value1)
Finally
If not fSave is Nothing Then
fSave.Close()
End If
End Try
You can add a Catch section before the Finally to handle any exception that occurs in this specific part of the code, or wrap all the code in the function inside a Try ... Catch to take care of any exception that occurs anywhere in the function.
---
b { font-weight: normal; }
|
|
|
|