Home > ASP.NET > ASP.Net – How to update database data using VB.Net

ASP.Net – How to update database data using VB.Net

In my previous post on how to insert a new data into the database, now let’s take a look at how to update/edit the data that has been written into the database earlier. Below is a simple code to update data from database.

‘ import the SqlClient class library
Imports System.Data.SqlClient

‘declare the connection string
Dim connStr As String = "Data Source=.\SQLExpress;integrated security=true;attachdbfilename=|DataDirectory|\StudentDB.mdf;user instance=true;"

‘declare the connection
Dim myconn As New SqlConnection(connStr)

‘declare update SQL statement
Dim updatequery As String = "UPDATE <<TABLE>> SET company=@company,address=@address"

‘declare sqlcommand
Dim update As New SqlCommand(updatequery, myconn)

‘using WITH loop function to loop the data from textbox
With update.Parameters
    .Add(New SqlParameter("@company", txtncompany.Text.ToString.Trim))
    .Add(New SqlParameter("@address", txtaddress.Text.ToString.Trim))
End With

‘open connection to database
myconn.Open()

‘execute the query
renew.ExecuteNonQuery()

‘close the connection
myconn.Close()

Advertisements
Categories: ASP.NET
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: