i am new in c# and i don't know more about it but i have task must to do .
the idea is add new employee and when the user click on add button it must check if the name exist on the employee table in database so if the name exist it will not be added on the table and if not exist will be added on the table .
so please can any one help me on it :(
this is what the page design will be like this pic :
i made the check by the stored procedure and i do not know if it correct and also have an error :
@F_name nvarchar(50),
@isExists bit output
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
set @isExists = 0
if exists (SELECT F_name FROM Employee WHERE F_name =@F_name)
begin
set @isExists=1
END
GO
the error is :
Msg 102, Level 15, State 1, Procedure CheckRecord, Line 22 Incorrect syntax near 'END'.
I'll suggest using this checks on code-behind. You can easily do it with LINQ:
using(DataContext db = new DataContext())
{
var name = db.Employee.Where(n => n.F_name == YourNameFromTextBox).FistOrDefault();
if(name == null)
{
// insert data
}
else
{
// record exist - throw error
}
}
If you dont wanna use LINQ, just use ADO.NET to read/write/update from DB:
SqlConnection dbConnection;
dbConnection = new SqlConnection(connectionString);
SqlCommand myCommand = new SqlCommand("SELECT * FROM Employee WHERE F_name=@F_name", dbConnection);
SqlParameter sqlParam = new SqlParameter("@F_name", F_name);
myCommand.Parameters.Add(sqlParam);
myCommand.Connection.Open();
SqlDataReader myReader = myCommand.ExecuteReader();
string name = "";
if (myReader.Read())
{
name = myReader.GetString("F_name");
// now you get 'name' from DB here, do your job
}
myReader.Close();
myCommand.Connection.Close();
Try this, Just added one more END Statement at the end
@F_name nvarchar(50),
@isExists bit output
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
set @isExists = 0
if exists (SELECT F_name FROM Employee WHERE F_name =@F_name)
begin
set @isExists=1
END
END
GO
You are missing one END
statement as every BEGIN
statement should have END
statement.
It would be:
@F_name nvarchar(50),
@isExists bit output
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
set @isExists = 0
if exists (SELECT F_name FROM Employee WHERE F_name =@F_name)
begin
set @isExists=1
END
END
GO
You can force the database to don't accept the duplication in field Employeename
or use the code
try
con.open
cmd.commandtext="select * from T1 where name = '" & varablename &"'"
reader=cmd.excutereader
if reader.read
msgbox"the emp. is alrady founded in the database"
exit sub
else
' do the insertion
end if
catch ex as exception
msgbox ex.massege
end try
This is how you can write your Stored Procedure. One of the many ways i would say.
CREATE PROCEDURE proc_yourProcName
@F_name nvarchar(50)
AS
BEGIN
IF NOT EXISTS(SELECT F_name FROM Employee WHERE F_name = @F_name)
BEGIN
-- Your INSERT statement here
END
ELSE
BEGIN
-- Your UPDATE statement here
END
END
So if there is any F_Name it wont
INSERT
. If you want, you can set yourUPDATE
logic on theELSE
statement.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.