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

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

adatapost>Strange!!!

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

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

This article has been dead for over six months. Start a new discussion instead.