techpreparation-homepage

Home  Interview Questions  Aptitude Questions  Tutorials  Placement Papers  Search  Resume Guide  Soft Skills  Video  Forum  Blog


Technical Interview Questions

Visual Basic Interview Question
.NET Web Interview Questions
.NET Interview Questions
C# Interview Questions
                              .........More

Source Codes
Asp .NET Source Codes
Asp VB Script Source Codes
                              .........More

Soft Skills
Communication Skills
Leadership Skills
                              .........More

Subscribe to our Newsletters
Name:
Email:

 

 

  

Asp.Net Source Codes

Uploading a file from a webpage directly to your database

Webpage Code
You'll notice that this uses a special control called a "file" This control allows the user to browse for a file on their system to upload, encodes the file and sends it to the web server.

<form runat="server" enctype="multipart/form-data" ID="Form2">
<P>
<input type="file" id="file1" runat="server" NAME="file1">
</P>
<P>
<asp:Button id="btn1" runat="server" text="Upload" onclick="upload" />
</P>
</form>


Server Side Code Behind


This code takes the file from the inputstream and reads the file into a string variable. Then it loads the value into an IMAGE field in SQL Server. You will need to change the connection settings and the insert statement to match your needs.

Imports System.Data
Imports System.Data.SqlClient

Public Sub Upload(ByVal sender As Object, ByVal e As System.EventArgs)
Dim b(file1.PostedFile.InputStream.Length - 1) As Byte

file1.PostedFile.InputStream.Read(b, 0, file1.PostedFile.InputStream.Length)

Dim con As New SqlConnection(ConfigurationSettings.AppSettings("ConnectionStringSQL"))

Dim sql As String = "INSERT INTO MY_TABLE(MyID, DATABLOB) VALUES(1,@BlobData) "
Dim cmd As New SqlCommand(sql, con)

Dim parmBlob As New SqlParameter("@BlobData", SqlDbType.Image, _
b.Length, ParameterDirection.Input, False, 0, _
0, Nothing, DataRowVersion.Current, b)

cmd.Parameters.Add(parmBlob)

con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Sub


<<<----- Return to Asp.Net Source Code Questions Page


Have a Question ? post your questions here. It will be answered as soon as possible.

Check Microsoft .Net Interview Questions for more Microsoft .Net Interview Questions with Answers.

Check .Net Database Interview Questions for more .Net Database Interview Questions with answers