Hi Friends,
I have a store procedure which return id from contact to c#, but in c# i couldn't take it.
This my c# code.
Store procedure works truly.
public static int ContactInsert(string name, string family, string address, string phoneNumber, string birthDate, string emailAddress, string faceBook)
{
try
{
using (SqlConnection cnn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DB"].ConnectionString))
{
SqlCommand cmm = new SqlCommand("Contact_Insert",cnn);
cmm.CommandType = CommandType.StoredProcedure;
cmm.Parameters.AddWithValue("@Name", name);
cmm.Parameters.AddWithValue("@Family", family);
cmm.Parameters.AddWithValue("@Adderess", address);
cmm.Parameters.AddWithValue("@phoneNumber", phoneNumber);
cmm.Parameters.AddWithValue("@BirthDate",Convert.ToDateTime( birthDate).Date);
cmm.Parameters.AddWithValue("@EmailAddress", emailAddress);
cmm.Parameters.AddWithValue("@Facebook", faceBook);
cnn.Open();
var value = cmm.ExecuteScalar();
cnn.Close();
return Convert.ToInt32(value);
}
}
catch (Exception)
{
throw;
}
}
and this my Store procedure
USE [Mobile1]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER Procedure [dbo].[Contact_Insert]
@Name nvarchar(50),
@Family nvarchar(50),
@Adderess nvarchar(100),
@PhoneNumber char(10),
@BirthDate date,
@EmailAddress nvarchar(100),
@Facebook nvarchar(100)
As
Declare @x int;
Begin
Begin Try
Begin Tran a
If exists(Select id from Contact Where Name= @Name and Family =@Family)
Begin
set @x = (Select Id From Contact Where Name = @Name and Family = @Family)
End
else
Begin
Insert into Contact
(Name, Family, Address1, PhoneNumber, BirthDate, EmailAddress, Facebook)
Values
(@Name, @Family, @Adderess, @PhoneNumber, @BirthDate, @EmailAddress, @Facebook )
set @x = (Select Id From Contact Where Name= @Name and Family = @Family)
End
Commit Tran a;
End Try
Begin Catch
RollBack tran a;
End Catch
Return @x;
End