I have created one that appears to work, but not correctly.
I have created a windows application project in VS 2005. The Form has a label, textbox and 2 buttons (One to display the contents of the DB and the other to insert). I have also added a DB and created 2 tables with no primary key. I created the DB in VS. When pressed, the Insert button takes the value in the textbox and adds it to the DB and then displays all the contents of the DB.
However, when I enter a value into the textbox and hit insert, it looks like I've added it to the DB, but when I actually open up the DB in VS the values are not there and when I run the program again, the values are not there.
If there is any help that can be given, I would really appreciate it. Whether it be adding to my code or pointing me into another direction.
Here is all my code for the Form
Imports System.Data.Sqlclient
Public Class Form1
Private ConnString As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ConnString = "Data Source= .\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ADVENTUREWORKS_DATA.mdf;Integrated Security=True;User Instance=True"
ReadMyData(ConnString)
End Sub
Public Sub ReadMyData(ByVal myConnString As String)
Dim mySelectQuery As String = "SELECT ProductID, Name FROM Product"
Dim myConnection As New SqlConnection(myConnString)
Dim myCommand As New SqlCommand(mySelectQuery, myConnection)
Dim StrFiller As String
myConnection.Open()
Dim myReader As SqlDataReader
myReader = myCommand.ExecuteReader()
' Always call Read before accessing data.
While myReader.Read()
StrFiller += ((myReader.GetInt32(0) & ", " & myReader.GetString(1) & Environment.NewLine))
End While
Label1.Text = StrFiller
' always call Close when done reading.
myReader.Close()
' Close the connection when done with it.
myConnection.Close()
End Sub 'ReadMyData
Public Sub InsertMyData(ByVal myConnString As String, ByVal NameStr As String)
Dim mySelectQuery As String = "SELECT ProductId, Name FROM Product"
Dim myInsertQuery As String = "INSERT into Product(Name, ProductID) VALUES('" & NameStr & "','" & 1110 & "')"
Dim myConnection As New SqlConnection(myConnString)
Dim myCommand As New SqlCommand(mySelectQuery, myConnection)
Dim myCommand2 As New SqlCommand(myInsertQuery, myConnection)
Dim retvalue As Integer
Dim StrFiller As String
myConnection.Open()
retvalue = myCommand2.ExecuteNonQuery()
Label1.Text = retvalue
Dim myReader As SqlDataReader
myReader = myCommand.ExecuteReader()
' Always call Read before accessing data.
While myReader.Read()
StrFiller += ((myReader.GetInt32(0) & ", " & myReader.GetString(1) & Environment.NewLine))
End While
Label1.Text = StrFiller
' always call Close when done reading.
myReader.Close()
' Close the connection when done with it.
myConnection.Close()
End Sub 'ReadMyData
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Insert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Insert.Click
Dim NameStr As String
Dim id As Integer
NameStr = Me.TextBox1.Text
'ConnString = "Data Source= .\SQLEXPRESS;Initial Catalog = C:\ADVENTUREWORKS_DATA.mdf;Integrated Security=True;User Instance=True;"
ConnString = "Data Source= .\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ADVENTUREWORKS_DATA.mdf;Integrated Security=True;User Instance=True"
InsertMyData(ConnString, NameStr)
End Sub
Private Sub exitbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitbtn.Click
Me.Close()
End Sub
End Class
Thanks

New Topic/Question
Reply




MultiQuote





|