943,633 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 1872
  • C# RSS
May 21st, 2009
0

Insert to DB (Access)

Expand Post »
Hi,

I am trying to insert into Access Database using following code

C# Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 1
Posting Whiz
sam1 is offline Offline
300 posts
since Nov 2004
May 22nd, 2009
0

Re: Insert to DB (Access)

Dear,

Please check the value of txtID.Text before executing command.
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
May 22nd, 2009
0

Re: Insert to DB (Access)

Click to Expand / Collapse  Quote originally posted by adatapost ...
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
Reputation Points: 10
Solved Threads: 1
Posting Whiz
sam1 is offline Offline
300 posts
since Nov 2004
May 22nd, 2009
0

Re: Insert to DB (Access)

adatapost>Strange!!!

Are you sure that a txtID name is given to that control. May be mistakenly ..
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
May 22nd, 2009
0

Re: Insert to DB (Access)

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
Reputation Points: 10
Solved Threads: 1
Posting Whiz
sam1 is offline Offline
300 posts
since Nov 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: How Insert Data Using EntityDataSource in FormView
Next Thread in C# Forum Timeline: string manipulations





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC