Hi Guys please help me out in my code where I got stuck and went wrong, I have two tables; one employee table and another branch table. The branch id will be stored in employee table which is a foreign key. When I'm inserting the data through stored procedure where I kept the code like bleow
Create procedure [dbo].[Insertemployee]
@d28 int,@d1 nchar(15),@d2 nchar(30),@d3 int,@d4 nchar(10),@d5 nchar(20),@d6 nchar(30),@d7 varchar(100),@d8 nchar(100),@d9 nchar(10),@d10 nchar(10),@d11 nchar(30),@d12 nchar(30),@d13 nchar(30),@d14 nchar(30),@d15 nchar(30),@d16 date,@d17 varchar(100),@d18 int,@d19 int,@d20 int,@d21 int,@d22 int,@d23 int,@d24 int,@d25 image,@d26 date,@d27 date,@d29 nchar(10)
AS
BEGIN
SET NOCOUNT ON
BEGIN TRY
BEGIN TRANSACTION
insert into Employee(Id,StaffID,StaffName,Branchid,Gender,DOB,FatherName,PermanentAddress,Country,PhoneNo,MobileNo,DateOfJoining,VisaNumber,PassportNumber,PassportExpiryDate,NationalityID,NationalityIDExpiryDate,Designation,BasicSalary,CostOfAllowance,HRA,TeaAndSnacks,Bonus,Loan,OtherDeduction,Picture,createddate,lastmodified,Active) VALUES (@d28,@d1,@d2,(select branchid from Branch where Branch.BranchName=@d3),@d4,@d5,@d6,@d7,@d8,@d9,@d10,@d11,@d12,@d13,@d14,@d15,@d16,@d17,@d18,@d19,@d20,@d21,@d22,@d23,@d24,@d25,@d26,@d27,@d29);
COMMIT TRANSACTION
END TRY
BEGIN CATCH
IF @@TRANCOUNT >0
BEGIN
ROLLBACK TRANSACTION
END
EXECUTE uspLogError
END CATCH
END;`
and my front end code is below
cmd = new SqlCommand("InsertEmployee");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = con;
cmd.Parameters.AddWithValue("@d28",txtID.Text);
cmd.Parameters.AddWithValue("@d26", System.DateTime.Now);
cmd.Parameters.AddWithValue("@d27", System.DateTime.Now);
cmd.Parameters.AddWithValue("@d1", txtStaffID.Text);
cmd.Parameters.AddWithValue("@d2",txtStaffName.Text);
cmd.Parameters.AddWithValue("@d3",txtDepartment.Text);
cmd.Parameters.AddWithValue("@d4",cmbGender.Text);
cmd.Parameters.AddWithValue("@d6",txtFatherName.Text);
cmd.Parameters.AddWithValue("@d7",txtPAddress.Text);
cmd.Parameters.AddWithValue("@d8",txtTAddress.Text);
cmd.Parameters.AddWithValue("@d9",txtPhoneNo.Text);
cmd.Parameters.AddWithValue("@d10",txtMobileNo.Text);
cmd.Parameters.AddWithValue("@d11",dtpDateOfJoining.Value);
cmd.Parameters.AddWithValue("@d12",visanumber.Text.Trim());
cmd.Parameters.AddWithValue("@d14",PassportExpirydate.Text);
cmd.Parameters.AddWithValue("@d15",NationalityID.Text.Trim());
if (NationalityID.Text == "")
{
cmd.Parameters.AddWithValue("@d16", "");
}
else
{
cmd.Parameters.AddWithValue("@d16", NationalityExpirydate.Value);
}
cmd.Parameters.AddWithValue("@d17",txtDesignation.Text.Trim());
cmd.Parameters.AddWithValue("@d13",Passportnumber.Text.Trim());
cmd.Parameters.AddWithValue("@d18",Convert.ToInt32(txtBasicSalary.Text.Trim()));
if (txtLIC.Text == "")
{
cmd.Parameters.AddWithValue("@d19", 0);
}
else
{
cmd.Parameters.AddWithValue("@d19",Convert.ToInt32(txtLIC.Text.Trim()));
}
if (txtGrpInsurance.Text == "")
{
cmd.Parameters.AddWithValue("@d21", 0);
}
else
{
cmd.Parameters.AddWithValue("@d21",Convert.ToInt32(txtGrpInsurance.Text.Trim()));
}
if (txtFamilyBenefitFund.Text == "")
{
cmd.Parameters.AddWithValue("@d22",0);
}
else
{
cmd.Parameters.AddWithValue("@d22",Convert.ToInt32(txtFamilyBenefitFund.Text.Trim()));
}
if (txtLoans.Text == "")
{
cmd.Parameters.AddWithValue("@d23",0);
}
else
{
cmd.Parameters.AddWithValue("@d23",Convert.ToInt32(txtLoans.Text.Trim()));
}
if (txtOtherDeductions.Text == "")
{
cmd.Parameters.AddWithValue("@d24",0);
}
else
{
cmd.Parameters.AddWithValue("@d24",Convert.ToInt32(txtOtherDeductions.Text.Trim()));
}
if (txtIncomeTax.Text == "")
{
cmd.Parameters.AddWithValue("@d20",0);
}
else
{
cmd.Parameters.AddWithValue("@d20",Convert.ToInt32(txtIncomeTax.Text.Trim()));
}
cmd.Parameters.AddWithValue("@d29", chkActive.Text);
cmd.Parameters.AddWithValue("@d5",DOB.Text);
MemoryStream ms = new MemoryStream();
Bitmap bmpImage = new Bitmap(pictureBox1.Image);
bmpImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] data = ms.GetBuffer();
SqlParameter p = new SqlParameter("@d25", SqlDbType.Image);
p.Value = data;
cmd.Parameters.Add(p);
cmd.ExecuteNonQuery();