Insert to DB (Access)

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2004
Posts: 257
Reputation: sam1 is an unknown quantity at this point 
Solved Threads: 1
sam1's Avatar
sam1 sam1 is offline Offline
Posting Whiz in Training

Insert to DB (Access)

 
0
  #1
May 21st, 2009
Hi,

I am trying to insert into Access Database using following code

  1. OleDbConnection empConnection = connection;
  2.  
  3. string insertStatement = "INSERT INTO d "
  4. + "([Employee],[Deduction Type],[Number of Hours],[value],[Month],[Year],[Comment]) "
  5. + "VALUES (@Employee,@DeductionType,@NumberofHours,@value,@Month,@Year,@Comment)";
  6.  
  7. command = new OleDbCommand(insertStatement, empConnection);
  8.  
  9. command.Parameters.Add("Employee", OleDbType.Numeric).Value = Convert.ToInt16(txtID.Text);
  10. command.Parameters.Add("DeductionType", OleDbType.Numeric).Value = Convert.ToInt16(cmbDedNum.Text);
  11. if (txtNoOfHours.Text == string.Empty) { txtNoOfHours.Text = "1"; }
  12. command.Parameters.Add("NumberofHours", OleDbType.Numeric).Value = Convert.ToInt16(txtNoOfHours.Text);
  13. if (txtValue.Text == string.Empty) { txtValue.Text = "0"; }
  14. command.Parameters.Add("value", OleDbType.Numeric).Value = Convert.ToInt16(txtValue.Text);
  15. if (cmbMonth.Text == string.Empty) { cmbMonth.Text = ""; }
  16. command.Parameters.Add("Month", OleDbType.VarChar).Value = cmbMonth.Text;
  17. if (cmbYear.Text == string.Empty) { cmbYear.Text = ""; }
  18. command.Parameters.Add("Year", OleDbType.VarChar).Value = cmbYear.Text;
  19. if (txtComments.Text == string.Empty) { txtComments.Text = ""; }
  20. command.Parameters.Add("Comment", OleDbType.VarChar).Value = txtComments.Text;
  21.  
  22. if (cmbYear.Text == string.Empty || cmbMonth.Text == string.Empty || txtComments.Text == string.Empty)
  23. {
  24. MessageBox.Show("Complete Year/Month/Comments Fields");
  25. return;
  26. }
  27.  
  28. empConnection.Open();
  29.  
  30. try
  31. {
  32. int count = command.ExecuteNonQuery();

all the columns get inserted apart from employee and employee in access db is numeric and optional. can someone help please.

thanks
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,639
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 472
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Insert to DB (Access)

 
0
  #2
May 22nd, 2009
Dear,

Please check the value of txtID.Text before executing command.
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 257
Reputation: sam1 is an unknown quantity at this point 
Solved Threads: 1
sam1's Avatar
sam1 sam1 is offline Offline
Posting Whiz in Training

Re: Insert to DB (Access)

 
0
  #3
May 22nd, 2009
Originally Posted by adatapost View Post
Dear,

Please check the value of txtID.Text before executing command.
thanks for ur reply. i have checked all they seem to have value strange thing is in access nothing gets written
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,639
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 472
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Insert to DB (Access)

 
0
  #4
May 22nd, 2009
adatapost>Strange!!!

Are you sure that a txtID name is given to that control. May be mistakenly ..
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 257
Reputation: sam1 is an unknown quantity at this point 
Solved Threads: 1
sam1's Avatar
sam1 sam1 is offline Offline
Posting Whiz in Training

Re: Insert to DB (Access)

 
0
  #5
May 22nd, 2009
ah it is fixed now. i didnt create the tables in access and seems there was a select statement for it and i removed it
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
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