user1724708
user1724708

Reputation: 1469

Invalid cast from 'System.String' to 'System.Guid'

I have a submission form that consist of a DropDownList Control; it's ListItem values consist of an UniqueIdentifier, e.g. 524988aa-0594-4ebe-b7fa-61396855e17f.

I'm attempting to insert the value of the ListItem into a table in the database via a stored procedure, but get a "Invalid cast from 'System.String' to 'System.Guid" error upon submission.

Hypertext:

 <asp:DropDownList ID="ddlserver" runat="server">
    <asp:ListItem Text="Server 1" Value="88dc9e7d-0904-4a49-a6d9-8d0badfc4d0b" />
    <asp:ListItem Text="Server 2" Value="524988aa-0594-4ebe-b7fa-61396855e17f" />
 </asp:DropDownList> 

Code Behind:

SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "InsertClient";
cmd.Parameters.Add("@UserId", SqlDbType.UniqueIdentifier).Value = ddlserver.SelectedValue.ToString();
cmd.Parameters.Add("@ClientName", SqlDbType.NVarChar).Value = txtName.Text;
cmd.Parameters.Add("@EmailAddress", SqlDbType.NVarChar).Value = txtEmail.Text;
cmd.Parameters.Add("@PhoneNumber", SqlDbType.NVarChar).Value = txtPhone.Text;
cmd.Parameters.Add("@ServicesNum", SqlDbType.NVarChar).Value = rbzero.Text;
cmd.Connection = con;

stored Procedure:

ALTER PROCEDURE [dbo].[InsertClient]

    @UserId UniqueIdentifier,
    @ClientName nvarchar(150),
    @EmailAddress nvarchar(150),
    @PhoneNumber nvarchar(50),
    @ServicesNum nvarchar(50)

AS
BEGIN
    SET NOCOUNT ON;
    INSERT INTO  Client_tb (UserId, ClientName, EmailAddress, PhoneNumber, ServicesNum)
    VALUES (@UserId, @ClientName,  @EmailAddress, @PhoneNumber, @ServicesNum) 
END

...could I get some assistance please as to what I'm doing wrong?

Upvotes: 0

Views: 3200

Answers (5)

Nicholas Carey
Nicholas Carey

Reputation: 74267

You can't cast a string to a GUID. You need to use the appropriate overload of Guid.Parse() or Guid.TryParse() to get what you want.

Upvotes: 1

Iucounu
Iucounu

Reputation: 1660

The problem is likely occurring in this line:

cmd.Parameters.Add("@UserId", SqlDbType.UniqueIdentifier).Value = ddlserver.SelectedValue.ToString();

Try explicitly creating a GUID like this:

cmd.Parameters.Add("@UserId", SqlDbType.UniqueIdentifier).Value = new System.Guid(ddlserver.SelectedValue.ToString());

Upvotes: 0

Jon Grant
Jon Grant

Reputation: 11530

It's this line here:

cmd.Parameters.Add("@UserId", SqlDbType.UniqueIdentifier).Value = ddlserver.SelectedValue.ToString();

You are saying it's a Guid (or UniqueIdentifier, different names for the same thing) and passing in a String (because of the ToString(), although it was already a String anyway).

Change it to pass a Guid like this:

cmd.Parameters.Add("@UserId", SqlDbType.UniqueIdentifier).Value = new Guid(ddlserver.SelectedValue.ToString());

Upvotes: 5

Aladin Hdabe
Aladin Hdabe

Reputation: 865

Try this:

cmd.Parameters.Add("@UserId", SqlDbType.UniqueIdentifier).Value = new Guid(ddlserver.SelectedValue.ToString());

Upvotes: 1

gdoron
gdoron

Reputation: 150253

ddlserver.SelectedValue.ToString(); Is obviously a string
You assign it as a guid parameter: .Add("@UserId", SqlDbType.UniqueIdentifier) Cast to guid with: new Guid(ddlserver.SelectedValue.ToString());

Upvotes: 0

Related Questions