Error in updating database.. help needed

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2009
Posts: 57
Reputation: facadie has a little shameless behaviour in the past 
Solved Threads: 0
facadie facadie is offline Offline
Junior Poster in Training

Error in updating database.. help needed

 
0
  #1
Sep 29th, 2009
  1. private void writeToDB(string password)///
  2. {
  3.  
  4. string cmdstr = "INSERT INTO " + TableName + " (Password) values (@Password)" ;
  5.  
  6. OleDbDataAdapter adapter = new OleDbDataAdapter();
  7.  
  8. OleDbCommand insert = new OleDbCommand(cmdstr, vlAccessConnection);
  9.  
  10. adapter.SelectCommand = insert;
  11.  
  12. insert.Parameters.AddWithValue("@Password", password);
  13. }

Hi all this is the code i've insert to update data with
password = tbconfirmp.text; [under save();]

but it seems like there is an error insert.ExecuteNonQuery();
state
Syntax error in INSERT INTO statement.

I'm struck=(
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,286
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 586
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Error in updating database.. help needed

 
0
  #2
Sep 29th, 2009
Don't use a data adapter for this. Here is the code using Sql* data accessors but you can flip them over to OleDb*, they're more or less identical in calling:
  1. public static string BuildSqlNativeConnStr(string server, string database)
  2. {
  3. return string.Format("Data Source={0};Initial Catalog={1};Integrated Security=True;", server, database);
  4. }
  5. private void simpleButton1_Click(object sender, EventArgs e)
  6. {
  7. const string query = "Insert Into Employees (RepNumber, HireDate) Values (@RepNumber, @HireDate)";
  8. string connStr = BuildSqlNativeConnStr("apex2006sql", "Leather");
  9.  
  10. try
  11. {
  12. using (SqlConnection conn = new SqlConnection(connStr))
  13. {
  14. conn.Open();
  15. using (SqlCommand cmd = new SqlCommand(query, conn))
  16. {
  17. cmd.Parameters.Add(new SqlParameter("@RepNumber", 50));
  18. cmd.Parameters.Add(new SqlParameter("@HireDate", DateTime.Today));
  19. cmd.ExecuteNonQuery();
  20. }
  21. }
  22. }
  23. catch (SqlException)
  24. {
  25. System.Diagnostics.Debugger.Break();
  26. }
  27. }
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 57
Reputation: facadie has a little shameless behaviour in the past 
Solved Threads: 0
facadie facadie is offline Offline
Junior Poster in Training

Re: Error in updating database.. help needed

 
0
  #3
Sep 29th, 2009
Originally Posted by sknake View Post
Don't use a data adapter for this. Here is the code using Sql* data accessors but you can flip them over to OleDb*, they're more or less identical in calling:
  1. public static string BuildSqlNativeConnStr(string server, string database)
  2. {
  3. return string.Format("Data Source={0};Initial Catalog={1};Integrated Security=True;", server, database);
  4. }
  5. private void simpleButton1_Click(object sender, EventArgs e)
  6. {
  7. const string query = "Insert Into Employees (RepNumber, HireDate) Values (@RepNumber, @HireDate)";
  8. string connStr = BuildSqlNativeConnStr("apex2006sql", "Leather");
  9.  
  10. try
  11. {
  12. using (SqlConnection conn = new SqlConnection(connStr))
  13. {
  14. conn.Open();
  15. using (SqlCommand cmd = new SqlCommand(query, conn))
  16. {
  17. cmd.Parameters.Add(new SqlParameter("@RepNumber", 50));
  18. cmd.Parameters.Add(new SqlParameter("@HireDate", DateTime.Today));
  19. cmd.ExecuteNonQuery();
  20. }
  21. }
  22. }
  23. catch (SqlException)
  24. {
  25. System.Diagnostics.Debugger.Break();
  26. }
  27. }
meaning that i can change the sql to oledb and use?
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,286
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 586
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Error in updating database.. help needed

 
0
  #4
Sep 29th, 2009
Yes
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 57
Reputation: facadie has a little shameless behaviour in the past 
Solved Threads: 0
facadie facadie is offline Offline
Junior Poster in Training

Re: Error in updating database.. help needed

 
0
  #5
Sep 29th, 2009
Originally Posted by sknake View Post
Yes
but i'm also using
  1.  
  2. private const string TableName = "AdminPassword";///
  3. public const string DBFileName = "AdminPassword.mdb";///
  4. public OleDbConnection vlAccessConnection;///
  5. private string password;///
  6. vlAccessConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + DBFileName);///
  7.  
  8. try///
  9. {
  10. vlAccessConnection.Open();
  11. }
  12. catch (OleDbException ee)
  13. {
  14. MessageBox.Show(ee.Message + ". StackTrace: " + ee.StackTrace.ToString());
  15. }///
i can see the code work the same way but how do they link back to data base file?=.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 57
Reputation: facadie has a little shameless behaviour in the past 
Solved Threads: 0
facadie facadie is offline Offline
Junior Poster in Training

Re: Error in updating database.. help needed

 
0
  #6
Sep 30th, 2009
Originally Posted by sknake View Post
Yes
tried to edit to oledb even more error=/ and getting myself confuse=( but thank=)
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,286
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 586
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Error in updating database.. help needed

 
0
  #7
Sep 30th, 2009
Here is an example of inserting into an excel spreadsheet:
  1. private static void WriteExcelFile()
  2. {
  3. string connStr = BuildExcel2007ConnectionString(@"C:\Data\Spreadsheet.xlsx", true);
  4. //Note 'rowNumber' is the first column in my spreadsheet.
  5. string query = @"Insert Into [Sheet1$] ([Row], [Name]) Values (?,?);";
  6. using (System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(connStr))
  7. {
  8. conn.Open();
  9. using (System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand(query, conn))
  10. {
  11. cmd.Parameters.Add(new System.Data.OleDb.OleDbParameter("Row", Convert.ToDouble(5)));
  12. cmd.Parameters.Add(new System.Data.OleDb.OleDbParameter("Name", "Fred"));
  13. cmd.ExecuteNonQuery();
  14. }
  15. }
  16. }
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC