EDN Admin
Well-known member
Hi all,
how can I insert data in a sql table using a textbox. I use this code but it doesnt work.
Imports System.Data<br/>
Imports System.Data.SqlClient<br/>
<br/>
Public Class Form1<br/>
<br/>
Private Property com As Object<br/>
<br/>
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click<br/>
Dim con As New SqlConnection<br/>
Dim cmd As New SqlCommand<br/>
Try<br/>
con.ConnectionString = "Data Source=.SQLEXPRESS;AttachDbFilename=|DataDirectory|DBname.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"<br/>
con.Open()<br/>
cmd.Connection = con<br/>
cmd.CommandText = "INSERT INTO Protocol (Protocol) VALUES (" & TextBox1.Text & ")"<br/>
cmd.ExecuteNonQuery()<br/>
<br/>
Catch ex As Exception<br/>
MessageBox.Show("Error while inserting record on table..." & ex.Message, "Insert Records")<br/>
Finally<br/>
con.Close()<br/>
End Try<br/>
End Sub<br/>
End Class
Thanks for the reply!
View the full article
how can I insert data in a sql table using a textbox. I use this code but it doesnt work.
Imports System.Data<br/>
Imports System.Data.SqlClient<br/>
<br/>
Public Class Form1<br/>
<br/>
Private Property com As Object<br/>
<br/>
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click<br/>
Dim con As New SqlConnection<br/>
Dim cmd As New SqlCommand<br/>
Try<br/>
con.ConnectionString = "Data Source=.SQLEXPRESS;AttachDbFilename=|DataDirectory|DBname.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"<br/>
con.Open()<br/>
cmd.Connection = con<br/>
cmd.CommandText = "INSERT INTO Protocol (Protocol) VALUES (" & TextBox1.Text & ")"<br/>
cmd.ExecuteNonQuery()<br/>
<br/>
Catch ex As Exception<br/>
MessageBox.Show("Error while inserting record on table..." & ex.Message, "Insert Records")<br/>
Finally<br/>
con.Close()<br/>
End Try<br/>
End Sub<br/>
End Class
Thanks for the reply!
View the full article