Click here to Skip to main content
15,900,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

C#
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:

C#
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))
          {
              // Report problem to your user
              return;
          }
          string Name = txtname.Text.ToString();
          string Location = txtlocation.Text.ToString();
          int Salary = Convert.ToInt32(txtsalary.Text);
          int DepartmentId = Convert.ToInt32(txtdepartmentid.Text);

          try
          {
             // Passing the Parameter in the Method of BAl using BAL object
              //recordStatus = objbll.checkExistance_BAL(Id);
              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.";

              }
              //recordStatus = objbll.checkExistance_BAL(Id);
              //if (recordStatus > 0)
              //{
              //    lbldisplay.Text = "This Record Already Exists.";
              //}
          }
          catch (Exception ex)
          {
              throw ex;
          }
          finally
          {
              objbll = null;
          }
Posted

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900