The main purpose of "Dot Net Labs" provide the dot net program. You can learn dot net, .Net Core, C#, SQL, Linq step-by-step.

Thursday, 8 September 2016

How to display image from database to website with gridview




How to bind images in asp.net gridview

Source Code: 


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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
  </head>
<body>
    <form id="form1" runat="server">
        <div class="GridviewDiv">
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id" DataSourceID="SqlDataSource1">
                <Columns>
                    <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True" SortExpression="id" />
                    <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
<asp:TemplateField HeaderText="Image">
                        <ItemTemplate>
                            <asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("image_path") %>' class="gridImages" Height="30px" Width="30px" />
                           
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:helloConnectionString %>" SelectCommand="SELECT * FROM [image]"></asp:SqlDataSource>
        </div>
    </form>
</body>
</html> 

Output : 





Thanks......................... 

Wednesday, 7 September 2016

How to save image in Database in asp.net with c#


In this article, Discuss about how to save  image in data base with asp.net with c#. Create a folder with name of Images in solution explorer 
example 


Database: -



 Source Code:-


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

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <center>
        <asp:Label runat="server" ID="lblmassage" ForeColor="White" BackColor="Black" BorderColor="Black"  Height="25px"></asp:Label><br /><br />
        <table style="border-collapse: 5; border-spacing: 5px">
            <tr>
                <td>Name</td><td>
                    <asp:TextBox ID="txtname" runat="server"></asp:TextBox></td>
                            </tr>
       
                        <tr>
                     <td colspan="2">
                    <asp:FileUpload ID="FileUpload1" runat="server" /></td>
                                        </tr>
             <tr>
                 <td colspan="2">
                <asp:Button runat="server" ID="btnsubmitt" Text="Submit" OnClick="btnsubmitt_Click"/></td>
                             </tr>
        </table>
               </center>
        </div>
    </form>
</body>
</html>


Code behind :


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

public partial class _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(@"Data Source=amit-pc;Initial Catalog=hello;Integrated Security=True");
    protected void Page_Load(object sender, EventArgs e)
    {
        lblmassage.Visible = false;
    }
    protected void btnsubmitt_Click(object sender, EventArgs e)
    {
        con.Open();
        SqlCommand com = new SqlCommand("insert into image(name,image_name,image_path) values(@a,@b,@c)", con);
        com.Parameters.AddWithValue("@a",txtname.Text);

        if (FileUpload1.HasFile)
        {
            string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
            FileUpload1.SaveAs(Server.MapPath("~/Images/" + filename));
            com.Parameters.AddWithValue("@b", filename);
            com.Parameters.AddWithValue("@c", "~/Images/" + filename);
        }
        int i = com.ExecuteNonQuery();
        if (i == 1)
        {
            lblmassage.Visible = true;
            lblmassage.Text = "Registration Successfully !";
        }
        else
        {
            lblmassage.Visible = true;
            lblmassage.Text = "There are some problem, Please try again !";
        }
        con.Close();
    }
}

Output:-

 below image before input data
  below image after input data

 Database output after input data



 Thanks.........................

Popular Posts