Saturday, 18 February 2012

To retrive image size from database and show in a gridview

Source Code

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


<!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>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:Label ID="Label1" runat="server" Text="Roll"></asp:Label>
        <asp:TextBox ID="txtroll" runat="server" 
            style="position:absolute; top: 8px; left: 70px;"></asp:TextBox>
        <br />
        <asp:Label ID="Label2" runat="server" Text="Name"></asp:Label>
        <asp:TextBox ID="txtname" runat="server" 
            style="position:absolute; top: 30px; left: 70px;"></asp:TextBox>
        <br />
        <asp:Label ID="Label3" runat="server" Text="Image"></asp:Label>
        <asp:FileUpload ID="FileUpload1" runat="server"  
            style="position:absolute; top: 53px; left: 69px;"/>
        <br />
        <asp:Button ID="btnsubmit" runat="server" Text="Submit" 
            style="position:absolute; top: 73px; left: 65px;" onclick="btnsubmit_Click" />
    
    </div>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        onselectedindexchanged="GridView1_SelectedIndexChanged">
    <Columns>
    <asp:BoundField  DataField="Roll"/>
    <asp:BoundField  DataField="Name"/>
    <asp:TemplateField>
    <ItemTemplate>
   
   <img id="im1" src='Image/<%#Eval("Imaga")%>' height="80px"/>
    </ItemTemplate>
    </asp:TemplateField>
    
    <asp:BoundField HeaderText="size in Kb" DataField="Size"/>
    
    </Columns>
    </asp:GridView>
    </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;

public partial class ImageInsert : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("server=satya-12\\sa;database=sa;uid=lui;pwd=sa@12");
    SqlDataAdapter da;
    SqlCommand com;
    DataSet ds;
    DataTable dt;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
          
        }
        FillGrid();
    }
    protected void btnsubmit_Click(object sender, EventArgs e)
    {

        FileInfo fileInfo = new FileInfo(Server.MapPath("Image/" + FileUpload1.FileName + ""));
        double fileSizeKB = fileInfo.Length / 1024;
        double fileSizeMB = fileInfo.Length / (1024 * 1024);


        //FileUpload1.SaveAs(Server.MapPath("Image/" + FileUpload1.FileName + ""));
        string ins = "Insert into Demo2 values('" + txtroll.Text + "','" + txtname.Text + "','" + FileUpload1.FileName + "','"+fileSizeKB+"')";
        if (con.State == ConnectionState.Open)
        {
            con.Close();
        }
        con.Open();
        com = new SqlCommand(ins,con);
        com.ExecuteNonQuery();
        //}
    }
    public void FillGrid()
    {
        string sel = "Select * from Demo2";
        if (con.State == ConnectionState.Open)
        {
            con.Close();
        }
        con.Open();
        da = new SqlDataAdapter(sel,con);
        dt = new DataTable();
        da.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}
database Design

Wednesday, 15 February 2012

Data show in a gridview and using scrollbar

Source Code

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

<!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">
    .scr
    {
       overflow-y: scroll;overflow-x:hidden; height: 200px;
    }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <br />
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        <br />
  <div class="scr" >
<asp:GridView  ID="GridView1" runat="server" Font-Size="12px" BackColor="#FFFFFF"
GridLines="Both" CellPadding="4" Width="560">
<HeaderStyle BackColor="#EDEDED" Height="26px" />
</asp:GridView>
</div>
 
    </div>
 
    </form>
</body>
</html>
Design

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;
public partial class ScrollGrid : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("server=Satya\\11;database=linku;uid=sa;pwd=satya123");
    SqlDataAdapter da;
    SqlCommand com;
    DataSet ds;
    DataTable dt;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FillGrid();
        }
    }
    public void FillGrid()
    {
        string sel = "Select * from student";
        if (con.State == ConnectionState.Open)
        {
            con.Close();
        }
        con.Open();
        da = new SqlDataAdapter(sel, con);
        dt = new DataTable();
        da.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}

Tuesday, 7 February 2012

Add Record to Database using Empty Template and Footer Template and in gridview serch data through Autocomplete Extender

Source Code
 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="AutoCompleteInGridView .aspx.cs" Inherits="AutoCompleteInGridView_" %>

<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>

<!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: 100%;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
     <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <div>
  
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <table class="style1">
                    <tr>
                        <td>
                            <asp:GridView ID="gv" runat="server" AllowPaging="True"
                                AutoGenerateColumns="False"
                                onselectedindexchanged="gv_SelectedIndexChanged" ShowFooter="True" >
                          
                                <Columns>
                                    <asp:TemplateField HeaderText="Name">
                                      <ItemTemplate>
                                       
                                              <asp:TextBox ID="TxtName" runat="server"  Enabled="true" Visible="true"></asp:TextBox>                                       
                                           <asp:AutoCompleteExtender ID="TxtName_AutoCompleteExtender" runat="server"
                                               DelimiterCharacters="" Enabled="True" ServiceMethod="GetName" MinimumPrefixLength="1" ServicePath="" TargetControlID="TxtName">
                                           </asp:AutoCompleteExtender>
                                        </ItemTemplate>                                     
                                   </asp:TemplateField>
                                   <asp:TemplateField HeaderText="Roll">
                                   <ItemTemplate>
                                   <asp:TextBox ID="Txtroll" runat="server" Text='<%# Eval("roll") %>' Enabled="true" Visible="true"></asp:TextBox>                                       
                                   </ItemTemplate>
                                 
                                   </asp:TemplateField>
                                  <asp:TemplateField HeaderText="Name">
                                    
                                      <ItemTemplate>
                                          <asp:Label ID="LblRollno" runat="server" Text='<%# Eval("roll") %>' Visible="false"></asp:Label>
                                              <asp:TextBox ID="TxtName1" runat="server" Text='<%# Eval("Name") %>' Enabled="true" Visible="true"></asp:TextBox>                                       
                                       
                                        </ItemTemplate>
                                      <FooterTemplate>
                                          <asp:TextBox ID="TxtName" runat="server"></asp:TextBox>
                                      </FooterTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Age">
                                        <FooterTemplate>
                                            <asp:TextBox ID="Txtage" runat="server"></asp:TextBox>
                                        </FooterTemplate>
                                      <ItemTemplate>
                                       
                                              <asp:TextBox ID="Txtage" runat="server" Text='<%# Eval("age") %>' Enabled="true" Visible="true"></asp:TextBox>                                       
                                       
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Date">
                                        <FooterTemplate>
                                            <asp:TextBox ID="TxtDate" runat="server"></asp:TextBox>
                                            <asp:CalendarExtender ID="TxtDate_CalendarExtender" runat="server"
                                                Enabled="True" TargetControlID="TxtDate">
                                            </asp:CalendarExtender>
                                        </FooterTemplate>
                                      <ItemTemplate>
                                       
                                              <asp:TextBox ID="Txtinfdate" runat="server" Text='<%# Eval("InfDate") %>' Enabled="true" Visible="true"></asp:TextBox>                                       
                                       
                                              <asp:CalendarExtender ID="Txtinfdate_CalendarExtender" runat="server"
                                                  Enabled="True" TargetControlID="Txtinfdate" Format="dd/MM/yyyy" ClearTime="false">
                                              </asp:CalendarExtender>
                                       
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField >
                                        <FooterTemplate>
                                            <asp:Button ID="Btn_FInsert" runat="server" onclick="Btn_FInsert_Click"
                                                Text="Save" />
                                        </FooterTemplate>
                                    <ItemTemplate>
                                        <asp:Button ID="btnupdate" runat="server" Text="Update" CommandName="UP" OnClick="btnupdate_Click" />
                                    </ItemTemplate>
                                    </asp:TemplateField>
                                     <asp:TemplateField>
                                    <ItemTemplate>
                                        <asp:Button ID="btndlt" runat="server" Text="Dlt" CommandName="UP"
                                            onclick="btndlt_Click"/>
                                    </ItemTemplate>
                                    </asp:TemplateField>
                                  
                                </Columns>
                             <EmptyDataTemplate>
        <tr style="background-color: Green;">
            <th scope="col">
                 Name
            </th>
            <th scope="col">
                Age
            </th>
            <th scope="col">
               Date
            </th>
            <th scope="col">
                 
            </th>
        </tr>
        <tr>
            <td>
                <asp:TextBox ID="txtName" runat="server" />
            </td>
            <td>
                <asp:TextBox ID="txtage" runat="server" />
            </td>
            <td>
                <asp:TextBox ID="txtdate" runat="server" />
            </td>
            <td>
                <asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="Add" CommandName = "EmptyDataTemplate" />
            </td>
        </tr>
    </EmptyDataTemplate>
                            </asp:GridView>
                         
                        </td>
                    </tr>
                </table>
            </ContentTemplate>
            <Triggers>
            <asp:PostBackTrigger ControlID="gv" />
            </Triggers>
        </asp:UpdatePanel>
    </div>
 
     <asp:Label ID="Label1" runat="server" Text="Success"></asp:Label>
 
    </form>
</body>
</html>


Design

Runtime Design //When Blank Table
 

