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?

Dani AI

Generated

The problem in the original post is twofold: the connection string variable was overwritten and the command was never sent to SQL (so no error appeared because nothing executed). correctly called out that a connection must be created/assigned and that an INSERT should be executed with ExecuteNonQuery rather than ExecuteScalarExecuteNonQuery is the right API for INSERT/UPDATE/DELETE and returns the number of rows affected. Also verify the connection-string syntax (named instances use the server\instance form). (learn.microsoft.com)

A safer, minimal pattern is: use Using blocks so connections/commands are always disposed, use parameterized SQL to avoid quoting/encoding bugs and SQL injection, and validate typed values (age) before binding. Example (VB.NET):

Imports System.Data.SqlClient

Dim conn As String = "Data Source=MYSERVER\MYINSTANCE;Initial Catalog=Information;Integrated Security=True;"
Dim sql As String = "INSERT INTO table_1 ([name],[address],[age]) VALUES (@name,@address,@age);"

Using cn As New SqlConnection(conn)
    Using cmd As New SqlCommand(sql, cn)
        cmd.Parameters.Add("@name", SqlDbType.NVarChar, 100).Value = txtname.Text
        cmd.Parameters.Add("@address", SqlDbType.NVarChar, 200).Value = txtaddress.Text

        Dim ageVal As Integer
        If Integer.TryParse(txtage.Text, ageVal) Then
            cmd.Parameters.Add("@age", SqlDbType.Int).Value = ageVal
        Else
            cmd.Parameters.Add("@age", SqlDbType.Int).Value = DBNull.Value
        End If

        cn.Open()
        Dim rowsInserted As Integer = cmd.ExecuteNonQuery()
        MessageBox.Show(rowsInserted.ToString() & " row(s) inserted.")
    End Using
End Using

The Using pattern guarantees Dispose even on exceptions, and the Parameters collection is where typed parameters belong — both improve reliability and security. Parameterized queries also prevent classic SQL injection risks and handle embedded quotes safely. (learn.microsoft.com)

Quick checklist to resolve similar issues:

  • Confirm the INSERT actually runs (ExecuteNonQuery > 0).
  • Ensure the connection string targets the correct server/instance and database.
  • Verify the account has INSERT permission on the target table.
  • Make sure column names/types match (use explicit SqlDbType for integers/dates).
  • Wrap open/execute in try/catch and surface the exception message during testing so hidden errors become visible.

References in the thread: identified the missing connection/execute steps; the code above shows a complete, safe execution pattern that addresses those gaps.

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.