我尝试了数百万种方法来使范围标识工作。它只是返回__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();
答案 0 :(得分:0)
尝试参数如下......
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;
或 尝试投射输出如下......
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();
答案 1 :(得分:0)
SCOPE_IDENTITY将返回小数,请尝试:
int ID = (int) (decimal) Command.ExecuteScalar();
你的意思不太清楚:
只是返回__Page !!
据推测,发布的代码会抛出异常,这会导致某些更高级别的代码执行“...返回__Page”的任何操作。
如果查看异常详细信息,您会发现更多有关发生的事情:我怀疑是InvalidCastException
,因为您正在尝试将Command.ExecuteScalar返回的对象(一个盒装小数)转换为一个int。
答案 2 :(得分:0)
使用set nocount off并尝试
As First execute语句是插入返回值是受影响的记录数