By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,928 Members | 1,810 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,928 IT Pros & Developers. It's quick & easy.

ASP.Net-C#: "Conversion failed... character string to uniqueidentifier"

P: 3
Hello,

I am using a SQL data source to insert records into a table. The table uses a GUID as the primary key (GoalEventID), and a GUID for a second field (GoalID) as a foreign key.

When I call SqlDataSource1.Insert() I get the exception : "Conversion failed when converting from a character string to uniqueidentifier."

I'd sure appreciate any help in resolving the issue. The code is below. Thank you.

---------------

The table definition is as follows:
GoalEventID : uniqueidentifier : pk : default=newid() : RowGuid=Yes
GoalID : uniqueidentifier : fk
GoalValue : float
GoalDateTime : datetime


Expand|Select|Wrap|Line Numbers
  1.     <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
  2.         ConflictDetection="CompareAllValues" 
  3.         ConnectionString="<%$ ConnectionStrings:goals_goalsConnectionString1 %>" 
  4.         DeleteCommand="DELETE FROM [tblGoalEvents] WHERE [GoalEventID] = @original_GoalEventID AND [GoalID] = @original_GoalID AND [GoalValue] = @original_GoalValue AND [GoalDateTime] = @original_GoalDateTime" 
  5.         InsertCommand="INSERT INTO tblGoalEvents(GoalEventID, GoalID, GoalValue, GoalDateTime) VALUES (@GoalEventID, @GoalID, @GoalValue, @GoalDateTime)" 
  6.         OldValuesParameterFormatString="original_{0}" 
  7.         SelectCommand="SELECT tblGoals.GoalID FROM tblGoals INNER JOIN vwUsers ON tblGoals.UserID = vwUsers.UserID WHERE (tblGoals.GoalName = @GoalName) AND (vwUsers.Email = @Email)" 
  8.         UpdateCommand="UPDATE [tblGoalEvents] SET [GoalID] = @GoalID, [GoalValue] = @GoalValue, [GoalDateTime] = @GoalDateTime WHERE [GoalEventID] = @original_GoalEventID AND [GoalID] = @original_GoalID AND [GoalValue] = @original_GoalValue AND [GoalDateTime] = @original_GoalDateTime">
  9.  
  10.       <!-- **Select, Update, and Delete commands ommitted for brevity**-->
  11.  
  12.         <InsertParameters>
  13.             <asp:Parameter Name="GoalEventID" Type="Empty" DefaultValue="newid()" />
  14.             <asp:Parameter Name="GoalID" Type="Empty" />
  15.             <asp:Parameter Name="GoalValue" Type="Double" />
  16.             <asp:Parameter Name="GoalDateTime" Type="DateTime" />
  17.         </InsertParameters>
  18.     </asp:SqlDataSource>

In the page load event I have:
Expand|Select|Wrap|Line Numbers
  1.   //...
  2.   goalID = new Guid(reader[0].ToString());
  3.   //...
  4.   SqlDataSource1.InsertParameters["GoalID"].DefaultValue = goalID.ToString();
  5.   SqlDataSource1.InsertParameters["GoalValue"].DefaultValue = goalEventValue.ToString();
  6.   SqlDataSource1.InsertParameters["GoalDateTime"].DefaultValue = goalDateTime.ToString();
  7.   SqlDataSource1.Insert();
Jan 3 '08 #1
Share this Question
Share on Google+
3 Replies


Frinavale
Expert Mod 5K+
P: 9,731
Hello,

I am using a SQL data source to insert records into a table. The table uses a GUID as the primary key (GoalEventID), and a GUID for a second field (GoalID) as a foreign key.

When I call SqlDataSource1.Insert() I get the exception : "Conversion failed when converting from a character string to uniqueidentifier."

I'd sure appreciate any help in resolving the issue. The code is below. Thank you.

---------------

The table definition is as follows:
GoalEventID : uniqueidentifier : pk : default=newid() : RowGuid=Yes
GoalID : uniqueidentifier : fk
GoalValue : float
GoalDateTime : datetime


