Connecting sql server 2008 using vb.net

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
i am trying to access sql server 2008 r2 using vb.net. it tried the following code but the datas are getting saved temporarily. the data vanishes after the session ends. please help me
<pre class="prettyprint lang-vb" style="font-size:12px Imports System.Data.SqlClient
Public Class Addproducts
Dim mycommand As SqlCommand
Dim ra As Integer
Dim ra1 As Integer
Dim dr As SqlDataReader
Dim myconnection As SqlConnection
Dim sql As String
Dim item As String
Dim adapter As New SqlDataAdapter


Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click


myconnection = New SqlConnection("Data Source=.SQLEXPRESS;AttachDbFilename=|DataDirectory|kankukh.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True")
myconnection.Open()
mycommand = New SqlCommand("select count(*) from stock where product=" & TextBox1.Text & "")


mycommand.Connection = myconnection
ra = mycommand.ExecuteScalar
If (ra > 0) Then
MsgBox("The product already exists ", MsgBoxStyle.Exclamation)
TextBox1.Clear()
TextBox2.Clear()
TextBox1.Focus()
TextBox3.Clear()
TextBox4.Clear()
ListBox1.ClearSelected()
ListBox2.ClearSelected()
myconnection.Close()
Else
myconnection = New SqlConnection("Data Source=.SQLEXPRESS;AttachDbFilename=|DataDirectory|kankukh.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True")
myconnection.Open()
sql = "INSERT INTO stock VALUES(" & TextBox1.Text & "," & ListBox1.SelectedItem & "," & TextBox2.Text & "," & TextBox3.Text & "," & TextBox4.Text & "," & ListBox2.SelectedItem & ")"

adapter.InsertCommand = New SqlCommand(sql, myconnection)
adapter.InsertCommand.ExecuteNonQuery()
MsgBox("Member added successfully...!! ")
End If
myconnection.Close()


End Sub[/code]
thank you in advance
<br/><hr class="sig shehzi

View the full article
 
Back
Top