Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > TableAdapter Query Connection
 

TableAdapter Query Connection

Using the wizard I created a query TableAdapter (qryTA)in my existing dataset. In the code I create an instance of qryTA. I then want to programatically change the connection string associated with the query. Now I can do this with other tableadapters I have created. The issue is that the Connection object does not show up in the available list. I made the connection Public but that did not expose it either.

Where is the Connection object?

Thanks,

Rick

Rick_in_Alpharetta  Tuesday, October 02, 2007 11:26 PM

Hi Rick,

Did you mean you cannot see the Connection property in the Property Window? If so, try to replace the code for Connection property of qryTA (in xxxDataSet.Designer.cs) with the following code, save this file, and rebuild you project.

Code Block

[System.ComponentModel.Browsable(true)]//make this property visible on Property Window

[System.Diagnostics.DebuggerNonUserCodeAttribute()]

public System.Data.SqlClient.SqlConnection Connection {

get {

if ((this._connection == null)) {

this.InitConnection();

}

return this._connection;

}

set {

this._connection = value;

if ((this.Adapter.InsertCommand != null)) {

this.Adapter.InsertCommand.Connection = value;

}

if ((this.Adapter.DeleteCommand != null)) {

this.Adapter.DeleteCommand.Connection = value;

}

if ((this.Adapter.UpdateCommand != null)) {

this.Adapter.UpdateCommand.Connection = value;

}

for (int i = 0; (i < this.CommandCollection.Length); i = (i + 1)) {

if ((this.CommandCollection[i] != null)) {

((System.Data.SqlClient.SqlCommand)(this.CommandCollection[i])).Connection = value;

}

}

}

}

Hope this helps.
Best regards.
Rong-Chun Zhang

Rong-Chun Zhang  Thursday, October 04, 2007 6:16 AM

Hi Rick,

Did you mean you cannot see the Connection property in the Property Window? If so, try to replace the code for Connection property of qryTA (in xxxDataSet.Designer.cs) with the following code, save this file, and rebuild you project.

Code Block

[System.ComponentModel.Browsable(true)]//make this property visible on Property Window

[System.Diagnostics.DebuggerNonUserCodeAttribute()]

public System.Data.SqlClient.SqlConnection Connection {

get {

if ((this._connection == null)) {

this.InitConnection();

}

return this._connection;

}

set {

this._connection = value;

if ((this.Adapter.InsertCommand != null)) {

this.Adapter.InsertCommand.Connection = value;

}

if ((this.Adapter.DeleteCommand != null)) {

this.Adapter.DeleteCommand.Connection = value;

}

if ((this.Adapter.UpdateCommand != null)) {

this.Adapter.UpdateCommand.Connection = value;

}

for (int i = 0; (i < this.CommandCollection.Length); i = (i + 1)) {

if ((this.CommandCollection[i] != null)) {

((System.Data.SqlClient.SqlCommand)(this.CommandCollection[i])).Connection = value;

}

}

}

}

Hope this helps.
Best regards.
Rong-Chun Zhang

Rong-Chun Zhang  Thursday, October 04, 2007 6:16 AM

Thank you for your response, I ended up adding to the partial class by adding a connection property, similar to your code snippet. After looking at the xxx.designer.vb code it appears that the qry table adapter is not built like the other adapters and does not include a connection property, why it gets built that way and not like the other TAs I don't know, seems like an oversight.

Regarding your suggestion, at least in vb the desinger code would get over written if I were to modify it and then rebuild.

Thanks,

Rick

Rick_in_Alpharetta  Thursday, October 04, 2007 12:58 PM

You can use google to search for other answers

Custom Search

More Threads

• Sorting databound ComboBox
• DataGridView
• Is it possible to have a checkbox flat but with the background always white with check black (Inside a DataGrid)
• HasChanges() question
• adding value items to combobox
• Datagridview math
• Exception while clearing the DataGridView Data source on a separate thread using Backgroundworker
• Datagrid: multiple data elements in one cell
• sql events
• Refreshing a data source