Professional Web Applications Themes

Insert method - Microsoft SQL / MS SQL Server

I am trying to insert data from a windows form in C++ to my MS SQL database. I have only found how to use the insert function when the values are constants. INSERT INTO mytable(id,password)VALUES('78','work'). When I use variable names I get the error, cannot use column names. But these variable are not column names they are values I created to hold the data from the edit control box. Your help is greatly appreciated....

Sponsored Links
  1. #1

    Default Insert method

    I am trying to insert data from a windows form in C++ to my MS SQL
    database.
    I have only found how to use the insert function when the values are
    constants.
    INSERT INTO mytable(id,password)VALUES('78','work').

    When I use variable names I get the error, cannot use column names. But
    these variable are not column names they are values I created to hold the
    data from the edit control box.

    Your help is greatly appreciated.



    Sponsored Links
    meipv Guest

  2. #2

    Default Re: Insert method

    > When I use variable names I get the error, cannot use column names.

    Maybe you can show the code, but remember to use single quotes around char /
    date variables while resolving the statement you are sending to the
    database...


    Aaron Guest

  3. #3

    Default Re: Insert method

    you must be missing putting single quotes around the column values.
    Ex:
    create table test2(i int, val varchar(50))
    following insert statement will give you an error like '.... Column names
    are not permitted'

    insert into test values(1,a)

    The correct statement would be

    insert into test2 values(1,'a')

    --
    -Vishal

    "meipv" <net> wrote in message
    news:##phx.gbl... 


    Vishal Guest

  4. #4

    Default Insert method

    You need to concatinate and pass the resultant string to
    the backend .

    'INSERT INTO mytable(id,password) VALUES (''' + varid
    + ''',''' + varpassword + ''')'

    Pls replace varid and varpassword with your front end
    variables.

    HTH
    Srinivas Sampangi
     
    my MS SQL 
    values are 
    column names. But 
    created to hold the 
    sampangi Guest

  5. #5

    Default Re: Insert method

    This is some of the code that is giving me a problem.

    void Enrollment::DoDataExchange(CDataExchange* pDX)

    {


    CDialog::DoDataExchange(pDX);

    DDX_Text(pDX,IDC_EDIT1,m_newmember);

    DDX_Text(pDX, IDC_EDIT2,m_newpassword);

    }



    // Enrollment message handlers

    void Enrollment::OnBnClickedOk()

    {

    // When the user selects okay, the data will be stored in the database

    UpdateData(TRUE);

    m_newname="Jean Paul";


    try

    {

    CDatabase dataSpeech;

    CString strCmd="INSERT INTO sampleSpeech VALUES('''+ m_newmember +''','''
    +m_newname +''','''+m_newpassword+''')";

    if(dataSpeech.Open("SpeechVerify"))

    {

    dataSpeech.ExecuteSQL(strCmd);

    dataSpeech.Close();

    }

    }


    meipv Guest

  6. #6

    Default Re: Insert method

    > dataSpeech.ExecuteSQL(strCmd);

    Instead of this, how about printing strCmd to console or stdout and showing
    us how it resolves.


    Aaron Guest

  7. #7

    Default Re: Insert method


    Try this..

    strCmd = "INSERT INTO sampleSpeech VALUES('" + m_newmember
    + "','" + m_newname + "','" + m_newpassword + "') "

    HTH
    Srinivas Sampangi 
    the database 
    m_newmember +''',''' 
    sampangi Guest

  8. #8

    Default Re: Insert method

    Thanks for your help. It works now.



    meipv Guest

Similar Threads

  1. Best Method to insert a Date into a MySQL database?
    By DigiWolf in forum Adobe Dreamweaver & Contribute
    Replies: 7
    Last Post: December 7th, 03:51 AM
  2. Question Insert from ASP to SQL using recordset data as values in insert statement
    By JasonM in forum Microsoft SQL / MS SQL Server
    Replies: 0
    Last Post: June 13th, 05:54 PM
  3. Replies: 2
    Last Post: April 21st, 12:26 PM
  4. Replies: 2
    Last Post: March 15th, 09:59 AM
  5. Replies: 3
    Last Post: September 30th, 09:24 PM

Bookmarks

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139