Image Upload into Database and get All images into the Grid View

    • Source code

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="imageuploadindatabase.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 53%;
            height: 250px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        This project is represent to upload data inside database
        <br />
        <br />
        <br />
        <br />
        <table bgcolor="#CC99FF" class="style1">
            <tr>
                <td>
                    babyid</td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    baby name</td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Father Name</td>
                <td>
                    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Upload baby pic</td>
                <td>
                    <asp:FileUpload ID="FileUpload1" runat="server" />
                </td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="save" />
                </td>
            </tr>
        </table>
    
    </div>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    <br />
    <br />
    <asp:HyperLink ID="HyperLink1" runat="server" ForeColor="#9999FF" 
        NavigateUrl="~/Allbabydata.aspx">All data</asp:HyperLink>
    </form>
</body>
</html>



  • C# code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.IO;

namespace imageuploadindatabase
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (!FileUpload1.HasFile)
            {
                Label1.Text = "pleace select image ";
            }
            else
            {
                String constring = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\alex\documents\visual studio 2010\Projects\imageuploadindatabase\imageuploadindatabase\App_Data\Database1.mdf;Integrated Security=True;User Instance=True";
                SqlConnection sqlcon = new SqlConnection(constring);
                FileUpload1.SaveAs(Server.MapPath("~/babypic/") + Path.GetFileName(FileUpload1.FileName));
                String link = "babypic/" + Path.GetFileName(FileUpload1.FileName);
                String query = "Insert into data(babyid,babyname,fathername,babypic) values(" + TextBox1.Text + ",'" + TextBox2.Text + "','" + TextBox3.Text + "','" + link + "')";
                SqlCommand cmd = new SqlCommand(query, sqlcon);
                sqlcon.Open(); ;
                cmd.ExecuteNonQuery();
                sqlcon.Close();
                Label1.Text = "Data Has Been Saved Successfully";
                TextBox1.Text = "";
                TextBox2.Text = "";
                TextBox3.Text = "";
            }
        }
    }
}



Note : using this changing as requerments

Comments

Popular posts from this blog

Search Record From Table Using jQuery in Asp.net core MVC

Basic control in Asp.net

How To insert Radio Button Data in DataBase -- MVC