Good day!.
I want to save my textbox to my sql database.
this is my code:
Dim con As New SqlConnection
Dim cmd As New SqlCommand
Try
con.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\dbEvaluation.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
con.Open()
cmd.Connection = con
cmd.CommandText = "INSERT INTO Comment([Comment]) VALUES ('" & TextBox1.Text & "')"
cmd.ExecuteNonQuery()

Catch ex As Exception
MessageBox.Show("Error while inserting record on table..." & ex.Message, "Insert Records")
Finally


con.Close()
End Try

End Sub


My problem is that code is running but it cant save the input data into my sql database.

What does it do?
Does it give an error or throw an exception?
...and is Comment the table name or column name?

sure it not save anything, because you don't have adapter to do that..

put this on your declaration part..

Dim adapter As New SqlDataAdapter

than put this upon "cmd.ExecuteNonQuery()" :

adapter.InsertCommand = cmd

where did i place that code?

Dim con As New SqlConnection
Dim cmd As New SqlCommand
Dim adapter As New SqlDataAdapter

Try
con.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\dbEvaluation.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
con.Open()
cmd.Connection = con
cmd.CommandText = "INSERT INTO Comment([Comment]) VALUES ('" & TextBox1.Text & "')"
adapter.InsertCommand = cmd
cmd.ExecuteNonQuery()

Catch ex As Exception
MessageBox.Show("Error while inserting record on table..." & ex.Message, "Insert Records")
Finally


con.Close()
i'll already insert that code but it cant work. :(

well i dont know what is the prob with this code but here is a code of insert .

dim con as new sqlconnection("connection string ")
dim cmd as new sqlcommand
con.open()
cmd.connection=con
cmd.commandtext="insert into table1 (id) values ("& txtid.text &")"
cmd.executenonquery()
con.close()

this will insert your records in your database , and there is no need to use dataadapter .

Regards

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.