3
Contributors
2
Replies
4
Views
10 Years
Discussion Span
Last Post by sierrainfo
0

A simple code on how to connect sql 2005 from VB.Net or ASP.Net via SQL connection string, and the use of ExecuteNonQuery()

method to execute the command and the try and catch to catch errors during runtime.

Example information:

SQL data:

Database name: user

Table name: user_t

Columns: user_id,lname,fname,username,password,usertype


Controls:

textboxes:

textID = for Id

textlname = for last name

textfname = for first name

textusername = for user name

textpassword = for password

combobox:

comboUsertype = for user type


Code:

Dim con As New SqlClient.SqlConnection

Dim strCon as string = "Data Source=Admin\SQLEXPRESS;Initial Catalog=user;Integrated Security=True"

Dim strCommand as string = “Insert into user_t (user_id, lname, fname, username, password, usertype) values ('”&

me.textID.text &”','” & me.textlname.text &”', '” &me.fname.text &”', '”&me.textusername.text &”','”& me.textpassword.text

&”','”&me.comboUsertype.text &”')”

Sub insert()


con.ConnectionString = strCon

Dim cm As New SqlClient.SqlCommand(strCommand, con)

con.Open()

cm.ExecuteNonQuery()

MsgBox("Succesfully saved…")

con.close

End sub

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.