create fuction [dbo].[Book_id](@id int)
returns char(12)
as begin
return 'BK00ID' + right('00000' + covert (varchar(10), @id),5)
end
i have created this function because i want when ever i enter others column of table Book_id column will automatically filled .

Kunal VaishyaPosted Nov 28, 2013, 11:23 PM
bool res = false;
using (SqlConnection conn = new SqlConnection(GetConnectionString()))
{
using (SqlCommand comm = new SqlCommand("dbo.MyFunction", conn))
{
comm.CommandType = CommandType.StoredProcedure;
SqlParameter p1 = new SqlParameter("@MyParam", SqlDbType.Int);
// You can call the return value parameter anything, .e.g. "@Result".
SqlParameter p2 = new SqlParameter("@Result", SqlDbType.Bit);
p1.Direction = ParameterDirection.Input;
p2.Direction = ParameterDirection.ReturnValue;
p1.Value = myParamVal;
comm.Parameters.Add(p1);
comm.Parameters.Add(p2);
conn.Open();
comm.ExecuteNonQuery();
if (p2.Value != DBNull.Value)
res = (bool)p2.Value;
}
}
return res;
Please refer : http://stackoverflow.com/questions/3013192/calling-sql-functions-directly-from-c-sharp