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?

Recommended Answers

All 2 Replies

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.

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

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.