Question

I'm new to programming and am having a problem with a web application I am coding in VB.net. I have an Access database in my project and am using a parameterized query to retrieve information from it. The weird thing is, I use almost the same query on another page that works fine, but this one gives me teh "No value given for one or more required parameters" error. I have been searching most of the day and realize the question has been asked but can't find the solution to my problem. I think I am using the "?" correctly, have the right number of parameters in the right order, and everything is spelled right. Here is my code:

 Dim strWthStn As String
    strWthStn = cboRainProb2.Text
    Dim strWthStnParm As New OleDb.OleDbParameter()
    strWthStnParm.Value = strWthStn

    Dim strMonth As String
    strMonth = cboRainProb.Text
    Dim strMonthParm As New OleDb.OleDbParameter()
    strMonthParm.Value = strMonth

    Dim sql As String 'Query to select the station for rainfall probability values

    sql = "SELECT 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 " &
    "FROM Rainfall Probability " &
    "WHERE Station = ?" &
    "AND Mth = ?"

    'Connects to the Access database and selects data based on query
    Dim AccessConnect As String
    AccessConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\Megan\Visual Studio Projects\DroughtCalculatorND_Web\DroughtCalculatorND_Web\NDStations.mdb"
    Dim conn As New OleDb.OleDbConnection
    Dim selectCommand As New OleDbCommand(sql, conn)
    selectCommand.Parameters.Add(strWthStnParm)
    selectCommand.Parameters.Add(strMonthParm)
    conn.ConnectionString = AccessConnect
    conn.Open()

    'Reads the data into a list
    Dim reader As OleDbDataReader =
        selectCommand.ExecuteReader(CommandBehavior.CloseConnection)
    Dim RainByYear As New List(Of Double)
    reader.Read()
    RainByYear.Add(reader("1999"))
    RainByYear.Add(reader("2000"))
    RainByYear.Add(reader("2001"))
    RainByYear.Add(reader("2002"))
    RainByYear.Add(reader("2003"))
    RainByYear.Add(reader("2004"))
    RainByYear.Add(reader("2005"))
    RainByYear.Add(reader("2006"))
    RainByYear.Add(reader("2007"))
    RainByYear.Add(reader("2008"))
    RainByYear.Add(reader("2009"))
    RainByYear.Add(reader("2010"))

    RainByYear.Sort()

etc.

The program errors at the Dim reader at OleDBDataReader line. I used a breakpoint there to check everything and both parameters had the correct value. I appreciate any help, thanks.

Was it helpful?

Solution

Names that have spaces require brackets:

"FROM [Rainfall Probability] "

Your numbered field names might also:

SELECT [1999], [2000] etc...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top