-1

I am new to C# . I am trying to create an simple application using ADO.Net in which I just want to insert and view the employee details. I am not able to insert and view the details of the employees. The method which I want to call is add_employee() which exist in EmployeeDAL class.

namespace Employee
{
    class EmployeeDAL
    { 
        public void add_employee(EmployeeBO emp)
        {
            string connectionString = "Data Source=SQLEXPRESS01;Initial Catalog=abc";
            SqlConnection con = new SqlConnection(connectionString);
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "sp_insert";
            command.Connection = con;
            command.Parameters.AddWithValue("@empname", emp.EmployeeName);
            command.Parameters.AddWithValue("@deptid", emp.DepatNumber);
            con.Open();
            int result = command.ExecuteNonQuery();
            if (result > 0)
                Console.WriteLine("Record inserted successfully");
            else
                Console.WriteLine("Some Error Occured");
            con.Close();
           //return result;
        }
    }
}

My main method is like this

    using System;
using System.Collections.Generic;



namespace Employee
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Employee Management system");
            Console.WriteLine("Menu\n");
            Console.WriteLine("1.Add Employees\n");
            Console.WriteLine("2.View Employees\n");
            Console.WriteLine("Enter your choice\n");
            int ch = Convert.ToInt32(Console.ReadLine());
            EmployeeBO emp = new EmployeeBO();
            EmployeeDAL dal = new EmployeeDAL();

            switch(ch)
            {
                case 1:

                    Console.WriteLine("Enter the name of the employee");
                    string name = Console.ReadLine().ToString();
                    emp.EmployeeName = name;
                    Console.WriteLine("Enter the department of the employee");
                    int deptNo = Convert.ToInt32(Console.ReadLine());
                    emp.DepatNumber = deptNo;

                     dal.add_employee(emp);
                    break;

I am not able to insert and view the already present data.

My EmployeeBO class consists of only the Employee class .

My sp_insert stored procedure is

create proc sp_insert(
@empname varchar(20),
@deptid int
)
as begin
insert into employee(empName,deptno) values(@empname,@deptid)
end

This is how I created my tables

create table employee( empno int identity(1000,1) primary key , empName varchar(20)) 
alter table employee add deptno int 

create table department(deptno int identity(1,1) primary key, deptName varchar(20))


alter table employee add constraint fk1 foreign key(deptno) references department(deptno) 
  • class EmployeeDAL should be public. What is the error message? – Steve Nov 17 '19 at 10:34
  • 1
    Did you try to debug this code? Are you sure that the variable _ch_ holds the value 1 when you enter the last code above? Did you see the input requests for name and deptNo? – Steve Nov 17 '19 at 10:42
  • My employee name is "uma" and department id is 2. My department table contains entries for 1,2 and 3 department id. So foreign key violation is not possible – Seeker1201 Nov 17 '19 at 11:31
  • con.Open(); is the last line hit by the debugger – Seeker1201 Nov 17 '19 at 11:46
  • `string connectionString = "Data Source=SQLEXPRESS01;Initial Catalog=abc";` looks like it may be incomplete. https://www.connectionstrings.com/sql-server/ When you run the code, an exception will be thrown on `con.Open`. What does the exception say? – mjwills Nov 17 '19 at 11:46
  • I am not getting any exception....when i run the app in console, I simple enter 1 for the option 1 , enter uma for employee name and 2 for department id ..that's it...nothing happens after this – Seeker1201 Nov 17 '19 at 11:56
  • You tried `localhost` like shown at https://stackoverflow.com/a/5283762/34092 ? – mjwills Nov 17 '19 at 11:59
  • That might look something like `Server=localhost\SQLEXPRESS01;Database=abc;Trusted_Connection=True;` or `Server=localhost\\SQLEXPRESS01;Database=abc;Trusted_Connection=True;` – mjwills Nov 17 '19 at 12:11

1 Answers1

-1

As @mjwills, I think this is a case of running the code that has not been compiled.

This could happen, for example, if you Run the code (F5) but there are compilation errors and you choose to run the last version that compiled. (And there are other circumstances that could lead to running not the code that's shown in the editor).

One solution is to add a change that allows you to confirm that you're running fresh code, recompile and run again.

You could also put something in the code that does NOT compile and confirm that the Run/Debug failed.

tymtam
  • 31,798
  • 8
  • 86
  • 126