Results 1 to 9 of 9
  1. #1
    Join Date
    Sep 2003
    Posts
    30

    Unanswered: How can i delete a data row in C++ (MFC)

    Hi,

    i like to delete data. I can read and write into the database. But somehow i cant delete data from my Microsoft SQL Server. Always get error on CanUpdate().

    Did i forgto something ???


    CODE::::
    Code:
    void CListAllDlg::OnBnClickedDelete()
    {
    	CDatabase db;
    	CProducts m_pData(&db);
    	CString m_ListItem="";
        int nItem=0;
    	
    	POSITION pos = m_list_all.GetFirstSelectedItemPosition();
    
    	//open Recordset
    	if(m_pData.Open()==0)
    	{
    		AfxMessageBox("Datenbank Verbindung fehlgeschlagen", IDOK);
    		return;
    	}
    	if (MessageBox("Möchten Sie diesen Datensatz wirklich löschen?","Datensatz löschen", MB_YESNO | MB_ICONQUESTION) == IDYES)
    	{
    		do
    		{
    			m_ListItem = m_list_all.GetItemText(nItem,1);
    			//delete data
    			while(!m_pData.IsEOF())
    			{	
    				//found data then delete
    				if(strcmp(m_pData.m_articel,m_ListItem)==0)
    				{
    			
    						m_pData.Delete();			  //delete data in database
    						m_pData.Update();			  //confirm & update database
    						nItem = m_list_all.GetNextSelectedItem(pos);
    						m_list_all.DeleteItem(nItem); //delete data in listbox			
    						MessageBox("Datensatz wurde erfolgreich gelöscht","Datensatz löschen", MB_OK);
    				}
    				m_pData.MoveNext();
    			}//end while			
    		}while (pos != NULL);
    		m_pData.Close();
    	}//end if
    }//end OnBnClickedDelete

  2. #2
    Join Date
    Feb 2004
    Posts
    492
    what should the effect be of the update after deleting the row? I think the delete is enough the delete the record from the database, and the update is not necessary.

  3. #3
    Join Date
    Sep 2003
    Posts
    30
    well..

    iam getting an error messag at the postion of delete. i also have tried with CanUpdate() and also withoug update... then iam getting an error message there. Seems like something is not right.

  4. #4
    Join Date
    Feb 2004
    Posts
    492
    I think it would help if you post more details about the errormessage, such as what is the message and exactly when do you get it? Does the code compile?

  5. #5
    Join Date
    Sep 2003
    Posts
    30
    ERROR MESSAGE -> "DATA CAN BE READ ONLY" that means no write access but i have set m_pData.Open(CRecordset::none). It should allow me to delete datarows. Why do i have only read and write access and no delete access ??


    i used now the main code

    Code::::
    Code:
    void CListAllDlg::OnBnClickedDelete()
    {
    	CDatabase db;
    	CProducts m_pData(&db);
    
    	//open Recordset
    	if(m_pData.Open(CRecordset::none) == 0)
    	{
    		AfxMessageBox("Datenbank Verbindung fehlgeschlagen", IDOK);
    		return;
    	}
    
    	while(!m_pData.IsEOF())
    	{	
     ERROR MESSAGE -> "DATA CAN BE READ ONLY" that means no write access :(
    		m_pData.Delete();				
    		nItem = m_list_all.GetNextSelectedItem(pos);
    	}
    	m_pData.MoveNext();
    	m_pData.Close();
    }//end OnBnClickedDelete
    Last edited by Indian; 06-22-04 at 09:00.

  6. #6
    Join Date
    Feb 2004
    Posts
    492
    what are your rights on the db/table? does the program run under a different user? are you able to execute a delete on the databasetable at all? did you try?

  7. #7
    Join Date
    Sep 2003
    Posts
    30
    i tried but the delete query isnt allowed and the dbo user has all rights. select, update, delete, insert and dri...
    if i run my application i can select and insert without any problem. i dont know why i cant delete ...

    what should i set up ??? how can i set up an account so i can access to the ms sql server with login and password.

    i created a role in sql server called login:mother with password:hello

    i try to login like this


    Code:
    CString CProducts::GetDefaultConnect()
    {
    	return _T("DSN=MOTHER;UID=mother;PWD=hello;APP=Microsoft\x00ae Visual Studio .NET;WSID=MOTHER;DATABASE=mother;Trusted_Connection=No");
    }
    but i cant login. getting message login failed for mother
    Last edited by Indian; 06-23-04 at 06:40.

  8. #8
    Join Date
    Sep 2003
    Posts
    30
    For all those who dont know the solution.

    Lösung:
    One Column musst be unique or Primary Key.



    Indian

  9. #9
    Join Date
    Jul 2003
    Location
    San Antonio, TX
    Posts
    3,662
    Yeah, and that's ONLY because you're using scrollable cursor and technology that is an offshoot of DAO! Switch to something a little more serious and reliable than that!
    "The data in a record depends on the Key to the record, the Whole Key, and
    nothing but the Key, so help me Codd."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •