Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm not sure what exactly is wrong, I can only imagine that <code>connectionStringI</code> is somehow <code>Null</code> when you are trying to use it, but I think it is good practice to close the connection every time you are done using it.</p> <p>So when you fetch the data instead of leaving the connection open as you do now, do something like</p> <pre><code>With New SqlConnection(connectionString) Try .Open() ' Do Stuff Catch ' Handle your exception Finally .Close() End Try End With </code></pre> <p>That way your connection will always be closed and you won't have to worry about checking whether or not it is already open.</p> <p>UPDATE</p> <p>I have taken the liberty of rewriting your code the way I would do it. Can you please give this a try and see if it works for you/</p> <p>You connection string property:</p> <pre><code>Private ReadOnly Property connectionString As String Get Return "Data Source=XXXX;Initial Catalog=XXXX;User ID=XXXX;Password=XXXX" End Get End Property </code></pre> <p>The sub which inserts the data:</p> <pre><code>Private Sub btnADD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnADD.Click Dim transaction As SqlTransaction = Nothing With New SqlConnection(connectionString) Try Call .Open() transaction = .BeginTransaction() With .CreateCommand() .Transaction = transaction .CommandText = "INSERT INTO [tbl_info] ([ID], [Name]) VALUES (@ID,@Name);" With .Parameters .AddWithValue("ID", txtID.Text) .AddWithValue("Name", txtName.Text) End With Call .ExecuteNonQuery() Call transaction.Commit() Call MessageBox.Show("has been inserted successfully") End With Catch ex As Exception Call transaction.Rollback() Call MessageBox.Show(ex.Message, "Error") Finally Call .Close() End Try End With Call txtID.Clear() Call txtName.Clear() End Sub </code></pre> <p>The sub which updates the data:</p> <pre><code>Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click Dim transaction As SqlTransaction = Nothing With New SqlConnection(connectionString) Try Call .Open() transaction = .BeginTransaction() With .CreateCommand() .Transaction = transaction .CommandText = "UPDATE [tble_info] SET [Name]=@Name WHERE [ID]=@ID;" With .Parameters .AddWithValue("Name", txtName.Text) .AddWithValue("ID", txtID.Text) End With Call .ExecuteNonQuery() Call transaction.Commit() Call MessageBox.Show("has been update successfully") End With Catch ex As Exception Call transaction.Rollback() Call MessageBox.Show(ex.Message, "Error") Finally Call .Close() End Try End With End Sub </code></pre> <p>If you make the connection string a (read-only) property, assuming it doesn't change, then you won't have to worry about whether it is has a value or not when you use it. Then all you have to worry about is make sure that the connection string is correct.</p> <p>Note that I haven't tested this code, but it should work as is I think.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload