dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 16

Thread: Connect database when start with Windows

Hybrid View

  1. #1
    Join Date
    May 2008
    Posts
    18

    Connect database when start with Windows

    I use ADO to connect to database. I connect successfully. But when I run my application with Windows there's an error :
    Debug error :
    This application has requested the Runtime to terminate it in an unusual way
    Some one plz explain for me & give me a solution

    Here is a part of my code:

    iSize = 0;
    DateTimeNote Temp;
    bool bFlag = true;
    Open("Provider=Microsoft.Jet.OLEDB.4.0; \
    Data Source=DateNote.mdb");
    CString sql;
    variant_t vNull;
    variant_t TheValue;

    vNull.vt = VT_ERROR;
    vNull.scode = DISP_E_PARAMNOTFOUND;
    CString StrResult;
    CString StrSql;
    CString StrDateTime;
    CString StrDateTimeTemp;

    StrDateTime.Format(_T("%i/%i/%i"),DateTime.GetMonth(),DateTime.GetDay(),DateTime.GetYear());
    StrSql.Format(_T("SELECT * FROM DATETIMENOTE WHERE NGAY = '%s'"), StrDateTime);

    m_pRecordset.CreateInstance(__uuidof(Recordset));
    m_pRecordset->PutRefActiveConnection(m_pConnection);
    m_pRecordset->Open(_variant_t(StrSql), vNull, adOpenForwardOnly, adLockOptimistic, adCmdText);

    while (!m_pRecordset->GetEndOfFile())
    {
    TheValue = m_pRecordset->GetCollect("GIO");
    if(TheValue.vt != VT_NULL)
    {
    StrResult = (char *)_bstr_t(TheValue);
    StrDateTimeTemp = StrDateTime + _T(" ") + StrResult;
    Temp.DateTime.ParseDateTime(StrDateTimeTemp, LOCALE_NOUSEROVERRIDE, LANG_USER_DEFAULT);
    }
    TheValue = m_pRecordset->GetCollect("NOTE");
    if(TheValue.vt != VT_NULL)
    {
    StrResult = (char *)_bstr_t(TheValue);
    Temp.Note = StrResult;
    }
    pTemp[iSize] = Temp;
    iSize ++;
    m_pRecordset->MoveNext();
    }

  2. #2
    Join Date
    Aug 2002
    Location
    Cluj-Napoca,Romania
    Posts
    3,496

    Re: Connect database when start with Windows

    Most likely you are using a variable that is not initialized properly or NULL, or an exception was thrown from your code.

    Code like m_pRecordset.CreateInstance(__uuidof(Recordset)); should be checked if it executed successfully and if it is possible to throw exceptions, it should be inside a try/catch block.

    The best way to see what happens is to debug your program line by line and see where it crashes.
    Har Har

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,172

    Re: Connect database when start with Windows

    Be careful about your usage of strings. You have "", _T(""), and _bstr_t in your code.

    Make sure that you are using the correct string type for what you need.

  4. #4
    Join Date
    May 2008
    Posts
    18

    Re: Connect database when start with Windows

    Let me explain : My code runs properly. When I debug as well as when I run the application file. It just doesn't work well when running with Windows. I don't know why b

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,172

    Re: Connect database when start with Windows

    Quote Originally Posted by capint
    Let me explain : My code runs properly. When I debug as well as when I run the application file. It just doesn't work well when running with Windows. I don't know why b
    What do you mean, "doesn't work well when running with Windows"?

    Do you mean during Windows startup?

  6. #6
    Join Date
    May 2008
    Posts
    18

    Re: Connect database when start with Windows

    Yes. I put my application in starup and it doesn't work. When I remove connecting database. It runs well with Windows. Of course, I put the file Access at the same directory
    Last edited by capint; May 20th, 2008 at 10:39 AM.

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,172

    Re: Connect database when start with Windows

    I suggest you add some error handling to your application and log the errors to a log file.

  8. #8
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,172

    Re: Connect database when start with Windows

    The start up folder of your application when run under startup may be different than what you think and as previously suggested, your app may not be able to find the .mdb file.

    I suggest you add some error handling to your application and log the errors to a log file. Or you could catch the exception and temporarily display a MessageBox with the error.

  9. #9
    Join Date
    Jan 2002
    Location
    Houston, TX
    Posts
    1,421

    Re: Connect database when start with Windows

    I'm not sure what you mean by "when runs with Windows".

    Perhaps you mean when you start the application from the Windows Explorer? If so, then your problem may be the default directory.

    When you run your application in the IDE (debugger) the default directory (where it's looking for your DateNote.mdb file is the directory containing the .sln and .vcproj files.

    However when you run it from Windows Explorer, the default directory is the one containing your .exe (Debug or Release probably). The .mdb file would need to be in that directory.

    Hope that helps.
    Be sure to rate those who help!
    -------------------------------------------------------------
    Karl - WK5M
    PP-ASEL-IA (N43CS)
    PGP Key: 0xDB02E193
    PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)