locked
Data type mismatch in criteria expression RRS feed

  • Question

  • User-2109039660 posted

    Hi,

    I need to update records using this GridView control. Basically, I would click the IsActive checkbox for it to update the record.

     

    But I get this message:-

    data type mismatch in criteria expression.

    Any ideas?

     

            <h3>
                Activate / Deactivate Users</h3>
            Sorting can be achieved by clicking on the column header</center>
        <br />
        <div align="center">
            <asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False"
                DataKeyNames="UserId" DataSourceID="AccessDataSource1" EnableModelValidation="True">
                <Columns>
                    <asp:BoundField DataField="UserId" HeaderText="UserId" SortExpression="UserId" ReadOnly="True" />
                    <asp:BoundField DataField="Comment" HeaderText="Comment" SortExpression="Comment" />
                    <asp:CheckBoxField DataField="IsApproved" HeaderText="IsApproved" SortExpression="IsApproved" />
                    <asp:CommandField ShowEditButton="True" />
                </Columns>
            </asp:GridView>
            <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/ASPNetDB.mdb"
                SelectCommand="SELECT [UserId], [Comment], [IsApproved] FROM [aspnet_Membership]"
                UpdateCommand="UPDATE [aspnet_Membership] SET [IsApproved] = ?, [Comment] = ?  WHERE [UserId] = ? ">
                <UpdateParameters>
                    <asp:Parameter Name="UserId" Type="Int32" />
                    <asp:Parameter Name="Comment" Type="String" />
                    <asp:Parameter Name="IsApproved" Type="Boolean" />
                </UpdateParameters>
            </asp:AccessDataSource>
        </div>


     

     

    Tuesday, October 12, 2010 12:39 PM

Answers

  • User-821857111 posted

    The MembershipProvider is a wrapper around what you are doing in code explicitly. The methods it exposes do the same as you are, but protect you from having to know the schema of the membership database or having problems with data type mismatch errors etc.



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 13, 2010 1:30 PM

All replies

  • User-821857111 posted

    Alter the order in which your parameters are added to the UpdateParameters collection so that it matches the order in which they appear in the SQL. OleDb parameters work on positional matching.


    Tuesday, October 12, 2010 1:49 PM
  • User-1199946673 posted

    Mike already explained what caused the error and how to solve it. But I want to point out something else. When using a membership provider, you shouldn't change the data in the membership tables directly, but you should use the membership properties and methods for that:

            Dim user As MembershipUser = Membership.GetUser()
            user.Comment = "new value"
            user.IsApproved = True
            Membership.UpdateUser(user)
    


     

    Tuesday, October 12, 2010 2:12 PM
  • User-2109039660 posted

    Thanks Mike that worked, I tested it by clicking the IsApproved checkbox for a given Username and then tried to login with that Username and was subsequently refused entry to the site. I reapplied the IsApproved checkbox and the username was allowed entry again. So on the face of it - it works.

    However, just to follow on from what Hans is saying:

    Mike already explained what caused the error and how to solve it. But I want to point out something else. When using a membership provider, you shouldn't change the data in the membership tables directly, but you should use the membership properties and methods for that:
    1. Dim user As MembershipUser = Membership.GetUser()   
    2. user.Comment = "new value"  
    3. user.IsApproved = True  
    4. Membership.UpdateUser(user)

    Could you explain further, I'm not all that experienced with ASP.NET so I could do with a bit of guidance. By the way, I'm using C#, the above example looks to be VB, not sure.

    Cheers Rob.

     

     

    Wednesday, October 13, 2010 6:41 AM
  • User-821857111 posted

    The MembershipProvider is a wrapper around what you are doing in code explicitly. The methods it exposes do the same as you are, but protect you from having to know the schema of the membership database or having problems with data type mismatch errors etc.



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 13, 2010 1:30 PM
  • User-2109039660 posted

    thanks Mike for the reply.

    Just so I understand, what you are saying is the update of the record is controlled by the method you outlined above and that I display the records in the GridView with the query? Does the method attach itself to the update button click?

    Just as a side issue that you may be able to answer, I've used the comment field to display a users name which I didn't really want to do. I have some personal information within the profile table that has Fname and Lname amongst other details. I don't know how to query that field, any ideas?

    Many thanks, 

    Rob.

     

    Thursday, October 14, 2010 4:16 AM