954,164 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Insert to DB (Access)

Hi,

I am trying to insert into Access Database using following code

OleDbConnection empConnection = connection;

                string insertStatement = "INSERT INTO d "
                + "([Employee],[Deduction Type],[Number of Hours],[value],[Month],[Year],[Comment]) "
                + "VALUES (@Employee,@DeductionType,@NumberofHours,@value,@Month,@Year,@Comment)";

                command = new OleDbCommand(insertStatement, empConnection);

                command.Parameters.Add("Employee", OleDbType.Numeric).Value = Convert.ToInt16(txtID.Text);
                command.Parameters.Add("DeductionType", OleDbType.Numeric).Value = Convert.ToInt16(cmbDedNum.Text);
                if (txtNoOfHours.Text == string.Empty) { txtNoOfHours.Text = "1"; }
                command.Parameters.Add("NumberofHours", OleDbType.Numeric).Value = Convert.ToInt16(txtNoOfHours.Text);
                if (txtValue.Text == string.Empty) { txtValue.Text = "0"; }
                command.Parameters.Add("value", OleDbType.Numeric).Value = Convert.ToInt16(txtValue.Text);
                if (cmbMonth.Text == string.Empty) { cmbMonth.Text = ""; }
                command.Parameters.Add("Month", OleDbType.VarChar).Value = cmbMonth.Text;
                if (cmbYear.Text == string.Empty) { cmbYear.Text = ""; }
                command.Parameters.Add("Year", OleDbType.VarChar).Value = cmbYear.Text;
                if (txtComments.Text == string.Empty) { txtComments.Text = ""; }
                command.Parameters.Add("Comment", OleDbType.VarChar).Value = txtComments.Text;

                if (cmbYear.Text == string.Empty || cmbMonth.Text == string.Empty || txtComments.Text == string.Empty)
                    {
                    MessageBox.Show("Complete Year/Month/Comments Fields");
                    return;
                    }

                empConnection.Open();

                try
                    {
                    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

sam1
Posting Whiz
300 posts since Nov 2004
Reputation Points: 10
Solved Threads: 1
 

Dear,

Please check the value of txtID.Text before executing command.

__avd
Posting Genius (adatapost)
Moderator
8,647 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

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

sam1
Posting Whiz
300 posts since Nov 2004
Reputation Points: 10
Solved Threads: 1
 

adatapost>Strange!!!

Are you sure that a txtID name is given to that control. May be mistakenly ..

__avd
Posting Genius (adatapost)
Moderator
8,647 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

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

sam1
Posting Whiz
300 posts since Nov 2004
Reputation Points: 10
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You