locked
New to programming RRS feed

  • Question

  • User-431499947 posted

    Hi everyone.  This is my first experience creating a MVC type application in C# with an ASP UI and I'm having trouble with the data access.  The strangest part is that I wasn't having any trouble and for what seems like no reason, my insert statements have stopped working.  I'm not sure what code will be helpful so if you would like more on the user controls or controller methods, just ask.  I'm not looking for criticism or critique, just need someone else to look for blantant syntax errors I could be missing.  I assure the file path is correct to the database.  If someone wants me to send them the whole VisualStudio solution I can do that too.  Here's the part of the code throwing exceptions.

    Thanks for anyone willing to attempt a solution here. I have learned a lot from these forums and this is my first post.

            public string InsertNewEmployee(Employee vEmpl)
            {                        
                bool isInserted = false;
                OleDbConnection curConn = new OleDbConnection();            
                curConn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\New\\Enrollments.accdb";
                string sSQL = "INSERT INTO Employees (Empl_FName, Empl_LName, Empl_Addr1, Empl_Addr2, Empl_City, Empl_State, Empl_Zip, Empl_Phone, Empl_PlanId, Empl_DeptId) VALUES ('" 
                    + vEmpl.Empl_FName + "','" + vEmpl.Empl_LName + "','" 
                    + vEmpl.Empl_Addr1 + "', '" + vEmpl.Empl_Addr2 + "','" 
                    + vEmpl.Empl_City + "','" + vEmpl.Empl_State + "','" 
                    + vEmpl.Empl_Zip + "'," + Convert.ToInt32(vEmpl.Empl_Phone) + "," 
                    + Convert.ToInt32(vEmpl.Empl_PlanId) + "," + Convert.ToInt32(vEmpl.Empl_DeptId) + ")";            
    
            OleDbCommand myCom = new OleDbCommand(sSQL, curConn);
                
            try 
                {
                curConn.Open();
                myCom.ExecuteScalar();            
                sSQL = string.Empty;
                sSQL = "SELECT EmpID FROM Employees WHERE Empl_FName = '" + vEmpl.Empl_FName + "' AND Empl_LName = '" + vEmpl.Empl_LName + "'";
                myCom = new OleDbCommand(sSQL, curConn);
                OleDbDataReader reader = myCom.ExecuteReader();
    
                while (reader.Read())
                {
                    vEmpl.Empl_Id = String.Format("{0}", reader[0]);
                }
                
                curConn.Close();
                isInserted = true;
                }
            catch(Exception e) 
                {
                isInserted = false;
                }
            finally
                {
                curConn.Close();
                }
    
            return vEmpl.Empl_Id;
            }
    Thursday, June 16, 2011 11:34 AM

Answers

  • User-1166080021 posted

    When you click save/enroll and load your information first you need to refresh, and then you need to refresh the table too! because you are loading the page when you run the app but is better that if you load your table employee List when you click the button so the last employee loaded will be in the table.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 16, 2011 2:52 PM
  • User-821857111 posted

    You will get a lot more help if you posted details of the exception rather than emailing your code off to random people. In the meantime, you should change that SQL to use parameters: Parameter Queries in ASP.NET with MS Access

    That may solve your problem.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 16, 2011 5:03 PM

All replies

  • User-1166080021 posted

    hi friend,

    Tell me more details about your project, or send me the solution!

    I'll help you!

    Thursday, June 16, 2011 1:19 PM
  • User-431499947 posted

    Just emailed you the solution with database included.  Thank you so much for your reply and attempt to help.

    Thursday, June 16, 2011 1:52 PM
  • User-1166080021 posted

    Friend,

    the problem is that you need to stop runnig and then start again and appear the employees that you are inserting. So We need to check the select statement to the access.

     

    Thursday, June 16, 2011 2:41 PM
  • User-1166080021 posted

    When you click save/enroll and load your information first you need to refresh, and then you need to refresh the table too! because you are loading the page when you run the app but is better that if you load your table employee List when you click the button so the last employee loaded will be in the table.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 16, 2011 2:52 PM
  • User-821857111 posted

    You will get a lot more help if you posted details of the exception rather than emailing your code off to random people. In the meantime, you should change that SQL to use parameters: Parameter Queries in ASP.NET with MS Access

    That may solve your problem.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 16, 2011 5:03 PM