Click here to Skip to main content
13,663,464 members
Rate this:
Please Sign up or sign in to vote.
See more:
cmd.Parameters.AddWithValue("@ParentGroupId", ddlParentName.SelectedItem.Value);

In this my parentGroupId is UniqueIdentifier but i am passing the value as parameters while inserting the data having the following error..

Error converting data type nvarchar to uniqueidentifier.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Error converting data type nvarchar to uniqueidentifier.

Source Error:
Line 39:                 }
Line 40:                 con.Open();
Line 41:                 int res = cmd.ExecuteNonQuery();
Line 42:                 if (res > 0)
Line 43:                 {

please how can i remove this error..
Posted 26-Nov-13 20:17pm
Updated 26-Nov-13 20:23pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Can you try
Guid guid;
if (Guid.TryParse(ddlParentName.SelectedItem.Value, out guid))
Parameters.Add("@ParentGroupId", SqlDbType.UniqueIdentifier).Value =  guid;
ajays3356 27-Nov-13 2:53am
Following is my dropdown list from where i am getting the ddlParentName.SelectedItem.Value
<asp:DropDownList ID="ddlParentName" CssClass="nonwatermarkedddl" runat="server"
Width="146" AppendDataBoundItems="true">
<asp:ListItem Selected="True" Text="None" Value="">

and code is

using (SqlDataAdapter adapt = new SqlDataAdapter(cmd))
DataSet ds = new DataSet();
ddlParentName.DataSource = ds;
ddlParentName.DataTextField = "AccountGroupName";
ddlParentName.DataValueField = "AccountGroupId";

And Now AccountgroupId is Uniqueidentifier
and i have requirement like if user selects the none in DropDown then null will be in dbcoloumn and when he selects the menu from dropdown the that uniqueidentifier value should be store in an database...

Now, i have try your code now it shows cannot convert string to GUID..
JoCodes 27-Nov-13 3:03am
Have you tried the updated code? Have added tryparse to handle null ? Can you show your sqlscript to check whether your accountgroupid is uniqueidentifier type?
ajays3356 27-Nov-13 3:12am
Guid does not contain an defination for TryParse..This is shows in Intellesense..
JoCodes 27-Nov-13 4:00am
Ok. That means you are using lower version than C# 3.5 ?
JoCodes 27-Nov-13 4:07am
Then you can use Guid.Parse in case you are using lower framework by checking the ddl selected value is null or not in the if condition.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

A little unclear which value is defined as a uniqueidentifier: the database column for ParentGroupId or ddlParentName.SelectedItem.Value? I assume here the first to be a uniqueidentifier, and the second a string. Hence I'd suggest:
cmd.Parameters.AddWithValue("@ParentGroupId", Guid.Parse(ddlParentName.SelectedItem.Value));
ajays3356 27-Nov-13 2:48am
Both value are uniqueidentifier in database..
Bernhard Hiller 27-Nov-13 2:53am
No, your comment is wrong: ddlParentName.SelectedItem.Value is a value in your local program, not in the database. When it has a different type than its correspondent value in the database, it must be converted. And look at your error message: ...converting...
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Try making the Unique ID parameter to nvarchar or varchar in the stored procedure. If proper unique identifier value is getting passed and then the value will be stored in the table without any errors

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web05-2016 | 2.8.180810.1 | Last Updated 27 Nov 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100