Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
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

SQL
USE [Mobile1]
GO
/****** Object:  StoredProcedure [dbo].[Contact_Insert]    Script Date: 9/23/2013 3:07:18 PM ******/
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
Posted
Updated 23-Sep-13 1:46am
v2

You need to declare parameter as a output to return a value.

See the link for reference http://stackoverflow.com/questions/14810037/get-return-value-from-stored-procedure-in-asp-net[^]
 
Share this answer
 
Comments
CPallini 23-Sep-13 7:59am    
5.
ArunRajendra 23-Sep-13 8:09am    
Thanks
Elham.Deljooei 23-Sep-13 8:18am    
Sorry, I couldn't found, i saw this link put ???
ArunRajendra 23-Sep-13 8:28am    
Do this change in your stored proc.
Remove this
Declare @x int;

and do this
@Facebook nvarchar(100),
@x int output
The ExecuteScalar takes the firs column on the first row.

Change this in your SP:
Return @x;

TO this:
Select @x;
 
Share this answer
 
Comments
Elham.Deljooei 24-Sep-13 3:21am    
I could do it with second solution, Thanks a million my friend.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900