ASP. NET adds images to the SQL Server database

Source: Internet
Author: User

Aspx:
<% @ Page Language = "C #" codebehind = "register. aspx. cs" autoeventwireup = "false" inherits = "Shop. Register" %>
<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title> Registration </title>
</Head>
<Body style = "font-size: 12px">
<Form ID = "form1" method = "Post" runat = "server">
<Font face = "">
<Table id = "Table1" cellspacing = "0" cellpadding = "0" width = "50%" align = "center" bgcolor = "beige"
Border = "0">
<Tr>
<TD bgcolor = "# cccc66" colspan = "2" Height = "25" rowspan = ""> Add a new user </TD>
</Tr>
<Tr>
<TD> name </TD>
<TD>
<Asp: textbox id = "txtpersonname" runat = "server"> </ASP: textbox> </TD>
</Tr>
<Tr>
<TD> email </TD>
<TD>
<Asp: textbox id = "txtpersonemail" runat = "server"> </ASP: textbox> </TD>
</Tr>
<Tr>
<TD> gender </TD>
<TD>
<Asp: radiobutton groupname = "sex" text = "male" id = "sexmale" runat = "server"/>
<Asp: radiobutton groupname = "sex" text = "female" id = "sexfemale" runat = "server"/>
</TD>
</Tr>
<Tr>
<TD> Date of Birth </TD>
<TD>
<Asp: textbox id = "txtpersondob" runat = "server"> </ASP: textbox> </TD>
</Tr>
<Tr>
<TD> photo </TD>
<TD> <input type = "file" id = "personimage" name = "personimage" runat = "server"> </TD>
</Tr>
<Tr>
<TD align = "center" colspan = "2">
<Asp: button id = "button1" onclick = "addperson" runat = "server" text = "add"> </ASP: button>
</TD>
</Tr>
</Table>
</Font>
</Form>
</Body>
</Html>
Aspx. CS:
Using system;
Using system. IO;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. Data. sqlclient;
Using system. drawing;
Using system. Web;
Using system. Web. sessionstate;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;
Using system. configuration;
Namespace shop
{
/// <Summary>
/// Summary of register.
/// </Summary>
Public class register: system. Web. UI. Page
{
Protected system. Web. UI. webcontrols. textbox txtpersonname;
Protected system. Web. UI. webcontrols. textbox txtpersonemail;
Protected system. Web. UI. webcontrols. radiobutton sexmale;
Protected system. Web. UI. webcontrols. radiobutton sexfemale;
Protected system. Web. UI. webcontrols. textbox txtpersondob;
Protected system. Web. UI. htmlcontrols. htmlinputfile personimage;
Protected system. Web. UI. webcontrols. Button button1;

Private void page_load (Object sender, system. eventargs E)
{
// Place user code here to initialize the page
}
Public void addperson (Object sender, system. eventargs E)
{
Int intimagesize;
String strimagetype;
Stream imagestream;
Intimagesize = personimage. postedfile. contentlength; // File Size
Strimagetype = personimage. postedfile. contenttype; // file type
Imagestream = personimage. postedfile. inputstream;
Byte [] imagecontent = new byte [intimagesize];
Int intstatus = imagestream. Read (imagecontent, 0, intimagesize );

// Write data to the database
String strconn = configurationsettings. etettings ["connectionstring"];
Sqlconnection myconnection = new sqlconnection (strconn );
Sqlcommand mycommand = new sqlcommand ("sp_person_isp", myconnection );
Mycommand. commandtype = commandtype. storedprocedure;

Mycommand. Parameters. Add ("@ personemail", sqldbtype. varchar, 255). value = txtpersonemail. text;
Mycommand. Parameters. Add ("@ personname", sqldbtype. varchar, 255). value = txtpersonname. text;
Mycommand. Parameters. Add ("@ personsex", sqldbtype. Char, 1 );
If (sexmale. Checked)
Mycommand. Parameters ["@ personsex"]. value = "M ";
Else
Mycommand. Parameters ["@ personsex"]. value = "F ";
Mycommand. Parameters. Add ("@ persondob", sqldbtype. datetime). value = txtpersondob. text;
Mycommand. Parameters. Add ("@ personimage", sqldbtype. Image). value = imagecontent;
Mycommand. Parameters. Add ("@ personimagetype", sqldbtype. varchar, 255). value = strimagetype;

Try
{
Myconnection. open ();
Mycommand. executenonquery ();
Myconnection. Close ();
Response. Write ("added successfully! ");
}
Catch (system. Exception sqlexe)
{
Response. Write ("failed to add! Cause: "+ sqlexe. tostring ());
}
}

# Code generated by region web Form Designer
Override protected void oninit (eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
Base. oninit (E );
}

/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. Load + = new system. eventhandler (this. page_load );
}
# Endregion
}
}

Related Article

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.