You are on page 1of 6

12.

5M

(46K on)

Sign in

articles Q&A forums Lounge

Ask a Question

Search for articles, questions, tips

All Unanswered

FAQ

sql connection using vb.net


See more: VB

VB.NET

Rate this:

Hello,
I created a windows form where we use a text box and a button.
What I want is when i click on the button the data is input in the textbox will store in the
MSSQL table name as student
and the col name is stuname
When i run the form its shows the form and when i input the data in the text box
and click on the button nothing happens,
and data is also not stored in the database table
please help me out
the code is given below
Hide Expand

Copy Code

Imports System.Data
Imports System.Data.SqlClient
Imports System.Windows.Forms

Public class Form1


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim con As SqlConnection
Dim cmd As SqlCommand
Dim row As Integer
'Dim str As String
con = New SqlConnection("server=YASH;database=test;integrated
security=true")
con.Open()
cmd = New SqlCommand("insert into([stuname]) student values ('" &
TextBox1.Text & "')", con)
row = cmd.ExecuteNonQuery()
If row > 0 Then
MessageBox.Show("the row inserted" & row)
End If
con.Close()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
End Sub
End Class
Posted 17-Mar-11 21:35pm Updated 17-Mar-11 22:17pm v2
ahsan.subiya 681
Tarun.K.S 42.2K

Add a Solution
Comments
Toniyo Jackson 18-Mar-11 3:48am
So, what is your problem?

1 solution
Solution 2
This is how i did (Its

Rate this:

an Example) :
Hide Copy Code

Dim conString As String = "Server=hcl\sqlexpress;Initial Catalog=vbtry;Integrated


Security=True"
Dim conn As SqlConnection = New SqlConnection(conString)
Dim adap As New SqlDataAdapter
Dim ds As New DataSet

Hide Expand

Copy Code

Private Sub btnInsert_Click(ByVal sender As System.Object, ByVal e As


System.Windows.RoutedEventArgs)
Dim selCmdText As String = "SELECT *FROM DEPARTMENTS"
Dim selCommand As New SqlCommand(selCmdText, conn)
Dim insCmdText As String = "INSERT INTO
DEPARTMENTS(Department_id,Department_name,Manager_id,Location_id)" & _
"VALUES(@DepId,@DepName,@ManId,@LocId)"
Dim insCommand As New SqlCommand(insCmdText, conn)
Dim adp As New SqlDataAdapter(selCommand)
adp.InsertCommand = insCommand

Dim param As New SqlParameter


param.ParameterName = "@DepId"
param.SqlDbType = SqlDbType.SmallInt
param.Value = Integer.Parse(txtDepId.Text)
insCommand.Parameters.Add(param)

param = New SqlParameter


param.ParameterName = "@DepName"
param.SqlDbType = SqlDbType.VarChar
param.Size = 15
param.Value = txtDepName.Text
insCommand.Parameters.Add(param)

param = New SqlParameter


param.ParameterName = "@ManId"

As you can see, I have used SqlDataAdapterclass. With this I don't have to care about the
opening and closing of connection. It will fill the values in the DataTablewhich on
updating can be updated back to the Database. Second thing is the use of SqlParameters
for each values that I want to insert to prevent SQL Injections. Avoid directly writing
TextBox1.text, if someone writes DROP TABLEand other malicious commands, you can
imagine what will happen!
Thirdly, I would recommend you to use LINQ to SQL, it's very easy to use and you don't
have to write the big coding stuff above. Here is the link :
http://www.codeproject.com/KB/linq/linqtutorial.aspx[^]
Hope it helped! :)

Posted 17-Mar-11 22:35pm


Tarun.K.S 42.2K

Updated 17-Mar-11 22:53pm v3

Comments
ulyses31 18-Mar-11 4:44am
you can your coding by using Typed Dataset. saves a lot of time & effort.

Tarun.K.S 18-Mar-11 4:46am


Was it you who downvoted this answer? If so, could you explain what's wrong with it?

ahsan.subiya 18-Mar-11 6:07am


sir i tried this code bt it is giving me the error
adp.fill(dt)
the error is
value of string type cannot be converted syste.data.sqlclient

one more thing why u used the select comand

Tarun.K.S 18-Mar-11 6:48am


I used select command to fill the adapter with the data present in the Table. Then you can insert, update or delete.
Regarding the error in string, check the sqlDbType of the parameter.

Tarun.K.S 18-Mar-11 7:58am


Did it work now?

ulyses31 18-Mar-11 4:44am


you can minimize

Add your solution here

Preview

Existing Members

Sign in to your account

...or Join us

Download, Vote, Comment, Publish.

Your Email
Password

Your Email
Optional Password

Forgot your password?


I have read and agree to the Terms of
Service and Privacy Policy
Please subscribe me to the
CodeProject newsletters. You may
withdraw your consent and unsubscribe
at any time.

Submit your solution!

When answering a question please:


1. Read the question carefully.
2. Understand that English isn't everyone's first language so be lenient of bad
spelling and grammar.
3. If a question is poorly phrased then either ask for clarification, ignore it, or edit
the question and fix the problem. Insults are not welcome.
Let's work to help developers, not make them feel stupid.

This content, along with any associated source code and files, is licensed under The Code Project Open
License (CPOL)

Discussions and Feedback


Visit to post and view comments on this entry, or click here to get a print view with
messages.

Advertise | Privacy | Mobile


Web02 | 2.8.160904.1 | Last Updated 18 Mar 2011

Copyright CodeProject, 1999-2016


All Rights Reserved. Terms of Service

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100

You might also like