Solved

Datagridview Update with SQLCommandBuilder Problem

Posted on 2012-03-18
5
383 Views
Last Modified: 2012-03-27
I have a WinForms app and have a situation where I fill a Datagridview with info and then the connection to the Database is closed.    It could some time later that the person needs to edit a cell in the Datagridview.  

This code works with the connection already open.  But, if it is not open, what do I need to add to this code to get the Update to work?   The connection to the sqlserver needs to be re-established.    Or, does the connection need to be Open at all times?

         Try
                 Dim cb As New Data.SqlClient.SqlCommandBuilder(DataAdapter)
                 DataAdapter.Update(Dataset, "Memo")
        Catch ex As Exception
                 MessageBox.Show(ex.Message, "Error")
        End Try

I am trying to follow the best practice of closing the connection when not needed.
0
Comment
Question by:browe68
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37740181
DataAdapter manages its connection itself. Do you close the connection explicitly?
0
 

Author Comment

by:browe68
ID: 37743076
I do close it explicitly b/c it could be minutes/hour before the update/edit may occur (depending upon user activity) and I do not want to keep the connection open that long.  So, I close it.

Maybe that is the wrong approach.    Any suggestions?
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37743943
Then initialize and open it again before you call the Update method.

Create and open a new connection and set it as insertcommand, updatecommand, deletecommand connection property.
0
 

Author Comment

by:browe68
ID: 37744548
Any examples of that?  Thanks very much for your help.
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 37774278
Dim conn As New SqlConnection("connectionstring")
conn.open

DataAdapter.SelectCommand.Connection = conn
DataAdapter.InsertCommand.Connection = conn
DataAdapter.UpdateCommand.Connection = conn
DataAdapter.DeleteCommand.Connection = conn
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sql2016-WIn10: standard,for SQL servc-account.. 51 44
Get sourcecode path 14 47
SQL Server Express or Standard? 5 32
How can I add google maps in vb.net. 2 22
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question