guys... please help me to save, edit and delete files in the data grid...
can you give me the codes so i can do it...
i badly need it for my system...

6 Years
Discussion Span
Last Post by JJCollins

you'll need SQL queries to achieve these things:

Here is the step-by-step procedure to INSERT a record into an SQL database:

1. Create your VB.NET project.
2. Include the following namespaces.

Imports System.Data 'Required for SQL
Imports System.Data.SqlClient 'Required for SQL

The System.Data namespace provides access to classes that represent the ADO.NET architecture while the System.Data.SqlClient namespace is the .NET Framework Data Provider for SQL Server.

3. Declare and instantiate your SQLConnection object and Command object as shown below

Dim con As New SqlConnection
Dim cmd As New SqlCommand

4. Pass the SQL connection string to ConnectionString property of your SQLConnection object.

con.ConnectionString = "Data Source=example;Initial Catalog=example;Persist Security Info=True;User ID=admin;Password=admin"

5. Use the Open Method to connect to SQL Server.


6. Set the connection of the command object.

cmd.Connection = con

7. Pass the INSERT SQL statement to the command object commandtext as shown below

cmd.CommandText = "INSERT INTO ExampleTable (field1, [field2, ... ]) VALUES (value1, [value2, ...])"

8. Use the ExecuteNonQuery() method to run the INSERT SQL statement that has no return value.


If you want a return value use ExecuteScalar() instead.
A good example of ExecuteScalar is to get the Identity column after inserting record(ExecuteScalar returns the first column of the first row from the result).

Full sample source code of inserting record to SQL Database:

Dim con As New SqlConnection 'Define variable con as a new SQL Server Connection
Dim cmd As New SqlCommand 'Define variable cmd as a new SQL Server Command
Try 'Used for Error Logging - Try/Catch
con.ConnectionString = "Data Source=examplesource;Initial Catalog=Example;Persist Security Info=True;User ID=admin;Password=admin" 'Sets the properties of the connection string
con.Open() 'Invokes the server connection to Open
cmd.Connection = con 'The Connection Used for the SQL Command
cmd.CommandText = "INSERT INTO ExampleTable([field1], [field2]) VALUES([Value1], [Value2])" 'Sets The Command Text To An Insert Query

Catch ex As Exception ' If there's an error, catch it!
MsgBox("Error while inserting record on table..." & ex.Message, "Insert Records") 'Displays a Message Box popup to the user notifying them of an error (if any)
con.Close() 'Close the SQL connection (Well you wouldn't leave your car door open when you get out of it would you?)
End Try

Edited by JJCollins: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.