hi, now I wants to insert some records into table in Microsoft Access and C#, below is my codes :

SQLStr = "Insert Into RegisterEmployee(EmployeeName,Password,Gender,ContactNumber,Position) " +
                        "Values ('"  + 
                        SQLStrCmd.ReplaceStr(nameReg.Text) + "', '" +
                        SQLStrCmd.ReplaceStr(passReg.Text) + "', '" +
                        SQLStrCmd.ReplaceStr(genReg.Text) + "', '" +
                        SQLStrCmd.ReplaceStr(Hptxt.Text) + "', '" +
                        SQLStrCmd.ReplaceStr(posReg.Text) + "')";
                              
                if (AccessDb_Cmd.ExecuteNonQuery(SQLStr, Access_Db))
                {
                   // Do event                
                }

My table has attributes of ID (auto number),EmployeeName(text),Password(text),Gender(text),ContactNumber(Number) and Position(text).
When I compile the program, it detected that my sql query has exception, but i cant found out the error in my sql query as above, anyone please help me.

What exception you encounter?

What exception you encounter?

Inside the codes above, I am using try catch, as below:

private void btnDone_Click(object sender, EventArgs e)
        {                    
            try
            {               
                Access_Db.OpenTransaction();              
                Access_Db.BeginTransaction();
               
                SQLStr = "Insert Into RegisterEmployee(EmployeeName,Password,Gender,ContactNumber,Position) " +
                        "Values ('" + 
                        SQLStrCmd.ReplaceStr(nameReg.Text) + "', '" +
                        SQLStrCmd.ReplaceStr(passReg.Text) + "', '" +
                        SQLStrCmd.ReplaceStr(genReg .Text) + "', '" +
                        SQLStrCmd.ReplaceStr(Hptxt.Text) + "' , '" +
                        SQLStrCmd.ReplaceStr(posReg.Text) + "')";

                if (AccessDb_Cmd.ExecuteNonQuery(SQLStr, Access_Db))
                {                                     
                    // do event
                }
                    
                else
                {
                   // do event
                }
               
            }
            catch (Exception ex)
            {                                           
                Access_Db.RollBackTransaction();               
                throw new Exception("WRONG");              
            }
            finally
            {
                Access_Db.CloseTransaction();
            }
        }

In the program above, it will give the errow message "WRONG" as inside the catch block, that is what I means.

write this line in ur catch block

MessageBox.Show(ex.Message + " : Exception raised in " + ex.TargetSite.ToString() , ex.Source.ToString(), MessageBoxButtons.OK, MessageBoxIcon.Error);

then tell the output

write this line in ur catch block

MessageBox.Show(ex.Message + " : Exception raised in " + ex.TargetSite.ToString() , ex.Source.ToString(), MessageBoxButtons.OK, MessageBoxIcon.Error);

then tell the output

I had tried, and it prompted the message" Syntax error in INSERT INTO statement.:Exception raised in Boolean ExecuteNonQuery(System.String,Attendance_System.AccessDB)", is it means sql error?

Yes you query is not right

try this code

SQLStr = "Insert Into RegisterEmployee(EmployeeName,Password,Gender,ContactNumber,Position) " +
                        "Values ('@nameReg.Text','@passReg.Text', '@genReg .Text', '@Hptxt.Text' , '@posReg.Text')";

or

SQLStr = "Insert Into RegisterEmployee(EmployeeName,Password,Gender,ContactNumber,Position) " +
                        "Values ('"+nameReg.Text+"','"+passReg.Text+"', '"+genReg .Text+"', '"+Hptxt.Text+"' ,'"+posReg.Text+"')";

Thank you :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.