I was working with SqlDataAdapter
which is doing insert operation and should show identity column as output parameter in application.
Here is my code
// created connection object
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand("GetPersons",con);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
da.InsertCommand = new SqlCommand("IPersons",con);
da.InsertCommand .CommandType = CommandType.StoredProcedure;
da.InsertCommand .Parameters.AddWithValue("@Name", txtname.Text);
// output param
SqlParameter param = new SqlParameter();
param.ParameterName = "@ID";
param.DbType = DbType.Int32;
param.Direction = ParameterDirection.Output;
da.InsertCommand.Parameters.Add(param);
DataSet ds = new DataSet();
da.Fill(ds);
string var = da.InsertCommand.Parameters["@ID"].Value.ToString();
..but in the last line I can't get value of the output parameter, it shows null value
This is the stored procedure:
Create Procedure [dbo].[IPersons]
@Name varchar(255) = null,
@ID int Out
AS
Begin
insert into Persons values(@Name)
select @ID = scope_Identity()
End
What am I missing from taking out output parameter?