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

Thread: Database Error

  1. #1
    Join Date
    Jun 2005
    Posts
    125

    Question Database Error

    Hi Gurus,
    I am trying to create a data-entry form for a database. All is going out well and program builds without any error. But When I press "Save" button, It is giving me "Invalid Character Value for cast specification on column number 3 (Question)". I am unable to get cause of this error.

    I have a Edit Box whose contents are added to Questions table of the database. I have created CQuestionsRecordset class for it. On dialog box, I have code something like :

    void CDatabaseInterfaceDlg::OnOK()
    {
    // TODO: Add extra validation here
    UpdateData(TRUE);
    CString question = m_EditQuestion;
    question.TrimLeft();
    question.TrimRight();
    m_DBconnection.Connect();
    m_DBconnection.StoreQuestion(question);
    m_DBconnection.Disconnect();
    MessageBox("Test");
    CDialog::OnOK();
    }

    The StoreQuestion function is:

    void CDatabaseConnection::StoreQuestion(CString Question)
    {
    CQuestionsRecordset recset( &m_Database );

    CString sqlString;
    sqlString.Format(_T("SELECT * FROM Questions"));
    // Execute the query
    recset.Open(CRecordset::snapshot,sqlString,CRecordset::none);

    recset.AddNew();
    recset.m_Question = Question;

    recset.Update();
    recset.Close();
    }

    Please help me gurus with this problem.

    Thanks in advance.

    Regards,
    -- Prateek J Duble

  2. #2
    Join Date
    May 2000
    Location
    Scotland, Livingston.
    Posts
    728

    Re: Database Error

    Can you post the code for CQuestionsRecordset.
    What is the definition of m_Question in the recordset and the database?

    Also why do you query for all the records in the table before adding the new one.

    Pass NULL instread of sqlParameter.
    Dave Mclelland.

  3. #3
    Join Date
    Jun 2005
    Posts
    125

    Question Re: Database Error

    Hi,
    Following is the CPP code of CQuestionRecordset.

    // QuestionsRecordset.cpp : implementation file
    //

    #include "stdafx.h"
    #include "DatabaseInterface.h"
    #include "QuestionsRecordset.h"

    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif

    /////////////////////////////////////////////////////////////////////////////
    // CQuestionsRecordset

    IMPLEMENT_DYNAMIC(CQuestionsRecordset, CRecordset)

    CQuestionsRecordset::CQuestionsRecordset(CDatabase* pdb)
    : CRecordset(pdb)
    {
    //{{AFX_FIELD_INIT(CQuestionsRecordset)
    m_AudioFile = "";
    m_LanguageId = 0;
    m_Qid = 0;
    m_Question = "";
    m_nFields = 4;
    //}}AFX_FIELD_INIT
    m_nDefaultType = snapshot;
    }


    CString CQuestionsRecordset::GetDefaultConnect()
    {
    return _T("ODBC;DSN=MS Access Database");
    }

    CString CQuestionsRecordset::GetDefaultSQL()
    {
    return _T("[Questions]");
    }

    void CQuestionsRecordset:oFieldExchange(CFieldExchange* pFX)
    {
    //{{AFX_FIELD_MAP(CQuestionsRecordset)
    pFX->SetFieldType(CFieldExchange:utputColumn);
    RFX_Text(pFX, _T("[AudioFile]"), m_AudioFile);
    RFX_Long(pFX, _T("[LanguageId]"), m_LanguageId);
    RFX_Long(pFX, _T("[Qid]"), m_Qid);
    RFX_Text(pFX, _T("[Question]"), m_Question);
    //}}AFX_FIELD_MAP
    }

    /////////////////////////////////////////////////////////////////////////////
    // CQuestionsRecordset diagnostics

    #ifdef _DEBUG
    void CQuestionsRecordset::AssertValid() const
    {
    CRecordset::AssertValid();
    }

    void CQuestionsRecordset:ump(CDumpContext& dc) const
    {
    CRecordset:ump(dc);
    }
    #endif //_DEBUG

  4. #4
    Join Date
    May 2000
    Location
    Scotland, Livingston.
    Posts
    728

    Re: Database Error

    Check the type of the field in the table definition in the dabase. Is it of type "Text" and of a suitable size?

    if thats not it then

    Check the return value from the call to Open (it should be nonzero).
    Dave Mclelland.

  5. #5
    Join Date
    Jun 2005
    Posts
    1,255

    Re: Database Error

    I don't know if the order of the columns is the same in your program and in the database.

    On one hand, you have:
    Code:
    RFX_Text(pFX, _T("[AudioFile]"), m_AudioFile);
    RFX_Long(pFX, _T("[LanguageId]"), m_LanguageId);
    RFX_Long(pFX, _T("[Qid]"), m_Qid);
    RFX_Text(pFX, _T("[Question]"), m_Question);
    which leads to think that the third field is "Qid" and not "Question".

    But on the other hand, you have your message saying that the problem is caused by the third field whose name is Question.

    Instead of "SELECT * ..." in your query, could you try "SELECT AudioFile, LanguageId, Qid, Question ...", so that you'll be sure that the order is the same as the one you expect.

    (Maybe I'm completely wrong, this is just an hypothese.)

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured