How to Upload Image in database Store in folder in ASP.NET-Example

How to Upload Image in database Store in folder in ASP.NET-Example


Upload IMAGE.ASPX

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Upload_photos.aspx.cs" Inherits="Upload_photos" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body style="font-style: italic">
    <form id="form1" runat="server">
    <div>
   
    </div>
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <p>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:Label ID="Label2" runat="server" Text="plz Enter any image first.." Visible="False"></asp:Label>
        </p>
        <p>
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
        </p>
        <p>
            <asp:Label ID="Label1" runat="server" Text="PhotoUploaded" Visible="False"></asp:Label>
        </p>
    </form>
</body>
</html>


UPLOAD IMAGE.ASPX.CS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class Upload_photos : System.Web.UI.Page
{
    SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=D:\Asp_dot_ne\WebSite3\App_Data\pro_gridview.mdf;Integrated Security=True");
    SqlCommand cmd;

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text == "" || FileUpload1.FileName == "")
        {
            Label2.Visible = true;
            Label1.Visible = false;
        }
        else
        {
            conn.Open();
            string str = "~/myupload" + FileUpload1.FileName;
            FileUpload1.SaveAs(Server.MapPath(str));
            cmd = new SqlCommand("insert into imgupload values('" + str + "','" + TextBox1.Text + "')", conn);
            cmd.ExecuteNonQuery();
            conn.Close();
            Label1.Visible = true;
            Label2.Visible = false;
        }
    }
}
How to insert data in SQL database in ASP.NET-Example

How to insert data in SQL database in ASP.NET-Example


INSERT.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Insert.aspx.cs" Inherits="Insert" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            width: 100%;
        }
        .auto-style2 {
            width: 98px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <table class="auto-style1">
            <tr>
                <td class="auto-style2">ID</td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="auto-style2">Name</td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="auto-style2">Address</td>
                <td>
                    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                </td>
            </tr>
        </table>
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Insert" />
        <br />
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
    </form>
</body>
</html>


INSERT.ASPX.CS

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

public partial class Insert : System.Web.UI.Page
{
    SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=D:\Program OF Asp.net\WebSite2\App_Data\Database.mdf;Integrated Security=True");
    SqlCommand cmd;      //for insert update delete.
    SqlDataAdapter adp;
    DataTable dt;

    public void disp_emp() // create Function and define it.
    {                      // Or manualy you can write it.
        conn.Open();
        adp = new SqlDataAdapter("select * from emp", conn);
        dt = new DataTable();
        adp.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
        conn.Close();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        disp_emp();        //define class.
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        conn.Open();
        cmd = new SqlCommand("insert into emp values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "')", conn);
        cmd.ExecuteNonQuery();
        conn.Close();
        disp_emp();         //define class.
    }
}
How to Update data in SQL database in ASP.NET-Example

How to Update data in SQL database in ASP.NET-Example


UPDATE.ASPX
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Update.aspx.cs" Inherits="Update" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            width: 100%;
        }
        .auto-style2 {
            width: 98px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <table class="auto-style1">
            <tr>
                <td class="auto-style2">ID</td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="auto-style2">Name</td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="auto-style2">Address</td>
                <td>
                    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                </td>
            </tr>
        </table>
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Update" />
        <br />
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
    </form>
</body>
</html>


UPDATE.ASPX.CS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class Update : System.Web.UI.Page
{
    SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=D:\Program OF Asp.net\WebSite2\App_Data\Database.mdf;Integrated Security=True");
    SqlCommand cmd;      //for insert update delete.
    SqlDataAdapter adp;
    DataTable dt;

    public void disp_emp() // create class and define it.
    {                      // Or manualy you can write it.
        conn.Open();
        adp = new SqlDataAdapter("select * from emp", conn);
        dt = new DataTable();
        adp.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
        conn.Close();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        disp_emp();        //define class.
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        conn.Open();
        cmd = new SqlCommand("update emp set name = '" + TextBox2.Text + "', address = '" + TextBox3.Text + "' where id = '" + TextBox1.Text + "'", conn);
        cmd.ExecuteNonQuery();
        conn.Close();
        disp_emp();         //define class.
    }
}
How to delete data in SQL database in ASP.NET-Example

How to delete data in SQL database in ASP.NET-Example


DELETE.ASPX.CS

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Delete.aspx.cs" Inherits="Delete" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            width: 100%;
        }
        .auto-style2 {
            width: 98px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <table class="auto-style1">
            <tr>
                <td class="auto-style2">ID</td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="auto-style2">Name</td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="auto-style2">Address</td>
                <td>
                    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                </td>
            </tr>
        </table>
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Delete" />
        <br />
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
    </form>
</body>
</html>


DELETE.ASPX.CS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class Delete : System.Web.UI.Page
{
    SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=D:\Program OF Asp.net\WebSite2\App_Data\Database.mdf;Integrated Security=True");
    SqlCommand cmd;      //for insert update delete.
    SqlDataAdapter adp;
    DataTable dt;
   
    public void disp_emp() // create class and define it.
    {                      // Or manualy you can write it.
        conn.Open();
        adp = new SqlDataAdapter("select * from emp", conn);
        dt = new DataTable();
        adp.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
        conn.Close();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        disp_emp();        //define class.
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        conn.Open();
        cmd = new SqlCommand("delete from emp where id = '" + TextBox1.Text + "'", conn);
        cmd.ExecuteNonQuery();
        conn.Close();
        disp_emp();         //define class.
    }
}

How to display data in gridview control from database(Declarative Binding) in ASP.NET -Example


Gridview.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="19_Gridview.aspx.cs" Inherits="_19_Gridview" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
            <Columns>
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
                <asp:CommandField ShowEditButton="True" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [detail]" UpdateCommand="UPDATE detail set Name=@Name,City=@city where City=@City"></asp:SqlDataSource>
   
    </div>
    </form>
</body>
</html>

Step:-

Click on Controls right manu


select new SqlDataSource1 on dropdownlist


Select database


click next

select data table


Click next






Click finish




………………WE not require write any think in cs file (Don’t delete cs file).
Don’t display data in declarative binding  every time (It is not strong connection)
How to display image in Datalist control from database(Programmatic Binding) in ASP.NET -Example

How to display image in Datalist control from database(Programmatic Binding) in ASP.NET -Example


Datalist.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="21_Bind_datalist_wiht_photo.aspx.cs" Inherits="_21_Bind_datalist_wiht_photo" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            width: 100%;
            height: 131px;
        }
        .auto-style2 {
            height: 63px;
        }
        .auto-style3 {
            height: 164px;
        }
        .auto-style4 {
            height: 64px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:DataList ID="DataList1" runat="server" RepeatColumns="2" RepeatDirection="Horizontal">
            <ItemTemplate>
                <table class="auto-style1">
                    <tr>
                        <td class="auto-style2">
                            <asp:Image ID="Image1" runat="server" Width="150px" Height="100" ImageUrl='<%#Eval("image") %>'/>
                        </td>
                    </tr>
                    <tr>
                        <td class="auto-style4">
                            <asp:Label ID="Label1" runat="server" Text='<%#Eval("name") %>'></asp:Label>
                        </td>
                    </tr>
                </table>
            </ItemTemplate>
        </asp:DataList>
   
    </div>
        <p>
            &nbsp;</p>
    </form>
</body>
</html>


Datalist.aspx.cs

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

public partial class _21_Bind_datalist_wiht_photo : System.Web.UI.Page
{
    SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=D:\Asp_dot_ne\WebSite3\App_Data\pro_gridview.mdf;Integrated Security=True");
    SqlDataAdapter adp;
    DataTable dt;

    protected void Page_Load(object sender, EventArgs e)
    {
        conn.Open();
        adp = new SqlDataAdapter("select * from imgupload", conn);
        dt = new DataTable();
        adp.Fill(dt);
        DataList1.DataSource = dt;
        DataList1.DataBind();
        conn.Close();
    }
}

How to display data in Gridview control from Database(Programmatic Binding) in ASP.NET -Example

How to display data in Gridview control from Database(Programmatic Binding) in ASP.NET -Example


Gridview.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="17_pro_gridview.aspx.cs" Inherits="_17_pro_gridview" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
    </div>
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
    </form>
</body>
</html>


Gridview.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class _17_pro_gridview : System.Web.UI.Page
{
    SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=D:\Asp_dot_ne\WebSite3\App_Data\pro_gridview.mdf;Integrated Security=True");
    SqlDataAdapter adp;
    DataTable dt;

    protected void Page_Load(object sender, EventArgs e)
    {
        conn.Open();
        adp = new SqlDataAdapter("select * from DataBind", conn);
        dt = new DataTable();
        adp.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
        conn.Close();
    }
}
How to create Login page using database Connection in ASP.NET -Example

How to create Login page using database Connection in ASP.NET -Example


Login.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="18_Login.aspx.cs" Inherits="_18_Login" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            width: 100%;
            height: 82px;
        }
        .auto-style2 {
            text-align: right;
        }
        .auto-style3 {
            width: 21px;
        }
        .auto-style4 {
            width: 100%;
        }
        .auto-style5 {
            width: 268px;
        }
        #form1 {
            height: 278px;
            text-align: center;
        }
        .auto-style6 {
            text-align: left;
        }
    </style>