Runtime Design
 

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.SqlClient;
using System.Data;
using System.Configuration;
public partial class AutoCompleteInGridView_ : System.Web.UI.Page
{
    SqlConnection con;
    SqlCommand cmd = new SqlCommand();
    DataSet ds;
    SqlDataAdapter da;
    int roll;
    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Visible = false;
        if (IsPostBack == false)
        {
            con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            //cmd.Connection = con;
            //cmd.CommandText = "select * from fms.fms_demo";
            con.Open();

            da = new SqlDataAdapter("select max(roll) as roll  from Student", con);
            ds = new DataSet();
            da.Fill(ds);
            if (ds.Tables[0].Rows[0]["roll"].ToString() != "")
            {
                roll = int.Parse(ds.Tables[0].Rows[0]["roll"].ToString());
                con.Close();
            }
            else
            {
                roll = 1;
            }
            fillGridview();
        }
    }

    private void fillGridview()
    {
        con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        //cmd.Connection = con;
        //cmd.CommandText = "select * from
Student";
        con.Open();
        da = new SqlDataAdapter("select roll,Name,age,Convert(varchar(20),InfDate,111) as InfDate  from
Student", con);
        ds = new DataSet();
        da.Fill(ds);
        gv.DataSource = ds;
        gv.DataBind();
        con.Close();
    }
    [System.Web.Script.Services.ScriptMethod()]
    [System.Web.Services.WebMethod]
    public static List<string> GetName(string prefixText)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from
Student ", con);
        cmd.Parameters.AddWithValue("@Name", prefixText);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        List<string> CountryNames = new List<string>();
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            CountryNames.Add(dt.Rows[i]["name"].ToString());
        }
        return CountryNames;
    }
    protected void btnupdate_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
        Button bt = (Button)sender;
        GridViewRow gr = (GridViewRow)bt.NamingContainer;
        int rr = gr.RowIndex;

        Label t1 = (Label)gr.FindControl("LblRollno");
        TextBox t2 = (TextBox)gr.FindControl("TxtName1");
        con.Open();
        SqlCommand com = new SqlCommand("update
Student set Name='" + t2.Text + "' where roll=" + t1.Text + "", con);
        com.ExecuteNonQuery();
    }

   protected void Add(object sender, EventArgs e)
   {
    Control control = null;
    if (gv.FooterRow != null)
    {
        control = gv.FooterRow;
    }
    else
    {
        control = gv.Controls[0].Controls[0];
    }
    string nm = (control.FindControl("txtName") as TextBox).Text;
    int ag = int.Parse((control.FindControl("txtage") as TextBox).Text);
    DateTime dt =Convert.ToDateTime((control.FindControl("txtdate") as TextBox).Text);
    string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    using (SqlConnection con = new SqlConnection(strConnString))
    {
        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.Connection = con;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "INSERT INTO
Student (Name,age,InfDate) VALUES(@CustomerName, @Customerage, @Customerdob)";
            cmd.Parameters.AddWithValue("@CustomerName", nm);
            cmd.Parameters.AddWithValue("@Customerage", ag);
            cmd.Parameters.AddWithValue("@Customerdob", dt);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }
    Response.Redirect(Request.Url.AbsoluteUri);
}
    protected void gv_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void btndlt_Click(object sender, EventArgs e)
    {

       SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
        Button bt = (Button)sender;
        GridViewRow gr = (GridViewRow)bt.NamingContainer;
        int rr = gr.RowIndex;

        Label t1 = (Label)gr.FindControl("LblRollno");
       // TextBox t2 = (TextBox)gr.FindControl("TxtName1");
        con.Open();
        SqlCommand com = new SqlCommand("delete from
Student where roll=" + t1.Text + "", con);
        com.ExecuteNonQuery();
    }
    protected void Btn_FInsert_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
        Button bt = (Button)sender;
        GridViewRow gr = (GridViewRow)bt.NamingContainer;
        TextBox Nm = (TextBox)gr.FindControl("TxtName");
        TextBox ag = (TextBox)gr.FindControl("Txtage");
        TextBox dt = (TextBox)gr.FindControl("TxtDate");
        con.Open();
        SqlCommand com = new SqlCommand("Insert into
Student (age,InfDate,Name) values('"+ag.Text+"','"+dt.Text+"','"+Nm.Text+"')",con);
       int x= com.ExecuteNonQuery();
       if (x > 0)
       {
           fillGridview();
           Label1.Visible = true;
       }     
    }
}


Web Config File
<?xml version="1.0"?>
<configuration>
  <connectionStrings>
    <add name="ConnectionString" connectionString="server=Satya\S1;database=linku;uid=ma;pwd=li2011"/>
  </connectionStrings>
    <system.web>
   
        <compilation debug="true" targetFramework="4.0">
            <assemblies>
                <add assembly="System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
                <add assembly="System.Web.Extensions.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
                <add assembly="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/></assemblies></compilation>
   
    </system.web>
</configuration>


Databse Design