WHAT IS THE WRONG WITH SCOPE_IDENTITY?

Simons Hood

Total Post:70

Points:494
Posted by  Simons Hood
C#
 1106  View(s)
Ratings:
Rate this:

I tried milions of methods to make scope identity work. It is just returns __Page !!

Query = "INSERT INTO seekers(name,sname,lname,status,gender,dob,major,experince,email,password,phone,valid,city) values(@name,@sname,@lname,@status,@gender,@dob,@major,@exp,@email,@password,@phone,0,@city);SELECT SCOPE_IDENTITY();";


// setting up command definition

Command = new SqlCommand(Query, Connection);

 

// setting up command parameters

Command.Parameters.AddWithValue("email", txt_email.Text);

Command.Parameters.AddWithValue("gender", lst_gender.SelectedValue);

Command.Parameters.AddWithValue("status", lst_status.SelectedValue);

Command.Parameters.AddWithValue("phone", long.Parse("968" + txt_phone.Text));

Command.Parameters.AddWithValue("password", txt_password.Text);

Command.Parameters.AddWithValue("exp", lst_exp.SelectedValue);

Command.Parameters.AddWithValue("city", lst_exp.SelectedValue);

Command.Parameters.AddWithValue("major", lst_major.SelectedValue);

Command.Parameters.AddWithValue("name", txt_name.Text);

Command.Parameters.AddWithValue("sname", txt_sname.Text);

Command.Parameters.AddWithValue("lname", txt_lname.Text);

Command.Parameters.AddWithValue("dob", cld_birth.SelectedDate);

int ID = (int)Command.ExecuteScalar();

  1. Sumit Kesarwani

    Post:378

    Points:2694
    Re: What is the wrong with Scope_Identity?

    Hi Samuel, 


    Try Out Parameter as follow...

    Query = "INSERT INTO seekers(name,sname,lname,status,gender,dob,major,experince,email,password,phone,valid,city) values(@name,@sname,@lname,@status,@gender,@dob,@major,@exp,@email,@password,@phone,0,@city);SET @ID=SCOPE_IDENTITY();"

    //Your Parameters..

    SqlParameter ID=new SqlParameter();

    ID.Name="@ID";

    ID.Direction=ParameterDirection.Output;

    Command.Parameters.Add(ID);

    Command.ExecuteNonQuery();

    int id=(int)ID.Value;

    or Tryb to Cast Output as follow...

     Query = "INSERT INTO seekers(name,sname,lname,status,gender,dob,major,experince,email,password,phone,valid,city) values(@name,@sname,@lname,@status,@gender,@dob,@major,@exp,@email,@password,@phone,0,@city);SELECT CAST(scope_identity() AS int);"

     int id= (Int32)Command.ExecuteScalar();

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!