tate Posted August 22, 2005 Posted August 22, 2005 I'm having a problem using a parameter as part of the SELECT statement. Can someone please help me to understand why I get the following error; "The OleDbParameterCollection only accepts non-null OleDbParameter type objects, not String objects" Thanks in advance for the help. *********** Here is the code ******************************* Dim daStudy As New OleDbDataAdapter("SELECT * FROM tblStudy WHERE StudyID = @StudyID", conSmallGrp) Dim dsStudy As New DataSet Me.ShowStudy(ddlStudy.SelectedItem.Value) Private Sub ShowStudy(ByVal StudyID As Integer) daStudy.SelectCommand.Parameters.Add("@StudyID") daStudy.SelectCommand.Parameters("@StudyID").Value = StudyID ' Fill dataset that will be used to populate text boxes daStudy.Fill(dsStudy, "tblStudy") txtStudyID = dsStudy.Tables("tblStudy").Rows(0).Item("StudyID") txtCategory = dsStudy.Tables("tblStudy").Rows(0).Item("CategoryID") txtBookTitle = dsStudy.Tables("tblStudy").Rows(0).Item("Book") txtAuthor = dsStudy.Tables("tblStudy").Rows(0).Item("Author") txtISBN = dsStudy.Tables("tblStudy").Rows(0).Item("Isbn") txtSummary = dsStudy.Tables("tblStudy").Rows(0).Item("Summary") End Sub Quote
Joe Mamma Posted August 22, 2005 Posted August 22, 2005 Oledb doesnt support named parameters in the command text - you can name them as you set them. try this: [indent]Dim daStudy As New OleDbDataAdapter("SELECT * FROM tblStudy WHERE StudyID = ?", conSmallGrp) Dim dsStudy As New DataSet Me.ShowStudy(ddlStudy.SelectedItem.Value) [/indent] Private Sub ShowStudy(ByVal StudyID As Integer) [indent]daStudy.SelectCommand.Parameters.Add("StudyID", StudyID) daStudy.Fill(dsStudy, "tblStudy") txtStudyID = dsStudy.Tables("tblStudy").Rows(0).Item("StudyID") txtCategory = dsStudy.Tables("tblStudy").Rows(0).Item("CategoryID") txtBookTitle = dsStudy.Tables("tblStudy").Rows(0).Item("Book") txtAuthor = dsStudy.Tables("tblStudy").Rows(0).Item("Author") txtISBN = dsStudy.Tables("tblStudy").Rows(0).Item("Isbn") txtSummary = dsStudy.Tables("tblStudy").Rows(0).Item("Summary") [/indent] End Sub Quote Joe Mamma Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized. Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.
tate Posted August 22, 2005 Author Posted August 22, 2005 Thank you for your reply. However, after making the change you requested I now receive the following error; "Specified cast is not valid" Which looks to be in the positive direction but now it appears their is a problem with the MS Access data type for the index value. I thought the auto-number field would be an integer. By the way, this is the same error received if I use the original code presented with 1 change; daStudy.SelectCommand.Parameters.Add("@StudyID") changed to daStudy.SelectCommand.Parameters.Add("@StudyID", OleDbType.Integer) Quote
cpopham Posted August 22, 2005 Posted August 22, 2005 The Autonumber field of access uses Long Integer as the data type I believe. Chester Quote ____________________________________________ http://www.pophamcafe.com I am starting a developers section, more tutorials than anything.
tate Posted August 23, 2005 Author Posted August 23, 2005 I finally figured it out so the code is posted below for all to see how you can use parameterized queries in MS Access. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load If Not IsPostBack Then ' Create data adapter and dataset for ddlStudy then fill Dim daStudyList As New OleDbDataAdapter("SELECT StudyID, Study FROM tblStudy", conSmallGrp) Dim dsStudyList As New DataSet Try ' Open connection conSmallGrp.Open() ' Fill dataset used to populate ddlStudy daStudyList.Fill(dsStudyList, "tblStudyList") ' Populate ddlStudy ddlStudy.DataSource = dsStudyList.Tables("tblStudyList") ddlStudy.DataValueField = "StudyID" ddlStudy.DataTextField = "Study" ddlStudy.DataBind() ' If there is data fill text boxes If dsStudyList.Tables("tblStudyList").Rows.Count > 0 Then Me.ShowStudy(ddlStudy.SelectedItem.Value) End If Catch ex As Exception lblTest.Text = ex.Message Finally conSmallGrp.Close() End Try End If End Sub Private Sub ShowStudy(ByVal StudyID As Integer) ' Create data adapter and dataset for text boxes then fill the dataset Dim daStudy As New OleDbDataAdapter("SELECT * FROM tblStudy WHERE StudyID = @StudyID", conSmallGrp) Dim dsStudy As New DataSet daStudy.SelectCommand.Parameters.Add("@StudyID", OleDbType.Integer) daStudy.SelectCommand.Parameters("@StudyID").Value = StudyID ' Fill dataset that will be used to populate text boxes daStudy.Fill(dsStudy, "tblStudy") txtStudyID.Text = CStr(dsStudy.Tables("tblStudy").Rows(0).Item("StudyID")) txtCategory.Text = CStr(dsStudy.Tables("tblStudy").Rows(0).Item("CategoryID")) txtBookTitle.Text = dsStudy.Tables("tblStudy").Rows(0).Item("Book") txtAuthor.Text = dsStudy.Tables("tblStudy").Rows(0).Item("Author") txtISBN.Text = dsStudy.Tables("tblStudy").Rows(0).Item("Isbn") txtSummary.Text = dsStudy.Tables("tblStudy").Rows(0).Item("Summary") End Sub Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.