Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implicit conversion from data type sql_variant to uniqueidentifier is not allowed. Use the CONVERT function to run this query

I am creating a website now using Microsoft Visual Web Developer 2010 Express and I used the toolbox of "createUserWizard". Then I put the userId for my "CustomerInfo" table to the "data type=uniqueidentifier" because I need to link it to the user name in the aspnet_ table.

Later on, I need to link my "Order" table to the "CustomerInfo" table so I put my orderId data type=uniqueidentifier. Then, I plan insert my order details to the "Order" table but I have the problem of:

"Implicit conversion from data type sql_variant to uniqueidentifier is not allowed. Use the CONVERT function to run this query".

I search and find some answer like datatype of the parameter set to "Empty"or delete it. but then I have this error

" Conversion failed when converting from character string to uniqueidentifier."

This is my SQL

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
                                    DeleteCommand="DELETE FROM [Order] WHERE [orderId] = @orderId" 
                                    InsertCommand="INSERT INTO [Order] ([orderId], [userId], [Services], [PickUpDate], [PickUpTime], [SpecialDate], [SpecialTime]) VALUES (@orderId, @userId, @Services, @PickUpDate, @PickUpTime, @SpecialDate, @SpecialTime)" 
                                    SelectCommand="SELECT * FROM [Order]" 

                                    UpdateCommand="UPDATE [Order] SET [userId] = @userId, [Services] = @Services, [PickUpDate] = @PickUpDate, [PickUpTime] = @PickUpTime, [SpecialDate] = @SpecialDate, [SpecialTime] = @SpecialTime WHERE [orderId] = @orderId">
                                    <DeleteParameters>
                                        <asp:Parameter Name="orderId" Type="Object" />
                                    </DeleteParameters>
                                    <InsertParameters>
                                    <asp:Parameter Name="orderId" Type="Object" />
                                    <asp:Parameter Name="userId" Type="Object" />
                                        <asp:Parameter Name="Services" Type="String" />
                                        <asp:Parameter Name="PickUpDate" Type="String" />
                                        <asp:Parameter Name="PickUpTime" Type="String" />
                                        <asp:Parameter Name="SpecialDate" Type="String" />
                                        <asp:Parameter Name="SpecialTime" Type="String" />
                                    </InsertParameters>
                                    <UpdateParameters>
                                        <asp:Parameter Name="userId" Type="Object" />
                                        <asp:Parameter Name="Services" Type="String" />
                                        <asp:Parameter Name="PickUpDate" Type="String" />
                                        <asp:Parameter Name="PickUpTime" Type="String" />
                                        <asp:Parameter Name="SpecialDate" Type="String" />
                                        <asp:Parameter Name="SpecialTime" Type="String" />
                                        <asp:Parameter Name="orderId" Type="Object" />
                                    </UpdateParameters>
                                </asp:SqlDataSource>

Now that I think of it, maybe there is a problem with my code in vb. I will put it here, please tell me how to do the correct way of inserting data to the database.

Protected Sub OrderButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles OrderButton.Click
    SqlDataSource1.InsertParameters(0).DefaultValue = Now
    SqlDataSource1.Insert()
    Response.Redirect("~/Customer/AfterOrder.aspx")
End Sub
like image 929
user815266 Avatar asked Jun 25 '11 15:06

user815266


1 Answers

If you want to insert GUID from your code, you should make parameter type GUID

<asp:Parameter Name="ParamName" DbType="Guid" />

And you should pass GUID value as (c#):

SqlDataSource1.InsertParameters["ParamName"].DefaultValue = System.Guid.NewGuid().ToString();

If you want to pass a string value, you have to use CONVERT(UNIQUEIDENTIFIER,'YOUR VALUE') in the insert query like this:

InsertCommand="INSERT INTO [Order] ([UserId], [etc]) 
VALUES (CONVERT(UNIQUEIDENTIFIER,@UserId), @etc)" 
like image 159
Kirill Avatar answered Nov 15 '22 03:11

Kirill