2017-03-22 9 views
0

Button1,2,3をクリックしようとすると、以下のエラーが発生します。このエラーは、データベースに2つのテーブルがある場合に常に発生します。このエラーはなぜ発生しますか? "1つまたは複数の必須パラメーターに値が指定されていません"

一つ以上の必要なパラメータ

に指定された値がありません。これは私のコード

ボタンのクリックハンドラに接続文字列を渡しているところ、私は表示されません
Imports System.Data.OleDb 
Module MSAccessConnection 
    Public Function OpenConnection() As String 
     Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=G:\MerdoresNew\OrderData.accdb" 

     Return connString 
    End Function 
End Module 


Public Class Order 
    Public total As Integer 
    Public ordered As Integer 
    Public ordered2 As Integer 

    Public ordered3 As Integer 
    Public price As Integer 
    Dim myConnection As OleDbConnection = New OleDbConnection 


    Private Sub Order_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
     'TODO: This line of code loads data into the 'OrderDatabaseDataSet.Items' table. You can move, or remove it, as needed. 
    End Sub 

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged 

    End Sub 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
     myConnection.ConnectionString = OpenConnection() 
     myConnection.Open() 

     'the query: 
     Dim cmd As OleDbCommand = New OleDbCommand("SELECT [Price] FROM [Items] where Item='Cheese Burger'", myConnection) 

     Dim dr As OleDbDataReader = cmd.ExecuteReader 
     ' the following variable is hold true if user is found, and false if user is not found 
     Dim Found As Boolean = False 

     While dr.Read 
      Found = True 
      price = dr("Price") 
     End While 

     If Found = True Then 


      ordered = TextBox1.Text * price 



     Else 





      MsgBox("Saved!") 

      Me.Close() 


     End If 

     myConnection.Close() 

    End Sub 

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 
     myConnection.ConnectionString = OpenConnection() 
     myConnection.Open() 

     'the query: 
     Dim cmd As OleDbCommand = New OleDbCommand("SELECT [Price] FROM [Items] where Item='Ham Burger'", myConnection) 

     Dim dr As OleDbDataReader = cmd.ExecuteReader 
     ' the following variable is hold true if user is found, and false if user is not found 
     Dim Found As Boolean = False 

     While dr.Read 
      Found = True 
      price = dr("Price") 
     End While 

     If Found = True Then 


      ordered2 = TextBox2.Text * price 




     Else 








      MsgBox("Saved!") 


     End If 

     myConnection.Close() 
    End Sub 

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click 

     total = ordered + ordered2 + ordered3 
     Form1.TextBox1.Text = total 





    End Sub 

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click 
     myConnection.ConnectionString = OpenConnection() 
     myConnection.Open() 

     'the query: 
     Dim cmd As OleDbCommand = New OleDbCommand("SELECT [Price] FROM [Items] where Item='Chicken Sandwich'", myConnection) 

     Dim dr As OleDbDataReader = cmd.ExecuteReader 
     ' the following variable is hold true if user is found, and false if user is not found 
     Dim Found As Boolean = False 

     While dr.Read 
      Found = True 
      price = dr("Price") 
     End While 

     If Found = True Then 


      ordered3 = TextBox3.Text * price 




     Else 


      MsgBox("Saved!") 

     End If 

     myConnection.Close() 
    End Sub 
End Class 
+2

コードを正しくフォーマットするのに時間がかかりますか?そしてあなたの質問に付随する簡潔なコードを提供しますか?私はいくつかのルーチンがすべてデータベースと通信しているのを見ています。確かにあなたの問題を実証するには1つのルーチンで十分ですか? –

+1

[mcve]を入力してください。これは現在のところデバッグに関する質問ですが、コードの負荷を除けば、あまり進んではいけません。 – Bugs

+0

コード内の列名とテーブル名がデータベース内で同じであることを確認します。また、[Option Strict On](https://msdn.microsoft.com/en-us/library/zcd4xwzs.aspx)を使用すると、Visual Studioはいくつかの問題を指摘し、いくつかの修正を提案します。 –

答えて

-1

です。 connStringとして接続文字列をDIMしますが、ハンドラでは決して呼び出しません。

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
    myConnection.ConnectionString = OpenConnection() <----- Change ConnectionString to connString 
    myConnection.Open() 

私はあなたが欠けている変数だと思います。私はコメントしただろうが、まだコメントするのに十分な担当者がいない。

+1

OPはOpenConnection()関数を使用して接続文字列を取得しています。 'connString'はそれを使用することを提案している範囲にはありませんが、' connString'は、とにかくOleDbConnectionのプロパティではありません。 –

+0

私はOleDbConnectionの部分を見逃して、SQLタグを見ただけです。私の悪い。 –

関連する問題