Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hello,
C#
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
Updated 26-Nov-13 20:23pm
v2

Can you try
C#
Guid guid;
if (Guid.TryParse(ddlParentName.SelectedItem.Value, out guid))
{
Parameters.Add("@ParentGroupId", SqlDbType.UniqueIdentifier).Value =  guid;
}
 
Share this answer
 
v3
Comments
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();
adapt.Fill(ds);
ddlParentName.DataSource = ds;
ddlParentName.DataTextField = "AccountGroupName";
ddlParentName.DataValueField = "AccountGroupId";
ddlParentName.DataBind();
}
}

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.
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));
 
Share this answer
 
Comments
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...
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
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900