Results 1 to 7 of 7

Thread: problem in oledb.connection

Threaded View

  1. #1

    Thread Starter
    Fanatic Member karthikeyan's Avatar
    Join Date
    Oct 2005
    Location
    inside .net
    Posts
    919

    problem in oledb.connection

    hi
    at present iam working in oracle forms
    in my form there is one text bix,4 command buttons,and one picture box.the name of the buttons are browse image,read image,save image,usedatareader.i am using acess as a database,if i clilck browse image using this i can get an image and if i click save image it has to be save in my database,if i click read image the image has to be shown in the picture box.
    in my acess table the folllowing feilds are there
    userid autonumber
    username text
    userphoto oleobject
    the followind code i did

    VB Code:
    1. Imports System.Data.OleDb
    2. Imports System.IO
    3.  
    4. Public Class Form1
    5.     Inherits System.Windows.Forms.Form
    6.     Private curImage As Image = Nothing
    7.     Private curFileName As String = Nothing
    8.     Private connectionString As String = _
    9.       "Provider=Microsoft.Jet.OLEDB.4.0; " & _
    10.       "Data Source=C:\AppliedAdoNet.mdb"
    11.     Private savedImageName As String _
    12.       = "C:\\ImageFromDb.BMP"
    13.  
    14.  
    15. #Region " Windows Form Designer generated code "
    16.  
    17.     Public Sub New()
    18.         MyBase.New()
    19.  
    20.         'This call is required by the Windows Form Designer.
    21.         InitializeComponent()
    22.  
    23.         'Add any initialization after the InitializeComponent() call
    24.  
    25.     End Sub
    26.  
    27.     'Form overrides dispose to clean up the component list.
    28.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    29.         If disposing Then
    30.             If Not (components Is Nothing) Then
    31.                 components.Dispose()
    32.             End If
    33.         End If
    34.         MyBase.Dispose(disposing)
    35.     End Sub
    36.  
    37.     'Required by the Windows Form Designer
    38.     Private components As System.ComponentModel.IContainer
    39.  
    40.     'NOTE: The following procedure is required by the Windows Form Designer
    41.     'It can be modified using the Windows Form Designer.  
    42.     'Do not modify it using the code editor.
    43.     Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    44.     Friend WithEvents Button1 As System.Windows.Forms.Button
    45.     Friend WithEvents Button2 As System.Windows.Forms.Button
    46.     Friend WithEvents Button3 As System.Windows.Forms.Button
    47.     Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
    48.     Friend WithEvents Button4 As System.Windows.Forms.Button
    49.     Friend WithEvents DB2DataAdapter1 As DDTek.DB2.DB2DataAdapter
    50.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    51.         Me.TextBox1 = New System.Windows.Forms.TextBox
    52.         Me.Button1 = New System.Windows.Forms.Button
    53.         Me.Button2 = New System.Windows.Forms.Button
    54.         Me.Button3 = New System.Windows.Forms.Button
    55.         Me.PictureBox1 = New System.Windows.Forms.PictureBox
    56.         Me.Button4 = New System.Windows.Forms.Button
    57.         Me.DB2DataAdapter1 = New DDTek.DB2.DB2DataAdapter
    58.         Me.SuspendLayout()
    59.         '
    60.         'TextBox1
    61.         '
    62.         Me.TextBox1.Location = New System.Drawing.Point(40, 48)
    63.         Me.TextBox1.Name = "TextBox1"
    64.         Me.TextBox1.Size = New System.Drawing.Size(168, 20)
    65.         Me.TextBox1.TabIndex = 0
    66.         Me.TextBox1.Text = "TextBox1"
    67.         '
    68.         'Button1
    69.         '
    70.         Me.Button1.Location = New System.Drawing.Point(264, 40)
    71.         Me.Button1.Name = "Button1"
    72.         Me.Button1.Size = New System.Drawing.Size(152, 40)
    73.         Me.Button1.TabIndex = 1
    74.         Me.Button1.Text = "browse image"
    75.         '
    76.         'Button2
    77.         '
    78.         Me.Button2.Location = New System.Drawing.Point(40, 136)
    79.         Me.Button2.Name = "Button2"
    80.         Me.Button2.Size = New System.Drawing.Size(136, 40)
    81.         Me.Button2.TabIndex = 2
    82.         Me.Button2.Text = "save image"
    83.         '
    84.         'Button3
    85.         '
    86.         Me.Button3.Location = New System.Drawing.Point(256, 136)
    87.         Me.Button3.Name = "Button3"
    88.         Me.Button3.Size = New System.Drawing.Size(152, 40)
    89.         Me.Button3.TabIndex = 3
    90.         Me.Button3.Text = "read image"
    91.         '
    92.         'PictureBox1
    93.         '
    94.         Me.PictureBox1.Location = New System.Drawing.Point(8, 184)
    95.         Me.PictureBox1.Name = "PictureBox1"
    96.         Me.PictureBox1.Size = New System.Drawing.Size(744, 352)
    97.         Me.PictureBox1.TabIndex = 4
    98.         Me.PictureBox1.TabStop = False
    99.         '
    100.         'Button4
    101.         '
    102.         Me.Button4.Location = New System.Drawing.Point(472, 48)
    103.         Me.Button4.Name = "Button4"
    104.         Me.Button4.Size = New System.Drawing.Size(176, 32)
    105.         Me.Button4.TabIndex = 5
    106.         Me.Button4.Text = "usedatareader"
    107.         '
    108.         'DB2DataAdapter1
    109.         '
    110.         Me.DB2DataAdapter1.DeleteCommand = Nothing
    111.         Me.DB2DataAdapter1.InsertCommand = Nothing
    112.         Me.DB2DataAdapter1.SelectCommand = Nothing
    113.         Me.DB2DataAdapter1.UpdateCommand = Nothing
    114.         '
    115.         'Form1
    116.         '
    117.         Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    118.         Me.ClientSize = New System.Drawing.Size(760, 534)
    119.         Me.Controls.Add(Me.Button4)
    120.         Me.Controls.Add(Me.PictureBox1)
    121.         Me.Controls.Add(Me.Button3)
    122.         Me.Controls.Add(Me.Button2)
    123.         Me.Controls.Add(Me.Button1)
    124.         Me.Controls.Add(Me.TextBox1)
    125.         Me.Name = "Form1"
    126.         Me.Text = "Form1"
    127.         Me.ResumeLayout(False)
    128.  
    129.     End Sub
    130.  
    131. #End Region
    132.  
    133.  
    134.     Private Sub BrowseBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    135.         Dim openDlg As OpenFileDialog = New OpenFileDialog
    136.         openDlg.Filter = "All Bitmap files|*.bmp"
    137.         Dim filter As String = openDlg.Filter
    138.         openDlg.Title = "Open a Bitmap File"
    139.         If (openDlg.ShowDialog() = DialogResult.OK) Then
    140.             curFileName = openDlg.FileName
    141.             TextBox1.Text = curFileName
    142.         End If
    143.     End Sub
    144.  
    145.     Private Sub SaveImageBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    146.  
    147.         If TextBox1.Text Is String.Empty Then
    148.             MessageBox.Show("Browse a bitmap")
    149.             Return
    150.         End If
    151.         ' Read a bitmap contents in a stream
    152.         Dim fs As FileStream = New FileStream(curFileName, FileMode.OpenOrCreate, FileAccess.Read)
    153.  
    154.         Dim rawData() As Byte = New Byte(fs.Length) {}
    155.         fs.Read(rawData, 0, System.Convert.ToInt32(fs.Length))
    156.         fs.Close()
    157.         ' Construct a SQL string and a connection object
    158.         Dim sql As String = "SELECT * FROM user"
    159.         Dim conn As OleDbConnection = New OleDbConnection
    160.         conn.ConnectionString = connectionString
    161.         ' Open connection
    162.         If conn.State <> ConnectionState.Open Then
    163.             conn.Open()
    164.         End If
    165.         ' Create a data adapter and data set
    166.         Dim adapter As OleDbDataAdapter = New OleDbDataAdapter(sql, conn)
    167.         Dim cmdBuilder As OleDbCommandBuilder = New OleDbCommandBuilder(adapter)
    168.         Dim ds As DataSet = New DataSet("user")
    169.         adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
    170.  
    171.         ' Fill data adapter
    172.         adapter.Fill(ds, "user")
    173.  
    174.         Dim userDes As String = "Mahesh Chand is a founder of C# Corner "
    175.         userDes += "Author: 1. A Programmer's Guide to ADO.NET;"
    176.         userDes += ", 2. Applied ADO.NET. "
    177.  
    178.         ' Create a new row
    179.         Dim row As DataRow = ds.Tables("user").NewRow()
    180.         row("userid") = "100"
    181.         row("UserName") = "karthikeyan"
    182.         row("UserPhoto") = rawData
    183.         ' Add row to the collection
    184.         ds.Tables("user").Rows.Add(row)
    185.         ' Save changes to the database
    186.         adapter.Update(ds, "user")
    187.         ' Clean up connection
    188.         If conn Is Nothing Then
    189.             If conn.State = ConnectionState.Open Then
    190.                 conn.Close()
    191.             End If
    192.             ' Dispose connection
    193.             conn.Dispose()
    194.         End If
    195.         MessageBox.Show("Image Saved")
    196.     End Sub
    197.  
    198.  
    199.  
    200.     Private Sub button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    201.  
    202.         ' Construct a SQL string and a connection object
    203.         Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; " & "Data Source=C:\AppliedAdoNet.mdb"
    204.         Dim sql As String = "SELECT userPhoto FROM user"
    205.         Dim conn As OleDbConnection = New OleDbConnection
    206.         conn.ConnectionString = connectionString
    207.         ' Open connection
    208.         'If conn.State <> ConnectionState.Open Then
    209.         conn.Open()
    210.         'End If
    211.  
    212.         Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
    213.         Dim fs As FileStream
    214.         Dim bw As BinaryWriter
    215.         Dim bufferSize As Integer = 300000
    216.         Dim outbyte(300000 - 1) As Byte
    217.         Dim retval As Long
    218.         Dim startIndex As Long = 0
    219.         Dim pub_id As String = ""
    220.         Dim reader As OleDbDataReader = _
    221.             cmd.ExecuteReader(CommandBehavior.SequentialAccess)
    222.         ' Read first record
    223.         reader.Read()
    224.         fs = New FileStream(savedImageName, FileMode.OpenOrCreate, FileAccess.Write)
    225.         bw = New BinaryWriter(fs)
    226.         startIndex = 0
    227.         retval = reader.GetBytes(0, 0, outbyte, 0, bufferSize)
    228.         bw.Write(outbyte)
    229.         bw.Flush()
    230.         ' Close the output file.
    231.         bw.Close()
    232.         fs.Close()
    233.         reader.Close()
    234.         ' Display image
    235.         curImage = Image.FromFile(savedImageName)
    236.         PictureBox1.Image = curImage
    237.         PictureBox1.Invalidate()
    238.         ' Clean up connection
    239.         If conn.State = ConnectionState.Open Then
    240.             conn.Close()
    241.             ' Dispose connection
    242.             conn.Dispose()
    243.         End If
    244.  
    245.  
    246.     End Sub
    247. End Class

    after compiling i got succeed,but if i browse the image and try to save that using the button i am getting the following error.

    "An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in system.data.dll"
    also problem comming in "conn.open"

    so what to do.
    Last edited by RobDog888; Oct 25th, 2005 at 01:44 PM. Reason: Added [vbcode] tags

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width