</head>
<body style="height: 78px">
    <form id="form1" runat="server">
    <div>
   
        <table class="auto-style1">
            <tr>
                <td class="auto-style2">
                    <asp:Label ID="Label1" runat="server" Text="Username"></asp:Label>
                </td>
                <td class="auto-style3">:-</td>
                <td class="auto-style6">
                    <asp:TextBox ID="TextBox1" runat="server" Height="25px" Width="150px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="auto-style2">
                    <asp:Label ID="Label2" runat="server" Text="Password"></asp:Label>
                </td>
                <td class="auto-style3">:-</td>
                <td class="auto-style6">
                    <asp:TextBox ID="TextBox2" runat="server" Height="25px" TextMode="Password"
Width="150px"></asp:TextBox>
                </td>
            </tr>
        </table>
   
    </div>
        <br />
        <asp:Label ID="Label3" runat="server" Text="Username OR passsword is incorrect Try again."
Visible="False"></asp:Label>
        <br />
        <br />
        <table align="right" class="auto-style4">
            <tr>
                <td class="auto-style5" style="text-align: right">
                    <asp:Button ID="Button1" runat="server" Height="30px" OnClick="Button1_Click" Text="CANCEL"
Width="100px" />
                </td>
                <td style="text-align: left">
                    <asp:Button ID="Button2" runat="server" Height="30px" OnClick="Button2_Click" Text="LOGIN"
Width="100px" />
                </td>                                                                                                          
            </tr>
        </table>
        <br />
        <br />
        <br />
        <asp:Label ID="Label4" runat="server" Text="Hi...Welcome...!" Visible="False"></asp:Label>
    </form>
</body>
</html>


Login.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class _18_Login : System.Web.UI.Page
{
    SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=D:\Asp_dot_ne\WebSite3\App_Data\pro_gridview.mdf;Integrated Security=True");
    SqlDataAdapter adp;
    DataTable dt;

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text == "" || TextBox2.Text == "")
        {
            Label3.Visible = true;
            Label4.Visible = false;
        }
        else
        {
            conn.Open();
            adp = new SqlDataAdapter("select * from Login where usnm='" + TextBox1.Text + "' and pwd='" + TextBox2.Text + "'", conn);
            dt = new DataTable();
            adp.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                Label4.Visible = true;
                Label3.Visible = false;
            }
            else
            {
                Label3.Visible = true;
                Label4.Visible = false;
            }
            conn.Close();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        TextBox1.Text = "";
        TextBox2.Text = "";
    }
}