Charles Xavier
Charles Xavier

Reputation: 1045

Failed to convert parameter value from a String to a Guid c# aspx

I'm trying to get the current GUID for the current page and saving the value into SQL. When the page loads it loads the GUID in a label. So I want to take the GUID from the label and put in into sql but it gives me the error: Failed to convert parameter value from a String to a Guid. This is my code:

   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;
        }

    }

This is the code behind page: .cs page

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

Upvotes: 2

Views: 2101

Answers (2)

BWA
BWA

Reputation: 5764

Instead

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

use

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

Upvotes: 4

Charles Xavier
Charles Xavier

Reputation: 1045

I've gotten it to work, This did the trick:

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

Upvotes: 2

Related Questions