Friday, 2 May 2014

Check if perticular value already exists or not in SP

No comments
stored procedure

CREATE PROCEDURE apply_job
   
    (
        @Job_id int,
        @Stu_id int,
        @Error nvarchar(50) output
    )
   
AS
if exists (select * from Resume where Stu_id=@Stu_id)
begin
   

    insert into Job_applied(Job_id,Stu_id,Req_id) values(@Job_id,@Stu_id,(select Req_id from Jobs where Job_id=@Job_id))

    set @Error='Applied For Job Sucessfully'
end
else
begin
set @Error='Please Upload Your Resume First'
end


.cs
 
    public string apply_job(portalDal dl)
    {
        con.Open();
        cmd = new SqlCommand("apply_job", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@Job_id", dl.job_id);
        cmd.Parameters.AddWithValue("@Stu_id", dl.stu_id);
        cmd.Parameters.Add("@Error", SqlDbType.Char, 50);
        cmd.Parameters["@Error"].Direction = ParameterDirection.Output;
        cmd.ExecuteNonQuery();
        string msg = (string)cmd.Parameters["@Error"].Value;
        con.Close();
        return msg;
    }
protected void btnapply_Click(object sender, EventArgs e)
    {             
            string msg = string.Empty;
            dl.job_id = Convert.ToInt16(l1.Text);
            dl.stu_id = Convert.ToInt16(Session["Stu_id"]);
            msg = bl.apply_job(dl);
            lbljob.Text = msg;               
    }