如何在查询字符串中检查段

时间:2015-11-10 14:55:59

标签: c# asp.net

我收到以下错误但由于某种原因它在一个在uril中没有的guid我认为我可以只检查是否为空但当然关键不存在

这并不重复它要求如何检查它自己的实际密钥以查看它是否存在。 !!!

Line 23: 
Line 24:                 player _player = new player();
Line 25:                 Guid id = new         
Guid(Request.QueryString["id"].ToString());
Line 26:                 if (id == Guid.Empty)
Line 27:                 {

这是其权利中的代码

protected void Page_Load(object sender, EventArgs e)
    {

        try
        {

            player _player = new player();
            Guid id = new Guid(Request.QueryString["id"].ToString());
            if (id == Guid.Empty)
            {


            }
            else
            {
                _player = _dal.GetPlayerBYID(id);


                if (!Page.IsPostBack)
                {
                    if (_player.Name != null)
                        txtFullName.Text = _player.Name;
                    if (_player.address != null)
                        txtAddress.Text = _player.address;

                    dlGenders.SelectedValue = _player.gender;

                }


            }


            var dlGendersSource = _dal.GetGenders();
            dlGenders.DataSource = dlGendersSource;
            dlGenders.DataValueField = "LookupValue";
            dlGenders.DataTextField = "LookupDescription";
            dlGenders.DataBind();



            string message = "";
            if (Context.User.IsInRole("canEdit"))
            {
                //enable edit controls on page

                message += "This user can edit a record";
            }
            else if (Context.User.IsInRole("canDelete"))
            {
                //enable delete controls on page
                message += "This user can delete a record.";
            }
            else if (Context.User.IsInRole("canAdd"))
            {
                //enable delete controls on page
                message += "This user can add a record.";
            }
            RadNotification1.Position = NotificationPosition.BottomRight;
            RadNotification1.Text = message;


        }
        catch (Exception ex)
        {
            throw new EntityContextException("Page Load Failed in Edit Players  .", ex);
        }


    }

屏幕截图显示错误它导致团队最初不会总是附加一个玩家ID,所以我需要能够处理它直到调用保存更改。

enter image description here

这个部分是因为我有这个问题所以没有downvotes !!!!

       Guid id = new Guid(Request.QueryString["id"]);

1 个答案:

答案 0 :(得分:3)

改变这种方式

        Guid id = new Guid(Request.QueryString["id"] ?? "");
        if (id == Guid.Empty)

...