Vb. NET upload picture and display in DataGrid

Source: Internet
Author: User
Tags exit header split
datagrid| upload | upload pictures | show one, program function: When upload picture size exceeds 8 K or format discrepancy prohibit upload, upload pass, use datagrid display upload picture

Ii. Establishment of a database

Create a new users table in the MSSQL Northwind database with the following table design:

Column name data type length can be null other ID int 4 No primary key, set ID to be, identify seed 1, recursive increment 1headimgvarchar50 No
Third, form design:

1. New asp.net Web application, named DATAGRID3, save path for HTTP://192.168.0.1/DATAGRID3 (note: The IP of the website on my machine is 192.168.0.1 's main directory is d:\ Web folder) and click OK.

2. In the Solution Explorer window, rename WebForm1.aspx to Uppicture.aspx, and then add a Label control, a button button, to the form from the Toolbox. Then add a file to the form from an HTML toolbox The field control form interface is as follows:

3. In the Solution Explorer window, right-click the item and select Add-new Item-web form, with the name set to Viewpicture.aspx. Then add a DataGrid control to the open form.

4. Right-click the DataGrid control, and then click the "Property Builder" below to open the DataGrid Properties window. In the DataGrid Properties window, click Columns, cancel the check box before automatically creating columns at run time, add a bound column to the selected column, enter "ordinal" in the header text, and enter an ID in the data field. Add a bound column to the selected column, enter "Avatar" in the header text, and enter HEADIMG in the data field. then click OK.

The form interface is as follows;



Four, code design:

1, uppicture.aspx

Imports System.Data.SqlClient
Public Class WebForm1
Inherits System.Web.UI.Page
' Form code omitted
' Upload pictures
Private Sub button1_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Button1.Click

Dim img as String
' Define PostedFile file is to store user uploaded files
Dim PostedFile as Httppostedfile = File1.PostedFile
' Define a variable to store the size of the user's uploaded file
Dim Intimgsize as Int32
' Get the size of the user upload file,
Intimgsize = PostedFile. ContentLength

' If the file you want to upload is not empty
If intimgsize <> 0 Then

' If greater than 8K, it is forbidden to upload
If intimgsize > 8000 Then
Label1.Text = "Picture Too large"
Exit Sub
End If

' Define a variable to store the file type of the user uploading the image
Dim Strimgtype as String = PostedFile. ContentType

' Only pictures in. gif format are accepted.
Dim Filesplit () as String = Split (Strimgtype, "/")
Strimgtype = Filesplit (filesplit. LENGTH-1)
If strimgtype <> "gif" Then
Label1.Text = "Picture is not well-formed"
Exit Sub
End If


' Store the entire path of the file to be uploaded
Filesplit = Split (PostedFile. FileName, "\")
' Get the filename of the uploaded file
Dim filename as String = Filesplit (filesplit. LENGTH-1)
' Save uploaded images to the Headimg folder in the server's current directory
PostedFile. SaveAs (Server.MapPath ("Headimg") & "\" & filename)
' Define a variable to store the path of the currently uploaded picture on the server
Dim Imgpath as String = "headimg\" & filename
img = "
' Store a picture in a database
Dim Scon as New SqlConnection ("server=localhost;database=northwind;uid=sa;pwd=123")
Scon. Open ()
Dim scom as New SqlCommand (insert into users values (@img), Scon)
Scom. Parameters.Add ("@img", SqlDbType.VarChar). Value = img
Try
Scom. ExecuteNonQuery ()
Catch ex as Exception
End Try
Scon. Close ()
' Go to the View Picture window
Response.Redirect ("viewpicture.aspx")
End If
End Sub
End Class
2, Viewpicture.aspx code:

Imports System.Data.SqlClient
Public Class Viewpicture
Inherits System.Web.UI.Page
' Form code omitted
Private Sub Page_Load (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles MyBase.Load
Dim Scon as New SqlConnection ("server=localhost;database=northwind;uid=sa;pwd=123")
Dim SDA as New SqlDataAdapter ("select * from users", Scon)
Dim DS as New DataSet
Try
Sda. Fill (DS)
Catch ex as Exception
End Try
DataGrid1.DataSource = ds
Datagrid1.databind ()
End Sub
End Class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.