【问题标题】:Failed to convert parameter value from a String to a Guid c# aspx无法将参数值从字符串转换为 Guid c# aspx
【发布时间】:2017-05-04 13:54:37
【问题描述】:

我正在尝试获取当前页面的当前 GUID 并将值保存到 SQL 中。当页面加载时,它会在标签中加载 GUID。所以我想从标签中获取 GUID 并放入 sql 但它给了我错误:无法将参数值从字符串转换为 Guid。这是我的代码:

   protected void btnAddOGCoverageTeamMembers_Click(object sender, EventArgs e)
    {
        try
        {

            string AccountGUID = base.Request["account"]; 
            guidget.Text = AccountGUID.ToString();

            Hashtable htAMTeam = new Hashtable();
            htAMTeam["GUID"] = guidget.Text; 
            htAMTeam["UserID"] = Convert.ToInt32(Person.SelectedValue);
            htAMTeam["Location"] = Location.SelectedValue;
            htAMTeam["Product"] = Product.Text;
            obj.addTeam(htAMTeam);

        }

        catch (Exception ex)
        {
            lblMsg.Text = ex.Message;
        }

    }

这是页面背后的代码:.cs 页面

public void addTeam(Hashtable htAMTeam)
{
    SqlParameter[] OGTeamparam = new SqlParameter[4];
    OGTeamparam[0] = new SqlParameter("@AccountGUID", SqlDbType.UniqueIdentifier);
    OGTeamparam[1] = new SqlParameter("@UserID", SqlDbType.Int);
    OGTeamparam[2] = new SqlParameter("@Location", SqlDbType.VarChar, 50);
    OGTeamparam[3] = new SqlParameter("@Product", SqlDbType.VarChar, 50);


    OGTeamparam[0].Value = htAMTeam["AccountGUID"];
    OGTeamparam[1].Value = htAMTeam["UserID"].ToString();
    OGTeamparam[2].Value = htAMTeam["Location"].ToString();
    OGTeamparam[3].Value = htAMTeam["Product"].ToString();
    SqlHelper.ExecuteNonQuery(OGconnection, CommandType.StoredProcedure, "InsertAccountOGTeam", OGTeamparam);
}

【问题讨论】:

    标签: c# asp.net guid


    【解决方案1】:

    相反

    OGTeamparam[0].Value = htAMTeam["AccountGUID"];
    

    使用

    OGTeamparam[0].Value = new Guid((string)htAMTeam["AccountGUID"]);
    

    【讨论】:

    • 我收到此错误:CS0246:找不到类型或命名空间名称“GUID”(您是否缺少 using 指令或程序集引用?)
    • @ParbhuBissessar 我的错,我拼错了 GUID。使用指南。我编辑了我的答案。
    • 谢谢,对不起,我又遇到了一个错误:'System.Guid.Guid(byte[])' 的最佳重载方法匹配有一些无效参数
    • 试试Guid.Parse(htAMTeam["AccountGUID"]) ?
    • @ParbhuBissessar:根据您自己的回答,您确实将htAMTeam["AccountGUID"] 存储到string,但是当您取回它时,您会得到一个Object(因为索引器返回一个对象,无论您在其中存储什么)。这就是new Guid([..]) 不起作用的原因。
    【解决方案2】:

    我已经让它工作了,这成功了:

       OGTeamparam[0].Value = new Guid(Convert.ToString(htAMTeam["AccountGUID"]));
    

    【讨论】:

    • 我做了(绿星)
    猜你喜欢
    • 2012-09-30
    • 1970-01-01
    • 1970-01-01
    • 2012-08-16
    • 2021-04-01
    • 2011-11-04
    • 2016-02-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多