Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hi All,

I have intermediate knowledge in VB.Net coding. I have searched the web to find help with
my issue but to no avail. I mean I can't find one that is helpful though there are
some in Web based applications.
I have an urgent issue where I want to store some resumes into a MS SQL Server 2008 DB. I just want to create a windows form application which will have some buttons to upload the resumes into the DB and safe them, and retrive the specific resume from the DB when the name of the person is selected from a combobox, which will contan all the names of the people who's resumes are in the DB.

Senario:
MS SQL Server 2008 DB table will have 2 fields: NameOfResumeOwner, Resume
Windows Form: Will have 2 textboxes and 2 Buttons:
-2 textboxes to hold the ResumeOwner's Name and the Resume to be uploaded
-2 Buttons; 1 to upload/insert data into the DB and the other to Retrive the Resume for editing in MS Word.

my following code for the insertion of the MS Word doc into the DB works fine by inserting the image data but when I want to retrive the data as MS Word doc its not working. Giving the error as Access denied. I have given the necessary permissions to get the MS Word doc to be saved in the location as specified but to no avail. Please assist where possible. (Note: This is Windows Form application and not Web Form Applications as in ASP.Net, so don't point me to ASP.Net sites.)

Imports System.IO
Imports System.Data.SqlClient
'Imports System.Security.Permissions.FileIOPermission
Imports System.Security.Permissions
Imports System.Security

Public Class ImageDemo
    Inherits System.Windows.Forms.Form
    'Inherits CodeAccessPermission
    ''Implements IUnrestrictedPermission

#Region " Windows Form Designer generated code "
    Public Sub New()
        MyBase.New()
        'This call is required by the Windows Form Designer.
        InitializeComponent()
        'Add any initialization after the InitializeComponent() call
    End Sub
    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub
    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer
    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Panel1 As System.Windows.Forms.Panel
    Friend WithEvents btnDownload As System.Windows.Forms.Button
    Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
    Friend WithEvents btnUpload As System.Windows.Forms.Button
    Friend WithEvents txtImg As System.Windows.Forms.TextBox
    Friend WithEvents lblImg As System.Windows.Forms.Label
    Friend WithEvents lblImage As System.Windows.Forms.Label
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents SaveFileDialog1 As System.Windows.Forms.SaveFileDialog
    Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
    Friend WithEvents btnMSWOrd As System.Windows.Forms.Button
    Friend WithEvents btnseltfile As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Panel1 = New System.Windows.Forms.Panel
        Me.btnMSWOrd = New System.Windows.Forms.Button
        Me.btnseltfile = New System.Windows.Forms.Button
        Me.btnDownload = New System.Windows.Forms.Button
        Me.PictureBox1 = New System.Windows.Forms.PictureBox
        Me.btnUpload = New System.Windows.Forms.Button
        Me.txtImg = New System.Windows.Forms.TextBox
        Me.lblImg = New System.Windows.Forms.Label
        Me.lblImage = New System.Windows.Forms.Label
        Me.Label1 = New System.Windows.Forms.Label
        Me.SaveFileDialog1 = New System.Windows.Forms.SaveFileDialog
        Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
        Me.Panel1.SuspendLayout()
        CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'Panel1
        '
        Me.Panel1.Controls.Add(Me.btnMSWOrd)
        Me.Panel1.Controls.Add(Me.btnseltfile)
        Me.Panel1.Controls.Add(Me.btnDownload)
        Me.Panel1.Controls.Add(Me.PictureBox1)
        Me.Panel1.Controls.Add(Me.btnUpload)
        Me.Panel1.Controls.Add(Me.txtImg)
        Me.Panel1.Controls.Add(Me.lblImg)
        Me.Panel1.Controls.Add(Me.lblImage)
        Me.Panel1.Location = New System.Drawing.Point(9, 42)
        Me.Panel1.Name = "Panel1"
        Me.Panel1.Size = New System.Drawing.Size(352, 184)
        Me.Panel1.TabIndex = 7
        '
        'btnMSWOrd
        '
        Me.btnMSWOrd.Location = New System.Drawing.Point(246, 135)
        Me.btnMSWOrd.Name = "btnMSWOrd"
        Me.btnMSWOrd.Size = New System.Drawing.Size(75, 23)
        Me.btnMSWOrd.TabIndex = 6
        Me.btnMSWOrd.Text = "MS WORD"
        Me.btnMSWOrd.UseVisualStyleBackColor = True
        '
        'btnseltfile
        '
        Me.btnseltfile.Location = New System.Drawing.Point(304, 16)
        Me.btnseltfile.Name = "btnseltfile"
        Me.btnseltfile.Size = New System.Drawing.Size(32, 23)
        Me.btnseltfile.TabIndex = 5
        Me.btnseltfile.Text = "..."
        '
        'btnDownload
        '
        Me.btnDownload.Font = New System.Drawing.Font("Bookman Old Style", 9.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.btnDownload.Location = New System.Drawing.Point(248, 80)
        Me.btnDownload.Name = "btnDownload"
        Me.btnDownload.Size = New System.Drawing.Size(75, 23)
        Me.btnDownload.TabIndex = 4
        Me.btnDownload.Text = "Download"
        '
        'PictureBox1
        '
        Me.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.PictureBox1.Location = New System.Drawing.Point(64, 48)
        Me.PictureBox1.Name = "PictureBox1"
        Me.PictureBox1.Size = New System.Drawing.Size(176, 120)
        Me.PictureBox1.TabIndex = 3
        Me.PictureBox1.TabStop = False
        '
        'btnUpload
        '
        Me.btnUpload.Font = New System.Drawing.Font("Bookman Old Style", 9.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.btnUpload.Location = New System.Drawing.Point(248, 48)
        Me.btnUpload.Name = "btnUpload"
        Me.btnUpload.Size = New System.Drawing.Size(75, 23)
        Me.btnUpload.TabIndex = 1
        Me.btnUpload.Text = "Upload"
        '
        'txtImg
        '
        Me.txtImg.Location = New System.Drawing.Point(112, 16)
        Me.txtImg.Name = "txtImg"
        Me.txtImg.Size = New System.Drawing.Size(192, 20)
        Me.txtImg.TabIndex = 0
        '
        'lblImg
        '
        Me.lblImg.AutoSize = True
        Me.lblImg.Font = New System.Drawing.Font("Bookman Old Style", 9.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.lblImg.Location = New System.Drawing.Point(16, 16)
        Me.lblImg.Name = "lblImg"
        Me.lblImg.Size = New System.Drawing.Size(78, 14)
        Me.lblImg.TabIndex = 2
        Me.lblImg.Text = "Image Path"
        Me.lblImg.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'lblImage
        '
        Me.lblImage.AutoSize = True
        Me.lblImage.Font = New System.Drawing.Font("Bookman Old Style", 9.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.lblImage.Location = New System.Drawing.Point(16, 48)
        Me.lblImage.Name = "lblImage"
        Me.lblImage.Size = New System.Drawing.Size(45, 14)
        Me.lblImage.TabIndex = 4
        Me.lblImage.Text = "Image"
        Me.lblImage.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'Label1
        '
        Me.Label1.AutoSize = True
        Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label1.Location = New System.Drawing.Point(89, 10)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(224, 17)
        Me.Label1.TabIndex = 8
        Me.Label1.Text = "Upload & DownLoad of  images"
        '
        'SaveFileDialog1
        '
        Me.SaveFileDialog1.FileName = "doc1"
        '
        'ImageDemo
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(368, 238)
        Me.Controls.Add(Me.Panel1)
        Me.Controls.Add(Me.Label1)
        Me.Name = "ImageDemo"
        Me.Text = "Form1"
        Me.Panel1.ResumeLayout(False)
        Me.Panel1.PerformLayout()
        CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)
        Me.PerformLayout()
    End Sub
#End Region
    Private Sub btnUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpload.Click
        Try
            If Trim(txtImg.Text) = "" Then
                MsgBox("Please select a image.")
                Exit Sub
            End If
            Dim fs As New FileStream(Trim(txtImg.Text), FileMode.Open)
            Dim Data() As Byte = New [Byte](fs.Length) {}
            fs.Read(Data, 0, fs.Length)
            '        <add name="SCARMAK.ConnString" connectionString="Data Source=WAIITD138;Initial Catalog=SCARMAK;User ID=SCARMAKUSER;Password=password"
            Dim con As New System.Data.SqlClient.SqlConnection("data source=WAIITD138;Initial Catalog=WORDLOAD;Persist Security Info=True;User ID=WORDUSER;Password=password")
            con.Open()
            Dim cmd As New System.Data.SqlClient.SqlCommand("UploadImage")
            cmd.Connection = con
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.AddWithValue("@imgsamgetimage", Data)
            cmd.ExecuteNonQuery()
            con.Close()
            fs.Close()
        Catch ex As System.Data.SqlClient.SqlException
            MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub btnseltfile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnseltfile.Click
        OpenFileDialog1.ShowDialog()
        txtImg.Text = OpenFileDialog1.FileName
    End Sub
    Private Sub btnDownload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDownload.Click
        'Dim con As New System.Data.SqlClient.SqlConnection("data source=WAIITD138;Initial Catalog=WORDLOAD;Persist Security Info=True;User ID=WORDUSER;Password=password")
        'con.Open()
        'Dim cmd As New System.Data.SqlClient.SqlCommand("select * from SampleImageTable")
        'cmd.Connection = con
        'cmd.CommandType = CommandType.Text
        'Dim da As New System.Data.SqlClient.SqlDataAdapter(cmd)
        'Dim ds As New DataSet()
        'da.Fill(ds)
        'Dim bits As Byte() = CType(ds.Tables(0).Rows(0).Item(0), Byte())
        'Dim memorybits As New MemoryStream(bits)
        'Dim bitmap As New Bitmap(memorybits)
        'PictureBox1.Image = bitmap
        Try
            Dim PictureCol As Integer = 0 ' the column # of the BLOB field
            'Dim cn As New SqlConnection("server=localhost;integrated security=yes;database=NorthWind")
            Dim cn As New System.Data.SqlClient.SqlConnection("data source=WAIITD138;Initial Catalog=WORDLOAD;Persist Security Info=True;User ID=WORDUSER;Password=password")
            Dim cmd As New SqlCommand("Select * from SampleImageTable", cn)
            cn.Open()
            Dim dr As SqlDataReader = cmd.ExecuteReader()
            dr.Read()
            Dim b(dr.GetBytes(PictureCol, 0, Nothing, 0, Integer.MaxValue) - 1) As Byte
            dr.GetBytes(PictureCol, 0, b, 0, b.Length)
            dr.Close()
            cn.Close()
         Dim fs As New FileStream("C:\Documents and Settings\u13901\Desktop\Projects", IO.FileMode.Create, IO.FileAccess.Write)
            fs.Write(b, 0, b.Length)
            fs.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
Posted
Comments
[no name] 18-Sep-12 13:01pm    
You did not say so but I suspect that you are getting your error on this line: Dim fs As New FileStream("C:\Documents and Settings\u13901\Desktop\Projects", IO.FileMode.Create, IO.FileAccess.Write)
If so, try giving it a filename to write to instead of a directory name.
ekipongi 18-Sep-12 17:50pm    
Thank you Wes, I have created a MS WOrd document and saved into the above directory as (C:\Documents and Settings\u13901\Desktop\Projects\Test.doc). When I upload the MS WOrd from the DB onto that there are funny characters being written onto the Word DOcument. How do i change that to write the resume details as saved? Please assist.

1 solution

I have a sample project for you that I wrote.

http://sdrv.ms/QjleCk
 
Share this answer
 
v4
Comments
[no name] 18-Sep-12 12:57pm    
You don't
ekipongi 18-Sep-12 17:58pm    
Thank you very much Ernest. I downloaded what you have put here http://sdrv.ms/QjleCk. However, I can't restore the DB onto my MS SQL Server 2008 Enterprise edition. Could you please send me the scripts of the DB objects so I can them? I really need this to work. Please assist as the DB can't be restore as it's in Express Edition. Now I have done the above already but I'm getting the following error when trying to insert new resume; Please assist. "The INSERT statement conflicted with the FOREIGN KEY constraint "FK_tbl_users_pKeyUser". The conflict occurred in database "WORDLOAD", table "dbo.tbl_users", column 'pKeyUser'. The statement has been terminated."

How do I insert new resume and how do I retrive the saved resume. Do I have to input the Firstname and the Lastname and add the Resume for the client. It's not accepting my input for the names into the textboxes except from the datagridview. Please assist...
Ernest Wiggins 19-Sep-12 12:10pm    
The previuos version is 2010. Also I didn't not finsh all of the crud in the solution.

Goto the following link you will find a full crud based example.
This solution is built in VS 2008.
Also i have added the MDF and LDF for the database. Also include the scripts if you want to restore the database that way.

http://sdrv.ms/PHuJL1

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900