0
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim strsql As String = "Data Source=.ITSupport;" & _
        "Initial Catalog=Information;Integrated Security=true"
        strsql = " insert into table_1 (name, address, age ) values ('" _
        & txtname.Text & "','" _
        & txtaddress.Text & "','" _
        & txtage.Text & "')"
        Dim sqlcmd As New SqlClient.SqlCommand
        sqlcmd.CommandText = strsql
        MsgBox("save")            
    End Sub

End Class

But my data still not exist inside the db. there is no error found.. can some help me on this asap?

Edited by Reverend Jim: Fixed formatting

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

You haven't defined a connection, used your connection string (infact you overwrote it) and finally didn't execute the command - hence the no errors.

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim connstr As String = "Data Source=.\ITSupport;" & _
"Initial Catalog=Information;Integrated Security=true"
Dim strsql as String
strsql = " insert into table_1 (name, address, age ) values ('" _
& txtname.Text & "','" _
& txtaddress.Text & "','" _
& txtage.Text & "')"

dim sqlcon as new SqlClient.SqlConnection
Dim sqlcmd As New SqlClient.SqlCommand

sqlcon.connectionstring = connstr
sqlcmd.CommandText = strsql
sqlcmd.connection = sqlcon 

try 
sqlcon.open 
sqlcmd.executescalar 

catch e as exception 
msgbox(e.message) 
end try


End Sub
End Class

I suggest you visit http://www.connectionstrings.com and verify your connection string.

0

I don't know how I ended back here, but I just saw that I typed in sqlcmd.executescalar that won't work. Replace it with sqlcmd.executenonquery

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.