Click to See Complete Forum and Search --> : ADODC and DataGrid - programatic control at runtime
Nigel Taylor
April 27th, 1999, 04:18 PM
I have an AdoDC in a Dialog and a DataGrid bound to it. The AdoDC has a connection string that points at a specific database, specified at design time.
I need to set the connection string at runtime, before the AdoDC attempts to connect to the database. How can I do that?
(I have tried calling Adodc::SetConnectionString() from the dialog OnInitDialog method, but it's too late. The AdoDC has already connected to the database soecified at design time.)
April 28th, 1999, 12:07 AM
At design time,Don't set any property for AdoDC and DataGrid,
at runtime,like below:
Adodc1.CommandType = adCmdText
Adodc1.ConnectionString = adConnectStr
Adodc1.RecordSource = adSQLStr
Set DataGrid1.DataSource = Adodc1
The code above is used by VB, but it should work with Visual C++
cyberplasm
April 28th, 1999, 12:34 AM
Maybe leaving the connection string and recordsource empty for the adodc will
allow you to rebind a grid in VB but I haven't been able to do it in VC.
If you do leave the connection string and recordsource empty in the data
control, I verified you can then set these successfully at runtime in VC.
And you avoid all the nasty error dialogs if your design time strings are
wrong.
m_adodc.SetConnectionString(
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\any.mdb;Persist Security Info=False");
m_adodc.SetCommandType(2);
m_adodc.SetRecordSource("MyTable WHERE ID < 20");
m_adodc.Refresh();
m_adodc.UpdateData();
TRACE("GetRecordSource = %s\n", m_adodc.GetRecordSource() );
TRACE("GetConnectionString = %s\n", m_adodc.GetConnectionString() );
m_adodc.GetRecordset().MoveFirst();
Everything ok, to this point, and you have achieved your purpose. But ReBind to
a datagrid is not working for me. The grid returns null for the IUnknown
pointer to the datasource if the datacontrol has the blank initial settings. It
may be that the grid thinks its in unbound mode, and refuses to be reset to
bound, despite how you set it?
This doesn't work:
IUnknown * pU;
m_adodc.EnableAutomation();
LPDISPATCH lpd = m_adodc.GetIDispatch(TRUE);
lpd->QueryInterface( IID_IUnknown, (void**)&pU );
m_datagrid.SetDataMode(0);
m_datagrid.SetDataSource(pU);
pU->Release();
m_datagrid.ReBind();
TRACE("GetDataSource = 0x%lx, SetDataSource = 0x%lx\n", m_datagrid.GetDataSource(), (DWORD)pU );
In fact the SetDataMode(0) crashes it. Remove it and GetDataSource is 0.
I would appreciate some help on this too.
Nigel Taylor
April 28th, 1999, 09:05 AM
I'd figured out the first part (how to set up the AdoDC) but I could not figure out how to form the IUnknown pointer to pass to the data grid SetDataSource method. Thanks very much for clearing that up!
I'll play around some more with this idea and if I get anywhere I'll let you know.
Thanks again!
Nigel Taylor
April 28th, 1999, 09:09 AM
If you can make this work in VC++ I'd be most interested. See the previous reply.
cyberplasm
April 28th, 1999, 11:37 AM
Well, DataMode is read-only I think. So leave it as bound in the settings since SetDataMode causes an application error.
It may be possible to put the connection string in the registry using the control's Registry Path setting, but there is too much great microsoft documentation on this. They should be writing puzzles for a living. As I track all this down in Dejanews and in MSDN, it seems nobody is working on this, and we are pioneers.
Here is a workaround that may be usable. If the goal is to set the database at runtime, then use a udl file, and build the connection string as needed. You can write the connection string into the file before you open the dialog or form that contains your grid. When you then init the datacontrol and grid it will use the udl file, if you set the connection parameters in your datacontrol to use a udl file.
#include <oledb.h>
#include <msdasc.h>
HRESULT hr = S_OK;
IDataInitialize *pIDataInitialize = NULL;
hr = CoInitialize(NULL);
hr = CoCreateInstance( CLSID_MSDAINITIALIZE, NULL, CLSCTX_INPROC_SERVER,
IID_IDataInitialize, (void **)&pIDataInitialize);
hr = pIDataInitialize->WriteStringToStorage( L"mydb.udl",
L"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\mydb.mdb;Persist Security Info=False", CREATE_NEW );
CoUninitialize();
I haven't tested this enough yet. How does this work for you? This could be a codeguru article.
Nigel Taylor
April 28th, 1999, 12:06 PM
An excellent suggestion again - thank you. I have already tested the use of a UDL file, but had not gone to the step of creating the thing programatically as you suggest. I think this might be the kludge I need.
It is very difficult to do apparently simple things on this COM platform. The documentation is either absent or incredibly poor and the tools (cf. AdoDC and DataGrid) are truly primitive. Microsoft _do_ write puzzles for a living. The thing is, they don't know the answers either...
mohamed123
August 3rd, 2005, 05:28 AM
but I could not figure out how to form the IUnknown pointer to pass to the data grid SetDataSource method. Thanks very much for clearing that up!
hi
did u get an answer for that
i am facing a similar problem
regards
mohamed
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.