473,387 Members | 1,790 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
3 20605
Frinavale
9,735 Expert Mod 8TB
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
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
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

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

Similar topics

1
by: Ren? M?hle | last post by:
I have a psp script with a procedure just to run an update on one table. The Problem occurs when I try to compile this script with pspload: ORA-20006: "frsys_updatereport.psp": compilation...
1
by: rusttree | last post by:
I'm working on a program that manipulates bmp files. I know the offset location of each piece of relevent data within the bmp file. For example, I know the 18th through 21st byte is an integer...
2
by: jt | last post by:
Looking for an example how to convert and CString to an ASCII character string. Any examples on how to do this? Thank you, jt
0
by: MLH | last post by:
Is an apostrophe a character of special significance to MySQL in a way that would cause "Bob's dog" to become translated into a 12-character string when typed into a MySQL memo field? If I type...
7
by: Justin | last post by:
i need to build the unsigned character string: "PR0N\0Spam\0G1RLS\0Other\0Items\0\0\0" from the signed character string: "PR0N Spam G1RLS Other Items" Tokeninzing the character string is not...
5
by: Karthik | last post by:
Hello! I am not a wizard in this area! Just need some help out in this. I am trying to convert bstr string to new character string. Here is the snippet of my code. **** Code Start**** ...
25
by: lovecreatesbeauty | last post by:
Hello experts, I write a function named palindrome to determine if a character string is palindromic, and test it with some example strings. Is it suitable to add it to a company/project library...
8
by: Brand Bogard | last post by:
Does the C standard include a library function to convert an 8 bit character string to a 16 bit character string?
2
by: bipinskulkarni | last post by:
Hi, i have a field createdby with datatype GUID. In following query ,i encountered with the error "Conversion failed when converting from a character string to uniqueidentifier" select...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.