Results 1 to 7 of 7

Thread: problem in oledb.connection

  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

  2. #2
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    Re: how to solve this error

    Hi,

    Also, you are being far too vague for anyone to help. I have not looked at your other threads but would try to make some helpful comments:

    1. If you feel your last post is being ignored or missed, just add another brief post to it saying something like "Anyone please?" or simply "Bump"

    2. Problems with database connections are usually errors in the connection string (that assumes you have correctly created the instance). Check your path - make sure the database is where you think it is - and any passwords. But if you have got as far as your first error then your conn must be working.

    3. "An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in system.data.dll" often indicates a problem with field naming or data type conflict. e.g.

    Field Naming.

    a) VB.NET will only allow field names to contain alpha-numeric characters or the underscore ( _ ), even though Access will permit other characters such as spaces, brackets, some arithmetic signs, etc. Always begin your field names with an alphabetic character or an underscore - not a number

    b) Make sure your field names are not reserved words, in either VB.NET or Access (Assuming your database is Access). Either use a prefix e.g. sName instead of Name or place the reserved word in square brackets [ ] in your query.

    Data Type Conflict

    Make sure the data from VB.NEt is the same type as the Database field.


    Always use a Try....catch block around database code because it will give you the exact cause of the error.

    If you don't post your code AND indicate the line giving rise to the error, that's the best I can do.
    Last edited by taxes; Oct 21st, 2005 at 04:26 AM.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: how to solve this error

    Quote Originally Posted by taxes
    ...but would try to make some helpful comments
    Is that a dig at me or have I just got a guilty conscience?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    Re: how to solve this error

    Quote Originally Posted by jmcilhinney
    Is that a dig at me or have I just got a guilty conscience?

    Hi,

    NO WAY I have great respect for your input. Also, I've just looked at his other threads and understand your comments. You ARE trying to be helpful to him and guide him for future posts.

    No, my reference to trying to be helpful was solely related to my lack of knowledge added to his lack of information.

    Having looked at his posts, I see he is using Oracle ( about which I know nothing) but I thought Microsoft Jet.4 .... was only used for Access databases.

    Keep posting please.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  5. #5
    Frenzied Member Asgorath's Avatar
    Join Date
    Sep 2004
    Location
    Saturn
    Posts
    2,036

    Re: problem in oledb.connection

    You connection string seems to be incomplete http://www.connectionstrings.com/.
    Put a try catch block to get a precise error message.

    Regards
    Jorge
    "The dark side clouds everything. Impossible to see the future is."

  6. #6
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: this is the coding i tried

    This is the only post I am going to give you, since you don't seem to be following directions for the multiple threads. For 1 thing you need a SELECT command in order to select your values you need from the database, not an INSERT command, that's just basic SQL knowledge (although that INSERT statement might be for when you add records)

    As for the rest, there is a help topic you should look up, it is titled "Accessing Data With ADO.NET" in the Help file, or MSDN would have it if you did not install the Help. It has plenty of info there WITH EXAMPLES... you need to start researching the material you are working on instead of trying to throw together items from different sources without knowing what they do...

  7. #7
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: this is the coding i tried

    Ooohh. Please don't create a new thread for the continuation of our discussion just click the post reply button and not the new thread button.

    Ok I try to figure out your code.
    VB Code:
    1. Imports System.Data.OleDb
    2.  
    3. Public Class Form1
    4. Inherits System.Windows.Forms.Form
    5.  
    6. 'Declare your connection globally
    7. Dim cn as New OleDbConnection(strConn)
    8.  
    9. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    10. Dim cmd As New OleDbCommand("INSERT INTO users (userid, password)VALUES('" & txtuserid.Text & "','" & txtpassword.Text & "')", cn)
    11. cn.Open()
    12. cmd.ExecuteNonQuery()
    13. cn.Close()
    14.  
    15. End Sub
    16. End Class

    Btw what is Repeater1? Is it a datagrid?

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