Here i written the code for insert data into database table.but here my intention is to display the inserted record when user enters the id in respective textbox and other details of the employee(Name,Department Id,Location,Salary)should be displayed on the respective controls here textboxes. what is the code for finding the record whether it is available or not and what is the should i writhe in the business logic layer,data access layer and code behind.
please help me. thanks in advance.
code in dal:
public class clsdal
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
public int insertdetails(int Id, string Name, int DepartmentId, string Location, int Salary)
{
try
{
con.Open();
SqlCommand cmd = new SqlCommand("InsertDetails", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Id", Id);
cmd.Parameters.AddWithValue("@Name", Name);
cmd.Parameters.AddWithValue("@DepartmentId", DepartmentId);
cmd.Parameters.AddWithValue("@Location", Location);
cmd.Parameters.AddWithValue("@Salary", Salary);
cmd.CommandType = CommandType.StoredProcedure;
cmd.ExecuteNonQuery();
return cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
}
}
public int checkExistance(int Id)
{
con.Open();
SqlCommand cmd = new SqlCommand("Select * from tblEmployee where Id=" + Id + "", con);
cmd.CommandType = CommandType.Text;
SqlDataReader dr;
try
{
dr = cmd.ExecuteReader();
if (dr.Read())
{
return 1;
}
else
return 0;
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
con.Dispose();
}
}
code in bal:
public class clsbll
{
clsdal objdal = new clsdal();
public int insertrecord(int Id,string Name,int DepartmentId,string Location,int Salary)
{
try
{
return objdal.insertdetails(Id,Name,DepartmentId,Location,Salary);
}
catch
{
throw;
}
finally
{
objdal = null;
}
}
in codebehind Code:
clsbll objbll = new clsbll();
protected void btnsave_Click(object sender, EventArgs e)
{
int recordStatus = 0;
int Id = Convert.ToInt32(txtid.Text);
if (!int.TryParse(txtid.Text, out Id))
{
return;
}
string Name = txtname.Text.ToString();
string Location = txtlocation.Text.ToString();
int Salary = Convert.ToInt32(txtsalary.Text);
int DepartmentId = Convert.ToInt32(txtdepartmentid.Text);
try
{
if (recordStatus > 0)
{
lbldisplay.Text = "This User ID Already Exists.";
return;
}
recordStatus = objbll.insertrecord(Id, Name, DepartmentId, Location, Salary);
if (recordStatus > 0)
{
lbldisplay.Text = "New Record Inserted Successfully.";
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
objbll = null;
}