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.
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);
What error does he receive? That should work just fine as a query.
DirkFirst Tutorials | Linux Forum
That is his SQL query - what is his actual C# Code?????
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(); }
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.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks