locked
Data type mismatch in criteria expression RRS feed

  • Question

  • User-726861298 posted

    Hi,

    I have used the repeater function in an aspx page to create a discussion/fourm style page where user can posts comments, but when I try and write a comment I get this error;  Data type mismatch in criteria expression. The user gets to the discussion page from a link in a gridview so that it displays all the posts on the specific film, and the correct posts come up, its just when a user wants to post something themsevles I get the error.

    This is the code in the discussion.aspx.vb page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me

    .Load

    Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("MK2"

    ).ConnectionString)

    oleDbConn.Open()

    If IsPostBack()

    Then

    Dim sqlStr =

    "INSERT INTO Posts([PostTitle],[PostComment],[PostUserName],[PostWhichFilm],[PostDate])VALUES(@PostTitle,@PostWhichFilm,@PostComment,@PostUserName,@PostDate)"

    Dim newCommentDbCmd As New

    OleDbCommand(sqlStr, oleDbConn)

    newCommentDbCmd.Parameters.AddWithValue(

    "@PostTitle"

    , TextBox1.Text)

    newCommentDbCmd.Parameters.AddWithValue(

    "@PostComment"

    , TextBox2.Text)

    newCommentDbCmd.Parameters.AddWithValue(

    "@PostWhichFilm", CInt(Session("FilmId"

    )))

    newCommentDbCmd.Parameters.AddWithValue(

    "@UserName"

    , User.Identity.Name)

    newCommentDbCmd.Parameters.AddWithValue(

    "@PostDate"

    , DateTime.Now.Date)

    newCommentDbCmd.ExecuteNonQuery()

    Else

    'Session("FilmId") = Request.Params("ID")

    Session(

    "FilmId") = Request.Params("field"

    )

    End

    If

    ''Displays films and comments

    'Dim FilmId = CInt(Request.Params("ID"))

    Dim FilmId = (Request.Params("field"

    ))

    Dim relevantFilmStr As String =

    "SELECT ID,F_Title FROM Films WHERE (ID)= @ID"

    Dim filmDbCmd As New

    OleDbCommand(relevantFilmStr, oleDbConn)

    filmDbCmd.Parameters.AddWithValue(

    "@ID"

    , FilmId)

    Dim

    FilmDbread = filmDbCmd.ExecuteReader()

    If FilmDbread.HasRows()

    Then

    FilmDbread.Read()

    lblFilmTitle.Text = FilmDbread(

    "F_Title"

    )

    'lblImage.Text = FilmDbread("F_Image")

    End

    If

    FilmDbread.Close()

    filmDbCmd.Dispose()

     

    Dim allPostingStr As String =

    "SELECT * FROM Posts WHERE (PostWhichFilm)= @ID"

    Dim postingDbCmd As New

    OleDbCommand(allPostingStr, oleDbConn)

    postingDbCmd.Parameters.AddWithValue(

    "@ID"

    , FilmId)

    Dim

    dbread = postingDbCmd.ExecuteReader()

    Repeater1.DataSource = dbread

    Repeater1.DataBind()

    End

    Sub

     

    Wednesday, January 12, 2011 2:32 AM

Answers

  • User1867929564 posted

    once you should hard code the value and try.is it working ok ?
    then prperly implement Session or queryString

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 14, 2011 5:46 AM

All replies

  • User-821857111 posted

    I assume this an Access database? If so, the problem is with the PostDate, which needs to be converted to another format to be accepatable as an Access DateTime type: http://www.mikesdotnetting.com/Article/92/MS-Access-Date-and-Time-with-ASP.NET.

    You can either pass in

    DateTime.Now.ToString()

    or
    Datetime.Now.ToOADate()




    Wednesday, January 12, 2011 2:44 AM
  • User-726861298 posted

    Yes I'm using Access.

    I tried both of the waya that you posted, but I still get the same error :(

    I don't know if this helps but its this line  "newCommentDbCmd.ExecuteNonQuery()"

    that highlitghs with the error, however I saw that the values for Textbox1 and TextBox 2 are getting passed but when it comes to this line

    newCommentDbCmd.Parameters.AddWithValue("@PostWhichFilm", CInt(Session("FilmId")))

    the value is empty.

    Wednesday, January 12, 2011 3:22 AM
  • User-1199946673 posted

    that highlitghs with the error, however I saw that the values for Textbox1 and TextBox 2 are getting passed but when it comes to this line

    newCommentDbCmd.Parameters.AddWithValue("@PostWhichFilm", CInt(Session("FilmId")))

    the value is empty.

     

    So most likely Session("FilmId") doesn't have the right value?

    Wednesday, January 12, 2011 5:11 AM
  • User-726861298 posted

    well its meant to pass the film name from the link that a user would have clicked on, and this does come up, its just when the user clciks on the post button the error comes up. But yes most likely its from the PostWhichFilm.

    In access this is a look up field, so would this have an affect? 

    Wednesday, January 12, 2011 5:56 AM
  • User1867929564 posted

    once you should hard code the value and try.is it working ok ?
    then prperly implement Session or queryString

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 14, 2011 5:46 AM