Home > DeveloperSection > Forums > What is the wrong with Scope_Identity?
Simons Hood
Simons Hood

Total Post:70

Points:494
Posted on    August-28-2014 1:56 AM

 C#
Ratings:


 1 Reply(s)
 736  View(s)
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();



Sumit Kesarwani

Total Post:378

Points:2694
Posted on    August-28-2014 3:10 AM

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();


Don't want to miss updates? Please click the below button!

Follow MindStick