create table students ( id int identify (1,1) not null, name varchar(20), email varchar(30) unique, photo image );
<%@ Page Language="c#" %> <%@ import Namespace="System.Data.SqlClient" %> <script runat="server"> public void insertrow(object sender, EventArgs e) { // Create an array of bytes from the input file int len = upload.PostedFile.ContentLength; byte[] pic = new byte[len + 1]; Upload.PostedFile.InputStream.Read(pic, 0, len); // Insert the image and comment into the database SQLConnection con = new SQLConnection("database=pubs;uid=sa"); con.Open(); SQLCommand cmd = new SQLCommand("insert into students values (@name,@email,@photo)", con); cmd.Parameters.Add("@name", txtname.text); cmd.Parameters.Add("@email", txtemail.text); cmd.Parameters.Add("@photo", pic); cmd.ExecuteNonQuery(); con.Close(); lblmsg.text = "Students Details Inserted Successfully!"; } </script> <html> <head> <form enctype="multipart/form-data" runat="server"> <body> <h2>Student's Details </h2> <table> <tbody> <tr> <td> Student's Name</td> <td> <asp:TextBox id="txtname" RunAt="server"></asp:TextBox> </td> </tr> <tr> <td> Email Address</td> <td> <asp:TextBox id="txtemail" RunAt="server"></asp:TextBox> </td> </tr> <tr> <td> Image File Name</td> <td> <input id="Upload" type="file" runat="server" /></td> </tr> </table> <p> <asp:Button id="Button1" onclick="insertrow" RunAt="server" Text="Insert"></asp:Button> <p> <asp:label id="lblmsg" runat=server /> </form> </body> </html>
<%@ Page Language="c#" %> <%@ import Namespace="system.data.sqlclient,system.drawing,system.drawing.imaging,system.io" %> <script runat="server"> public void page_load() { SQlConnection con = new SQlConnection("uid=sa;database=pubs"); con.open(); SQLCommand cmd = new SQLCommand("select photo from students where id = '" + request.querystring("id") + "'", con); MemoryStream stream = new MemoryStream(); byte[] image = cmd.ExecuteScalar(); stream.Write(image, 0, image.Length); Bitmap bitmap = new Bitmap(stream); Response.ContentType = "image/gif"; bitmap.Save(Response.OutputStream, ImageFormat.Gif); con.Close(); stream.Close(); } </script>
<%@ Page Language="C#" %> <%@ import namespace="system.data.sqlclient,system.data"%> <script runat="server"> public void page_load() { SQLconnection con = new SQLconnection("database=pubs;uid=sa"); con.open(); sqlcommand cmd = new sqlcommand("select id,name,email from students", con); sqldatareader dr = default(sqldatareader); dr = cmd.executereader(); repeater1.datasource = dr; repeater1.databind(); con.close(); } </script> <html> <head> </head> <body> <form runat="server"> <asp:repeater runat=server id=repeater1> <headertemplate> <h2>Details of Students </h2> </headertemplate> <itemtemplate> Id : <%# Container.DataItem("id") %> <br> Name : <%# Container.DataItem("name") %> <br> Email : <%# Container.DataItem("email") %> <br> <img src=displayimage.aspx?id=<%# Container.DataItem("id")%>> <hr> </itemtemplate> </asp:repeater> </form> </body> </html>