|
Hi everyone. I am implementing a project for my dissertation which will manipulate a database (Oracle 9). I have to use (Visual) C++ (industrial project, requirement) for which I am totally new (I have spent many hour for MSDN).I will also use CORBA or DCOM. I have post some of the problems mentioned here in separate threads but the problems persist and the deadline is near (I have two weeks yet). The problems are:
I implement a class which will act as a server. All clients will connect to this class. This class is responsible for manipulating the database. Although most operations work properly (Select, Update and Delete) I cannot add a new records. I use an MFC CRecordset derived class.
EmployeeRecordset for connecting and retrieving data from the database. A sample of this class is:
class EmployeesRecordset : public CRecordset
{
public:
CString m_EPONYMO1;
------------------------------------------------------------------------------------------------------------
Of course there are many fields.
I use a class Employee which will carry data from server to clients. This class has as many variables as the EmployeesRecordset but the data types are those of C++ (in case I use CORBA).
An example of Employee:
class Employee
{
private:
char* m_EPONYMO1;
public
char* getm_EPONYMO1(){return(m_EPONYMO1);}
void setm_EPONYMO1(char* surname1){m_EPONYMO1 = surname1;}
So far I don't use copy constructor and I don't have anything at constructor, destructor
------------------------------------------------------------------------------------------------------------
The ADDNEW method:
bool Service::AddNew(Employee e){
long id;
EmployeesRecordset records(db);
//Begin transaction
db->BeginTrans();
cout << "before opening" << endl;
//open the recordset only for adding records mode
if (db->CanUpdate()){
try{
//"SELECT * FROM [TEIDB].[T_YPALLHLON];"
if ((records.Open(CRecordset::dynaset, NULL,CRecordset::appendOnly))!=0){
cout << "recordset opened" << endl;
//if recordset is updateable add new record
if (records.CanAppend()){
//find the last YP_ID
records.MoveLast();
cout << (LPCSTR)records.m_HMER_PROSL.Format( "%A, %B %d, %Y" ) << endl;
cout << records.m_YP_ID<< endl;
cout << "LAST RECORDCOUNT" << " " << (LPCTSTR)records.m_EPONYMO1<< endl;
id = records.m_YP_ID + 1;
cout << id << endl;
records.AddNew();
records.SetFieldNull( NULL );
// cout << "set new values" << endl;
//copy new values
// CopyToRecordset(records,e);
records.m_ADDR1 = e.getm_ADDR1();
records.m_ADDR2 = e.getm_ADDR2();
records.SetFieldDirty(&records.m_ADDR1, TRUE);
records.SetFieldDirty(&records.m_ADDR2, TRUE);
// cout << "values set" << endl;
// cout << id<< endl;
if (records.Update()){
// cout << "Added" <<endl;
end="" transaction
="" records.close();
="" db-="">CommitTrans();
return (true);
}//end if
}//end if
}//end if
}//end of try
catch (CDBException cdbe){
cout << "exception raised" << endl;
records.Close();
db->Rollback();
return (false);
}//end of catch
catch (CMemoryException cme){
cout << "exception raised" << endl;
records.Close();
db->Rollback();
return (false);
}//end of catch
}//end if
else cout << "data cannot update" << endl;
cout << "end of addnew" << endl;
records.Close();
db->Rollback();
return(false);
}//end of AddNew
------------------------------------------------------------------------------------------------------------
the SELECT method of the Service(server) class is :
this method searches the database for the given employees.
vector<employee> Service::Select(char* surname){
Employee temp_e;
vector<employee> e;
EmployeesRecordset records(db);
CString str;
CString temp = (CString)surname;
cout << "Inside select" << endl;
/* //set filter
records.m_strFilter = "EPONYMO1 = ?";
//bind parameter
records.m_EPONYMO1Param = surname;
cout << LPCTSTR(surname) << endl;
*/
int i =0;
if (!temp.IsEmpty())
str = "SELECT * FROM [TEIDB].[T_YPALLHLON] WHERE EPONYMO1 = '" + (CString)surname + "';";
else str = "SELECT * FROM [TEIDB].[T_YPALLHLON];";
cout << (LPCTSTR)str << endl;
//open the recordset
try{
if (records.Open(CRecordset::dynaset,str,CRecordset::readOnly)!=0){
// test if recordset is empty
if( records.IsBOF( ) ) {
cout << "recordset empty" << endl;
records.Close();
temp_e.setstatus(false);
e.push_back(temp_e);
return (e);
}//end if
//if recorset is not empty copy data to Employees
while ( !records.IsEOF( ) ){
//copy data to employee
// CopyToEmployee(records, temp_e);
cout << (LPCTSTR)records.m_ADDR1 << endl;
temp_e.setm_ADDR1(records.m_ADDR1.GetBuffer(records.m_ADDR1.GetLength()));
temp_e.setm_ADDR2(records.m_ADDR2.GetBuffer(records.m_ADDR2.GetLength()));
temp_e.setm_YP_ID(records.m_YP_ID);
temp_e.setm_EPONYMO1(records.m_EPONYMO1.GetBuffer(records.m_EPONYMO1.GetLength()));
temp_e.setm_ONOMA(records.m_ONOMA.GetBuffer(records.m_ONOMA.GetLength()));
cout << temp_e.getm_ADDR1() << endl;
records.m_ADDR1.ReleaseBuffer(-1);
records.m_EPONYMO1.ReleaseBuffer(-1);
records.m_ONOMA.ReleaseBuffer(-1);
//copy object to vector
e.push_back(temp_e);
cout << e[i].getm_ADDR1() << endl;
i++;
records.MoveNext();
}//end of while
}//end if
}//end of try
catch (CDBException cdbe){
return (e);
records.Close();
}//end of catch
catch (CMemoryException cme){
cout << "exception raised" << endl;
records.Close();
return (false);
}//end of catch
cout << "Exit select" <<endl;
records.close();
=""
="" return(e);
}="" end="" of="" select
select="" and="" addnew="" belong="" to="" the="" object="" service.="" in="" its="" constructor="" service="" accepts="" as="" an="" argument="" a="" cdatabase="" reference,="" which="" uses="" for="" initializing="" crecordset="" derived="" classes.
------------------------------------------------------------------------------------------------------------
this="" is="" how="" i="" call="" these="" methods:="" give="" example="" select="" because="" when="" although="" can="" print="" most="" data="" types="" cannot="" (cout)="" contents="" strings.="" returns="" vector="" employees.
cdatabase="" data;
="" try{
="" if="" (!data.isopen())="" connection="data.OpenEx(_T("DSN=TEIDB;UID=DBADMIN;PWD=DBADMIN"),0);
" cout="" <<="" "connection="" established"="" endl;
="" }="" try
="" catch(="" cdbexception="" cdbe)="" {
="" failed";
="" catch
="" (connection){
="" "service"="" endl;="" service
="" serv(&data),serv1(&data);
="" "constructing="" employee"="" endl;
="" employee="" e;
="" cstring="" el="ELA" ;
="" k="KATI" e.setm_addr1(el.getbuffer(el.getlength()));
="" e.setm_eponymo1(k.getbuffer(k.getlength()));
="" el.releasebuffer(-1);
="" k.releasebuffer(-1);
="" e.getm_addr1()<<="" e.getm_eponymo1()="" "calling="" select"="" vector<employee=""> emp = serv.Select("KATI");
cout << "size of vector " << emp.size() << endl;
// Print contents of the Vector.
cout << "contents of vector" << endl;
for (int size = 0; size < emp.size(); size++)
{
cout << emp[size].getm_EPONYMO1() << " " << emp[size].getm_YP_ID() << endl;
}//end for
e.setm_ADDR2("ELA");
e.setm_EPONYMO1("KATI");
e.setm_ADDR1("FANTASTIKH");
cout << "calling addnew" << endl;
bool add = serv.AddNew(e);
if (add) cout << "added" << endl;
else cout << "not added" << endl;
data.Close();
In the AddNew method I always het a runtime error when calling records.Update();
So the problems are:
1) AddNew doesn’t work. I have made a lot of changes to the OpenEx method but nothing works.
2) I cannot print the string(char*) returned from the vector.
While inside the function Select all strings are printed normally!!!!!!!!!!!!!!!!!!!
3) How can I convert a COleDateTime to CTime. I use the following code (with a number of different formats) but it doesn’t work
COleDateTime oleDt;
cout << "oledt" << endl;
CString str = "8:30:00 Jan. 25, 1996";
oleDt.ParseDateTime(str.Left(15));
cout << "parse date" << endl;
cout << oleDt.Format("%H %M %S") << endl;
SYSTEMTIME st;
cout << "systemtime" << endl;
oleDt.GetAsSystemTime(st);
cout << "getassystemtime" << endl;
CTime myTime(st);
cout << "CTime" << endl;
cout << (LPCTSTR)myTime.Format( "%A, %B %d, %Y" ) << endl;
4) Which values should I use to change the mode of a recordset to pessimisti or optimistic? I use the SetLockingMode(); functions with arguments either (optimistic, pessimistic) or (0,1) but nothing works.
5) Since I have to use a distributed technology which one is more flexible and easier to use (CORBA or DCOM)? I have used before CORBA but with Java. I also have some thoughts about the compatibility between CORBA orbs (I have downloaded omniOrb fro AT&T) and Visual Studio (MFC Classes)?
I apologise for the length of the ‘essay’ but I am trying three days to solve the above problems and I cannot find any solution
|
|
|
|
|
Since this is for a school project, I am hesitant to help, but I will throw you some bones
1) You are using a try/catch around your call to records.AddNew() , but you are not examining the CDBException m_strError or m_strStateNativeOrigin variables. These may give you a new clue as to why your call fails.
2) You are returning a variable, vector e , which is on the local stack.
3) Your problem is in this line: oleDt.ParseDateTime(str.Left(15));
4) How do you know they are not working? You must have two or more connections trying to Edit/Update the same record to see it work.
Roger Stewart
"I Owe, I Owe, it's off to work I go..."
|
|
|
|
|
3) With the exception of not knowing the year, this code works fine, so you'll have to further define "it doesn't work."
|
|
|
|
|
hey everyone thanks for replying.
I solved all the problems except the one that tries to add a new records inm the database, which I will work to. All the other operations (select, delete record, update one) are tested and work properly. I find it very bizzare that the add new is not working because I ve checked the code not only from MSDN but from various e-books and sites and all semm ok.
|
|
|
|
|
thanks all.
I ve solved all the problems except the one of adding a new record.
With regards
Vassilis Papoulidis
|
|
|
|
|
My visual C++ book uses these lines several times:
CRect m_EnclosingRect;<br />
CPoint Start, End;<br />
<br />
m_EnclosingRect = CRect(Start, End);
At first, they seemed simple enough, but after encountering the last line several times:
m_EnclosingRect = CRect(Start, End);
I finally realized I'm not familiar with that notation. The right side of the equals sign is a constructor, so how can it return anything to assign to m_Enclosing Rect? The method I'm familiar with for doing that is this:
CRect aRect(Start, End);<br />
m_EnclosingRect = aRect;
|
|
|
|
|
CRect(Start, End) is an instance of a CRect object, it has just not been assigned to a formal variable like CRect aRect(Start, End) has.
|
|
|
|
|
Have a read up on Copy Constructors. Do a Google search should help out.
Neville Franks, Author of ED for Windows. Free Trial at www.getsoft.com
|
|
|
|
|
Thanks for the replies.
I'm not sure how the copy constructor is involved. My book says:
The copy constructor is called when a class object is created and initialized with an existing object of the same class, or when an object is passed to a function by value.
m_EnclosingRect isn't being created in the line in question.
|
|
|
|
|
It was around 6.40am when I replied and my brain had quite kicked into gear, and probably still hasn't.
David was closer. The constuctor instantiates a CRect and the assignment operator copies it.
Neville Franks, Author of ED for Windows. Free Trial at www.getsoft.com
|
|
|
|
|
Writing CRect(Start, End) calls that CRect ctor to create a new CRect object. This object will get destroyed at the end of the statement.
The = uses the CRect assignment operator (not the copy constructor) to assign that new CRect object into m_EnclosingRect . The implementation of the assignment operator is usually similar/identical to that of the copy ctor, so the end result is that m_EnclosingRect gets a copy of that new CRect object.
--Mike--
Ericahist | Homepage | RightClick-Encrypt | 1ClickPicGrabber
Latest art~!@#2rDFA#@(#*%$Rfa39f3fqwf--=
NO CARRIER
|
|
|
|
|
Thanks for the detailed explanation.
|
|
|
|
|
I have an application that allows users to select "stay on top"...however if another application presents a message box (crital error for example), I'd like to trap that and force that message box into the foreground...does anyone have advice on the best way to implement such a feature?
TIA!
Regards,
Norm
|
|
|
|
|
Interesting. How do you going to determine a normal message box from one that conveys "critical error?"
Kuphryn
|
|
|
|
|
I hope I will not waste your time. I am a beginner and had never tried making my window on top, but I think that you do it by setting the window's style. I have an idea, but I do not know whether it is correct or applicable. If an application presents a message box, then your application will lose focus. May be you could handle WM_KILLFOCUS (or OnKillFocus() ) and test the window getting focus for its style. If the style contains on top (and maybe message box style), put it somehow in front of your app (I think you would need to change your app's Z-order to make it after the message box).
Hope this helps, and sorry for anything wrong I may have said.
Hosam Aly Mahmoud
|
|
|
|
|
As I tried to look it up, I saw that you may be able to handle it as follows:
case WM_KILLFOCUS:
if ( wParam != 0 )
{
WINDOWINFO wi;
GetWindowInfo( (HWND)wParam, &wi );
if ( (wi.dwExStyle & WS_EX_TOPMOST) == WS_EX_TOPMOST )
{
SetWindowPos( (HWND)wParam, HWND_TOP,
0, 0, 0, 0,
SWP_NOMOVE | SWP_NOSIZE |
SWP_NOOWNERZORDER | SWP_NOSENDCHANGING );
}
}
return 0;
Hope this helps (and hope I am not wrong!).
Hosam Aly Mahmoud
|
|
|
|
|
Thanks, just what I needed!
I'll check the wi.dwStyle for WS_POPUP or WS_POPUPWINDOW and if that window's RECT intersects my window will set the appropriate Z-Order for these two windows.
best regards,
norm
|
|
|
|
|
I am happy it worked!
Hosam Aly Mahmoud
|
|
|
|
|
I am trying to figure out this class, but
seems like this CdataGrid is not in MSDN.
I already installed subscription upto 2001,
still no luck. Anyone with the same problem
able to found the solution?
shin
|
|
|
|
|
That's probably because it is not a Microsoft class.
http://www.codeproject.com/useritems/datagrid.asp
|
|
|
|
|
Hi,
Sorry if this is the wrong forum to be asking this
Anyway, I'm going to install Visual Studio .NET 2003. I have Visual Studio .NET (ver. 7) installed already. Should I uninstall it and the framework before installing the newer version?
Thanks..;P
|
|
|
|
|
The answer depends on whether you still want to use version 7. I recommend uninstalling version 7 before installing .NET 2003.
Kuphryn
|
|
|
|
|
Hi,
I’m initializing the dialog in my MFC application from the parent window as follows:
In TreeView.cpp,
void CMyTreeView::OnDraw(CDC* pDC)
{
/**/
CMfcTree dlg;
int ret = dlg.DoModal();
}
In MfcTree.cpp OnInitDialog() is as follows,
BOOL CMfcTree::OnInitDialog()
{
CDialog::OnInitDialog();
m_ImageList.Create(IDB_BITMAP1, 13, 12, RGB(255,255,255));
HTREEITEM hItem = m_nTree.InsertItem( "Root", 0,2,TVI_ROOT );
m_nTree.SelectItem (hItem);
m_nTree.SetImageList(&m_ImageList, TVSIL_NORMAL);
m_nTree.SetItemState(hItem, TVIS_BOLD, TVIS_BOLD );
//sets the background color
m_nTree.SetTextColor(RGB(0, 0, 0));
m_nTree.SetBkColor(RGB(255, 255, 255));
……………………………….
………………………………..
}
If I try to move the dialog I keep getting this error:
Debug assertion failed.
And it points to ASSERT(m_hImageList == NULL); in the winctrl2.cpp.
It has something to do with the line m_ImageList.Create(IDB_BITMAP1, 13, 12, RGB(255,255,255));
Please advise. I’m really frustrated with this error.
Binayak
|
|
|
|
|
hi,
i'm programming an opengl program using visual studio .net. my project uses standard windows library(no mfc or atl). when i try to set a break point on a variable or a function, i keep on getting the error message "intellisense couldn't not find the specific location". does anyone know how to set the break point properly?
also, what file do i need to include in my project in order to use the "TRACE" function? is there a way to redirect printf's output to visual stuio .net's "output" panel/window?
thanx in advance.
|
|
|
|
|
Hello,
I Created a dialog which allows a user to select directories. I now have to code the tree control in which the directories appear. Is there a simple way to do this or do I have to handcode it all?
thanks
A student knows little about a lot.
A professor knows a lot about little.
I know everything about nothing.
|
|
|
|
|