i am trying to upload my word doc file in the database and then download by 
clicking on the download here button but im getting errors on cmd and 
br.close() ,saying br,fs and cmd are not declared.
Imports System.IO
Imports System.Data.SqlClient
Imports System.Data

Partial Class SUPREME_COURT
    Inherits System.Web.UI.Page
     Read the file and convert it to Byte Array
    Dim filePath As String = Server.MapPath("Documents/Supreme Fees.docx")
    Dim filename As String = Path.GetFileName(filePath)
    Dim fs As FileStream = New FileStream(filePath, FileMode.Open, 
FileAccess.Read)
    Dim br As BinaryReader = New BinaryReader(fs)
    Dim bytes As Byte() = br.ReadBytes(Convert.ToInt32(fs.Length))
    br.Close()
    fs.Close()

    'insert the file into database
    ' Dim cmd As SqlCommand = New SqlCommand(strQuery)
    





    Dim strQuery As String = "insert into Files(Name, ContentType, Data) values 
(@Name, @ContentType, @Data)"
    Dim cmd As SqlCommand = New SqlCommand(strQuery)

    cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = filename
    cmd.Parameters.Add("@ContentType", SqlDbType.VarChar).Value = 
"application/vnd.ms-excel"
    cmd.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes
    Function InsertUpdateData() As Boolean
        InsertUpdateData(cmd)
    End Function

    


    Public Function InsertUpdateData(ByVal cmd As SqlCommand) As Boolean
        Dim strConnString As String = System.Configuration.
            
ConfigurationManager.ConnectionStrings("JSMZConnectionString").ConnectionString()

        Dim con As New SqlConnection(strConnString)
        cmd.CommandType = CommandType.Text

        cmd.Connection = con

        Try

            con.Open()
            cmd.ExecuteNonQuery()
            Return True

        Catch ex As Exception
            Response.Write(ex.Message)
            Return False

        Finally
            con.Close()
            con.Dispose()
        End Try

    End Function



    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles 
Budwnldbtntton1.Click
        Dim strQuery As String = "select Name, ContentType, Data from tblFiles 
where id=@id"
        Dim cmd As SqlCommand = New SqlCommand(strQuery)
        cmd.Parameters.Add("@id", SqlDbType.Int).Value = 1
        Dim dt As DataTable = GetData(cmd)
        If dt IsNot Nothing Then
            download(dt)

        End If
    End Sub


    Public Function GetData(ByVal cmd As SqlCommand) As DataTable
        Dim dt As New DataTable
        Dim strConnString As String = 
System.Configuration.ConfigurationManager.ConnectionStrings("JSMZConnectionString").ConnectionString()
        Dim con As New SqlConnection(strConnString)
        Dim sda As New SqlDataAdapter
        cmd.CommandType = CommandType.Text
        cmd.Connection = con
        Try
            con.Open()
            sda.SelectCommand = cmd
            sda.Fill(dt)
            Return dt

        Catch ex As Exception

            Response.Write(ex.Message)
            Return Nothing

        Finally

            con.Close()
            sda.Dispose()
            con.Dispose()
        End Try

    End Function

    Private Sub download(dt As DataTable)
        Throw New NotImplementedException
    End Sub

End Class



_______________________________________________
support-seamonkey mailing list
support-seamonkey@lists.mozilla.org
https://lists.mozilla.org/listinfo/support-seamonkey

Reply via email to