Expand|Select|Wrap|Line Numbers
  1.     <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
  2.         ConflictDetection="CompareAllValues" 
  3.         ConnectionString="<%$ ConnectionStrings:goals_goalsConnectionString1 %>" 
  4.         DeleteCommand="DELETE FROM [tblGoalEvents] WHERE [GoalEventID] = @original_GoalEventID AND [GoalID] = @original_GoalID AND [GoalValue] = @original_GoalValue AND [GoalDateTime] = @original_GoalDateTime" 
  5.         InsertCommand="INSERT INTO tblGoalEvents(GoalEventID, GoalID, GoalValue, GoalDateTime) VALUES (@GoalEventID, @GoalID, @GoalValue, @GoalDateTime)" 
  6.         OldValuesParameterFormatString="original_{0}" 
  7.         SelectCommand="SELECT tblGoals.GoalID FROM tblGoals INNER JOIN vwUsers ON tblGoals.UserID = vwUsers.UserID WHERE (tblGoals.GoalName = @GoalName) AND (vwUsers.Email = @Email)" 
  8.         UpdateCommand="UPDATE [tblGoalEvents] SET [GoalID] = @GoalID, [GoalValue] = @GoalValue, [GoalDateTime] = @GoalDateTime WHERE [GoalEventID] = @original_GoalEventID AND [GoalID] = @original_GoalID AND [GoalValue] = @original_GoalValue AND [GoalDateTime] = @original_GoalDateTime">
  9.  
  10.       <!-- **Select, Update, and Delete commands ommitted for brevity**-->
  11.  
  12.         <InsertParameters>
  13.             <asp:Parameter Name="GoalEventID" Type="Empty" DefaultValue="newid()" />
  14.             <asp:Parameter Name="GoalID" Type="Empty" />
  15.             <asp:Parameter Name="GoalValue" Type="Double" />
  16.             <asp:Parameter Name="GoalDateTime" Type="DateTime" />
  17.         </InsertParameters>
  18.     </asp:SqlDataSource>

In the page load event I have:
Expand|Select|Wrap|Line Numbers
  1.   //...
  2.   goalID = new Guid(reader[0].ToString());
  3.   //...
  4.   SqlDataSource1.InsertParameters["GoalID"].DefaultValue = goalID.ToString();
  5.   SqlDataSource1.InsertParameters["GoalValue"].DefaultValue = goalEventValue.ToString();
  6.   SqlDataSource1.InsertParameters["GoalDateTime"].DefaultValue = goalDateTime.ToString();
  7.   SqlDataSource1.Insert();
Are you sure that the GoalID and GoalEventID are Strings in your DataBase?
Make sure that your data types match....
Jan 4 '08 #2

P: 3
Are you sure that the GoalID and GoalEventID are Strings in your DataBase?
Make sure that your data types match....
I know that they are NOT strings in the database. They are uniqueidentifiers. However, I thought I'd read somewhere that it would perform implicit conversion. Regardless, knowing the database fields are uniqueidentifiers and not strings, how would I pass the parameters as GUIDs? There is no 'Type="Empty"'

I have also tried casting them in the SQL statement (as follows), but I get the same "conversion" error message.

Expand|Select|Wrap|Line Numbers
  1. InsertCommand="INSERT INTO tblGoalEvents(GoalEventID, GoalID, GoalValue, GoalDateTime) VALUES (CAST(@GoalEventID AS UNIQUEIDENTIFIER), CAST(@GoalID AS UNIQUEIDENTIFIER), @GoalValue, @GoalDateTime)" 
Suggestions? Thanks.
Jan 4 '08 #3

P: 3
I've found the solution. This post had the answer: http://www.devnewsgroups.net/group/m...opic10857.aspx


i figuried it out. you have to do ..

"'{" & (new guid(str)).toString("D") & "}'"
That worked for me!
Jan 7 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.