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)