Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i want to develop a web base parking system

i want to retrieve the newly inserted or latest generated identity from sql server database.

after filling up and saving the new inserted data to the table

i want to display that identity value from the browser.

how may i suppose to do that.

updating/ inserting new data to database has no problem,

btw, i use stored procedure for adding data.

this is the code upon clicking button

SQL
protected void Button1_Click(object sender, EventArgs e)
    {
        string cs = "data source=.; database=palistuhan; integrated security=SSPI";
        using (SqlConnection con = new SqlConnection(cs))
        {
            SqlCommand cmd = new SqlCommand("addpark", con);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@plate", TextBox1.Text);
            cmd.Parameters.AddWithValue("@timein", TextBox1.Text);
            cmd.Parameters.AddWithValue("@timeout", 0);
            cmd.Parameters.AddWithValue("@cost", 0);
            cmd.Parameters.AddWithValue("@date", 0);
            SqlParameter outputParameter = new SqlParameter();
            outputParameter.ParameterName = "@ticket";
            outputParameter.SqlDbType = System.Data.SqlDbType.Int;
            outputParameter.Direction = System.Data.ParameterDirection.Output;
            cmd.Parameters.Add(outputParameter);

            con.Open();
            cmd.ExecuteNonQuery();

        }

    }


i tried other code from other forum. but it seems as it returns null value.
please help me. thanks


[edit]SHOUTING removed, Code block added - OriginalGriff[/edit]


and this is my strored procedure

USE [palistuhan]
GO
/****** Object: StoredProcedure [dbo].[addpark] Script Date: 04/12/2015 12:55:24 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[addpark]
@ticket int out,
@plate nchar(10),
@timein float,
@timeout float,
@date nchar(10),
@cost float
as
begin
Select @ticket = scope_identity()
insert into parking values (@plate, @timein, @timeout, @date,@cost)
return @ticket
end
Posted
Updated 11-Apr-15 18:59pm
v3

Start by looking at your SP and see exactly what it does. Concentrate on the output parameter.

If that's all ok (and you can test it in SSMS to check) then you need to start looking at the code where you use the return value - which you don't show...
 
Share this answer
 
Check out six different to get the last identity - 6 Different Ways To Get The Current Identity Value in SQL[^].
 
Share this answer
 
Check out to get the last identity @@identity
 
Share this answer
 
Try to user
ExecuteScalar
instead of using
ExecuteNonQuery
.

Its reason is that ExecuteNonQuery returns number of records affected.
While the ExecuteScalar returns the record from the first column of the first row of a given query. So if your query is returning the correct value, then ExecuteScalar will show the correct value. Also make sure to hold the returned records from the ExecuteScalar so that you can use it somewhere.
 
Share this answer
 

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