I am new to Vb.net and I'me trying to store data to the sql databse using text fields. Everything seems ok but i've tried several connection strings but they wont work.Could somebody give a hand, please?

Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Dim conn As New SqlClient.SqlConnection()
Dim cmd As New SqlClient.SqlCommand()
conn.ConnectionString = "Data Source=(local);Initial Catalog=myDatabase;Integrated Security=SSPI"
conn.Open()
cmd.Connection = conn
With cmd.Parameters
.Add(New SqlClient.SqlParameter("@Student_id", TextBox1.Text))
.Add(New SqlClient.SqlParameter("@FirstName", TextBox1.Text))
.Add(New SqlClient.SqlParameter("@LastName", TextBox2.Text))
.Add(New SqlClient.SqlParameter("@Email", TextBox3.Text))
.Add(New SqlClient.SqlParameter("@Module", TextBox1.Text))
End With

cmd.CommandText = "INSERT INTO <Students> (<field1>,<field2>,<field3>,<field4>,<field5>) VALUES (@Student_id, @FirstName, @LastName, @Email, @Module)"
Progress.Show()
End Sub

Recommended Answers

All 5 Replies

Member Avatar for iamthwee

Actually, after doing some research into the cmd.CommandText command, you probably don't need the single quotes.

I think the problem is the less than or greater signs around Student and the fields. Try removing them.

This is Visual Basic 6 String Connection

dim con as adodb.connection
dim rs as new adodb.recordset
Private Sub Form_Load()

Set con = New ADODB.Connection
ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\databasename.mdb;Persist Security Info=False"
con.Open ConnectionString

If rs.State = 1 Then rs.Close
sql = "select * tablename"
rs.Open sql, con, 3, 3

While Not rs.EOF
Call filldata(rs)
rs.MoveNext
Wend

End Sub

sub filldata(rs as adodb.recordset)
txtname.text = rs!name
cmbsex.text = rs!sex
txtage.text = rs!age
end sub

sub insertdata(byref rs as adodb.recordset)
rs.addnew
rs!name = txtname.text
rs!sex = cmbsex.text
rs!age = txtage.text
rs.update
msgbox"Record Saved"
end sub

sub cleardata()
txtname.text = ""
cmbsex.text = ""
txtage.text = ""
end sub

sub updatedata(byref rs as adodb.recordset)
rs.update
rs!name = txtname.text
rs!sex = cmbsex.text
rs!age = txtage.text
end sub

Public DBCONN As New SqlClient.SqlConnection("Data Source=JOYCEE_PARAGAS; Initial Catalog=positivedb; Integrated Security=false; user=sa; password=psu123!@#")
    Public ServerURL As String = "JOYCEE_PARAGAS"
    Public dbadapter As New SqlClient.SqlDataAdapter



 Dim insertcommand As New SqlClient.SqlCommand
        insertcommand.CommandText = "INSERT INTO employee(empFNAME,empLNAME) VALUES ('" & Me.TextBox2.Text & "', '" & Me.TextBox2.Text & "')"
        insertcommand.Connection = DBCONN
        DBCONN.Open()
        insertcommand.ExecuteReader()
        DBCONN.Close()
        MessageBox.Show("record as been inserted!", "information box", MessageBoxButtons.OK, MessageBoxIcon.Information)

joyvin: I'm certain you cannot run an insert command with the InsertCommand.ExecuteReader command.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.