2017-07-05 14 views
0

ここには多くの人がよく尋ねてきたことは知っていますが、私はこのWebサイトで提供されているソリューションのほとんどを試していないかどうか尋ねなかったでしょう。ms-accessからVB.netアプリケーションに画像を読み込む

vb.netのピクチャボックスへのms-accessからの領収書のイメージの読み込みに問題があります。

ここに私が現在持っているコードがあります。

Dim i = DataGridView1.CurrentRow.Index 
    Dim Dir As String = Path.GetDirectoryName(Application.ExecutablePath) & "\" 
    Dim Connection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Dir & "DB1.accdb") 
    Dim SQL As String = "SELECT Receipt FROM [Inventory] WHERE [email protected]" 
    Dim command As New OleDbCommand(SQL, Connection) 
    command.Parameters.AddWithValue("@DID", DataGridView1.Item(0, i).Value) 

    Connection.Open() 
    Using oleDBCmd As OleDb.OleDbCommand = Connection.CreateCommand() 
     oleDBCmd.CommandType = CommandType.Text 
     oleDBCmd.CommandText = SQL 
     Using oleDbReader As OleDb.OleDbDataReader = oleDBCmd.ExecuteReader() 
      oleDbReader.Read() 
      Dim ImageBytes As Byte() = CType(oleDbReader(0), Byte()) 
      Dim ms As New MemoryStream(ImageBytes) 
      FormReciept.Show() 
      FormReceipt.PictureBox1.Image = Image.FromStream(ms) 
     End Using 
    End Using 
    Connection.Close() 

特定項目の選択したフィールドに基づいて、ID番号に基づいて、レシートを取得し、画像ボックスが別の形でそれをプレビュー。

ここではエラーが発生しています。

An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred 
in System.Data.dll 
Additional information: No value given for one or more required parameters. 

任意の助けいただければ幸いです。ありがとうございました。

答えて

1

パラメータを使用してコマンドを作成し、それを使用しないで、実際に実行しているパラメータが追加されていない新しいコマンド "oleDBCmd"を作成します。そのため、「値が指定されていません」というエラーが表示されます。

次のように変更してみて、あなたはまだエラーが出るかどうか、新しいエラーを取得:

Dim i = DataGridView1.CurrentRow.Index 
    Dim Dir As String = Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) & "\" 
    Dim Connection As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Dir & "DB1.accdb") 
    Dim SQL As String = "SELECT Receipt FROM [Inventory] WHERE [email protected]" 
    'Dim command As New OleDb.OleDbCommand(SQL, Connection) 
    'command.Parameters.AddWithValue("@DID", DataGridView1.Item(0, i).Value) 

    Connection.Open() 
    Using oleDBCmd As OleDb.OleDbCommand = Connection.CreateCommand() 
     oleDBCmd.CommandType = CommandType.Text 
     oleDBCmd.CommandText = SQL 
     'add parameter to the command you will actually be executing 
     oleDBCmd.Parameters.AddWithValue("@DID", DataGridView1.Item(0, i).Value) 
     Using oleDbReader As OleDb.OleDbDataReader = oleDBCmd.ExecuteReader() 
      oleDbReader.Read() 
      Dim ImageBytes As Byte() = CType(oleDbReader(0), Byte()) 
      Dim ms As New MemoryStream(ImageBytes) 
      FormReciept.Show() 
      FormReceipt.PictureBox1.Image = Image.FromStream(ms) 
     End Using 
    End Using 
    Connection.Close() 

はちょうどそれが実行されませんので、「コマンド」OLEDBコマンドをコメントアウトし、追加パラメータを「oleDBCmd」に設定します。

+0

チャームのように働いた!どうもありがとうございます。 – JoeMcDon

関連する問題