Closed Thread
Results 1 to 6 of 6

Thread: MS Access Problem

  1. #1
    encoder is offline Learning Programmer
    Join Date
    Apr 2006
    Posts
    40
    Rep Power
    0

    MS Access Problem

    I got a friend who got a problem in MS Access. Hope someone could help answering this question then I would tell him how to do it.

    How can you put a value into a variable in the database? He wants to put the value inserted in the textbox and then store it in the database.

    Here's his code, is this correct?

    insert into tablename values (txtbox.text)

    But this didn't work.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    NeedHelp Guest
    That should work just fine. What programming language is he using?

    For PHP it would look like this

    Code:
    $query = "INSERT INTO tablename VALUES ('$variable')";
    $results = mysql_query($query);

  4. #3
    dirkfirst is offline Programming Expert
    Join Date
    May 2006
    Posts
    354
    Rep Power
    23
    What error does he receive? That should work just fine as a query.

  5. #4
    NeedHelp Guest
    That is his SQL query - what is his actual C# Code?????

  6. #5
    NeedHelp Guest
    Here is C# code for INSERT:
    Code:
    string sqlIns = "INSERT INTO table (name, information, other) VALUES (@name, @information, @other)";
    
    db.Open();
    try
    {
         SqlCommand cmdIns = new SqlCommand(sqlIns, db.Connection);
         cmdIns.Parameters.Add("@name", info);
         cmdIns.Parameters.Add("@information", info1);
         cmdIns.Parameters.Add("@other", info2);
         cmdIns.ExecuteNonQuery();
         cmdIns.Dispose();
         cmdIns = null;
    }
    catch(Exception ex)
    {
         throw new Exception(ex.ToString(), ex);
    }
    finally
    {
         db.Close();
    }

  7. #6
    brackett is offline Programmer
    Join Date
    May 2006
    Posts
    192
    Rep Power
    22
    Looks like you're using the SqlClient libraries, but you said you're inserting into Access? You need to use the OLEDB libraries (OleDbCommand, OleDbConnection, etc.) for that.

    As an aside, there's no reason to throw a new exception if you're not changing anything - all you're doing is screwing up the stack trace making debugging a lot harder (at least you kept the innerException though). Since you're also throwing a generic Exception, it'll be difficult to catch further up the stack. You should either (a) get rid of the catch (it's not required to have a catch in a try statement) which will let the actual exception bubble up the stack, (b) derive your own exception class and wrap the exception in it, or (c) log it and just put throw; which will just let the exception bubble up without clobbering the stack trace.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. MS Access connection problem with VB.NET
    By bradploz in forum Visual Basic Programming
    Replies: 0
    Last Post: 07-29-2011, 03:50 AM
  2. sending mail - problem with access!
    By AdrianWierciochPHP in forum PHP Development
    Replies: 1
    Last Post: 01-18-2011, 11:11 AM
  3. problem connecting to Access Database
    By system32 in forum PHP Development
    Replies: 0
    Last Post: 04-10-2010, 02:39 PM
  4. Problem Using JSP with Microsoft Access db
    By telemachus in forum Java Help
    Replies: 2
    Last Post: 11-26-2009, 07:45 PM
  5. connect MS access DB oleDB problem
    By scottmcg in forum C# Programming
    Replies: 0
    Last Post: 01-12-2009, 11:41 PM

Tags for this Thread

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