导航到其他页面时如何检索ID? [英] how can i retreive Id when i navigate to other pages?

查看:110
本文介绍了导航到其他页面时如何检索ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨.
我已经在sqlserver中编写了一个存储过程:

Hi.
i have written a stored procedure in sqlserver:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

Create procedure [dbo].[Insert_Person_SP]
(
@CityId int ,
@Name nvarchar(100),
@Family nvarchar(100),
@Gender nchar(10),
@MilitaryStatus nvarchar(50)
)
as 

begin
insert into Person (CityId,Name,Family,Gender,MilitaryStatus)
values (@CityId,@Name,@Family,@Gender,@MilitaryStatus)

return @@Identity 
end



而且我已经在Asp.net中制作了一个用于检索PersonId的类,如下所示:



and also i have made a class in Asp.net for retrieving PersonId as below:

public class globalvaribles
    {
        public static int PersonId
        {
            get
            {
                if (HttpContext.Current.Session["PersonId"] == null)
                    HttpContext.Current.Session["PersonId"] = -1;
                return int.Parse(HttpContext.Current.Session["PersonId"].ToString());
            }

            set
            {
                HttpContext.Current.Session["PersonId"] = value;
            }
        }

    }


最后,在我的主要代码中,我打算将数据插入DB:


and finally in my main code i intend to insert data into DB:

protected void SubmitButton(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection();
            SqlTransaction trans = null;
            
            conn.ConnectionString = ConfigurationManager.ConnectionStrings["PersonTest"].ConnectionString;
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            conn.Open();

            trans = cmd.Connection.BeginTransaction();
            try
            {
                int pcode = 0;
                int.TryParse(DropCity.SelectedValue, out pcode);
                cmd.CommandText = "Insert_Person_SP";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@Name", SqlDbType.NVarChar).Value = textbox1.Text;
                cmd.Parameters.Add("@Family", SqlDbType.NVarChar).Value = TextBox2.Text;
                cmd.Parameters.Add("@CityId", SqlDbType.Int).Value = DropCity.SelectedValue;
                cmd.Parameters.Add("@Gender", SqlDbType.NChar).Value = RadioGender.SelectedValue;
                cmd.Parameters.Add("@MilitaryStatus", SqlDbType.NVarChar).Value = DropDownMilitary.SelectedValue;
                trans.Commit();
                globalvaribles.PersonId = int.Parse(cmd.ExecuteScalar().ToString());
                
            }
            catch (SqlException se)
            {
                try
                {
                    trans.Rollback();
                }
                catch{ }
                Page.Title = "SqlException:" + se.Message;
                try
                {
                    trans.Rollback();
                }
                catch (Exception ex)
                {
                    Page.Title = "Exception:" + ex.Message;
                }   
            }
            finally
            {
                try
                {
                    conn.Close();
                }
                catch { }
            }
            Response.Redirect("~/JobInfo.aspx");
        }


在第一页中,当我导航到第二页时,我希望能够检索PersonId,但是此消息出现在此行中:
(


in the first page and when i navigate to second page , i want to be able to retrieve PersonId , but thsi message appeares in this line:
(

globalvaribles.PersonId = int.Parse(cmd.ExecuteScalar().ToString());

)

用户代码未处理NullRefrence异常.
对象引用未设置为对象的实例

而且我不知道这种检索PersonId的方法是否正确?

)

NullRefrence Exception Was Unhandeled by user code.
Object reference not set to an instance of an object

and i don''t know whether this way of retreiving PersonId is correct or not?

推荐答案

您还需要SELECT it,而不是RETURN it.

ExecuteScalar将返回结果集第一行中的第一列值.

在您的过程中尝试以下操作:
You also need to SELECT it, not RETURN it.

ExecuteScalar will return the first column value from the first row of a result set.

Try this in your procedure:
SELECT SCOPE_IDENTITY();



其余的事情都还可以.


--- Amit



Rest of the things are OK.


---Amit




发生异常是因为您已使用以下代码将标量值转换为ToString.

Hi,

Exception occur because you have used below code to cast your Scalar value into ToString.

globalvaribles.PersonId = int.Parse(cmd.ExecuteScalar().ToString());


改为使用


instead use

globalvaribles.PersonId = int.Parse(Convert.ToString(cmd.ExecuteScalar()));



这不会引发异常.

谢谢
-Amit Gajjar



This will not throw exception.

Thanks
-Amit Gajjar


这篇关于导航到其他页面时如何检索ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