0

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.

4
Contributors
5
Replies
6
Views
4 Years
Discussion Span
Last Post by M.Waqas Aslam
0

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

Edited by thines01: n/a

0

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

Edited by artemix22: n/a

0

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. :(

0

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

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.