0

I have written this code in visual basic. On executing no error is printed but the new row is not added to the database. I have tried using datasets also but that didnt work either. Any ideas?

Dim conSQL As SqlConnection = New SqlConnection
    conSQL.ConnectionString = "Data Source=USER-PC\SQLEXPRESS;Initial Catalog=Phd;Integrated Security=True"
    conSQL.Open()

    Dim cmd As New SqlCommand("Insert into Phd_Student(student_id,student_name,student_email) values ('" + idnotextbox.Text + "','" + studnametextbox.Text + "','" + studemailtextbox.Text + "')" , conSQL)

  cmd.ExecuteNonQuery()

3 Answers 3

1

There are only two possible outcomes of executing an insert; either it adds a record or you get an exception. So the alternatives in your case are:

  1. The code that you showed is not executed at all.
  2. You are catching the exception and ignoring it.
  3. The actual code that you have is something different from what you posted.
  4. You have created a trigger in the database that removes the record.
  5. One of the values in the textboxes uses SQL injection to remove the added value*.

*) If you enter the value -1','','');delete Phd_Student where student_id='-1'-- in the id textbox, that would add a record and then remove it.

Sign up to request clarification or add additional context in comments.

Comments

0

1- You should open the connection before executing the command.

try
conSQL.open()
 Dim cmd As New SqlCommand("Insert into Phd_Student(student_id,student_name,student_email) values ('" + idnotextbox.Text + "','" + studnametextbox.Text + "','" + studemailtextbox.Text + "')" , conSQL)

  cmd.ExecuteNonQuery()
Finally
conSQL.close()
end try

2- you should pass parameters to the query not like this way, to avoid SQL Injection.

Comments

0

first thing, make sure to call conSQL.Close() after cmd.ExecuteNonQuery() line.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.