https://support.microsoft.com/en-us/kb/316748
Monday, 15 June 2015
Tuesday, 28 April 2015
GridView with Paging and sorting
Connection string: Data Source=banka-pc;Integrated Security=true;Initial Catalog=test2
ASPX file:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="gridviewSortPaging.aspx.cs"
Inherits="GridviewPagingSorting._Default" %>
<!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>Gridview Paging and Sorting </title>
</head>
<body>
<form id="form1" runat="server">
<div>
<div style="font-size: 20px; font-family: Verdana">
<u> Gridview Paging and Sorting</u>
<br />
<br />
</div>
<div>
<asp:GridView ID="GridVwPagingSorting" runat="server" AutoGenerateColumns="False"
Font-Names="Verdana" AllowPaging="True" AllowSorting="True" PageSize="1" Width="75%"
OnPageIndexChanging="PageIndexChanging" BorderColor="#CCCCCC" BorderStyle="Solid"
BorderWidth="1px" OnSorting="Sorting">
<AlternatingRowStyle BackColor="#BFE4FF" />
<PagerStyle BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" />
<PagerSettings Mode="NumericFirstLast" FirstPageText="First" LastPageText="Last" PageButtonCount="2" NextPageText="next" PreviousPageText="prev" />
<HeaderStyle Height="30px" BackColor="#6DC2FF" Font-Size="15px" BorderColor="#CCCCCC"
BorderStyle="Solid" BorderWidth="1px" />
<RowStyle Height="20px" Font-Size="13px" BorderColor="#CCCCCC" BorderStyle="Solid"
BorderWidth="1px" />
<Columns>
<asp:BoundField DataField="name" HeaderText="Student Name" SortExpression="name" />
<asp:BoundField DataField="dob" HeaderText="DOB" SortExpression="dob" />
<asp:BoundField DataField="sal" HeaderText="Salary" SortExpression="sal" />
</Columns>
</asp:GridView>
</div>
<div style="color: Green; font-weight: bold">
<br />
<i>You are viewing page
<%=GridVwPagingSorting.PageIndex + 1%>
of
<%=GridVwPagingSorting.PageCount%>
</i>
</div>
</div>
</form>
</body>
</html>
ASPX.cs
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
namespace GridviewPagingSorting
{
public partial class _Default : System.Web.UI.Page
{
string Sort_Direction = "name ASC";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["SortExpr"] = Sort_Direction;
DataView dvEmployee = Getdata();
GridVwPagingSorting.DataSource = dvEmployee;
GridVwPagingSorting.DataBind();
}
}
private DataView Getdata()
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString()))
{
DataSet dsEmployee = new DataSet();
string strSelectCmd = "SELECT [name],[dob],[sal] FROM [student]";
SqlDataAdapter da = new SqlDataAdapter(strSelectCmd, conn);
da.Fill(dsEmployee, "Student");
DataView dvEmp = dsEmployee.Tables["student"].DefaultView;
dvEmp.Sort = ViewState["SortExpr"].ToString();
return dvEmp;
}
}
protected void PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridVwPagingSorting.PageIndex = e.NewPageIndex;
DataView dvEmployee = Getdata();
GridVwPagingSorting.DataSource = dvEmployee;
GridVwPagingSorting.DataBind();
}
protected void Sorting(object sender, GridViewSortEventArgs e)
{
string[] SortOrder = ViewState["SortExpr"].ToString().Split(' ');
if (SortOrder[0] == e.SortExpression)
{
if (SortOrder[1] == "ASC")
{
ViewState["SortExpr"] = e.SortExpression + " " + "DESC";
}
else
{
ViewState["SortExpr"] = e.SortExpression + " " + "ASC";
}
}
else
{
ViewState["SortExpr"] = e.SortExpression + " " + "ASC";
}
GridVwPagingSorting.DataSource = Getdata();
GridVwPagingSorting.DataBind();
}
}
}
Web.Config
<connectionStrings>
<add name="ConnectionString" connectionString="Data Source=banka-pc;Integrated Security=true;Initial Catalog=test2" providerName="System.Data.SqlClient"/>
</connectionStrings>
Tuesday, 10 March 2015
Hidden field data
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script language="javascript">
function ShowHdnVal() {
alert(document.getElementById('hdnAdi').value);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="hidden" id="hdnAdi" />
<asp:Button runat="server" ID="btnPost" Text="Post Me" OnClick="btnPost_Click" />
<input type="button" value="Show my hidden value" onclick="ShowHdnVal();" />
</div>
</form>
</body>
</html>
---------------------------------------
protected void btnPost_Click(object sender, EventArgs e)
{
if (true)
{
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), Guid.NewGuid().ToString(), "document.getElementById('hdnAdi').value='Avaneesh Dash';", true);
}
}
<head>
<title></title>
<script language="javascript">
function ShowHdnVal() {
alert(document.getElementById('hdnAdi').value);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="hidden" id="hdnAdi" />
<asp:Button runat="server" ID="btnPost" Text="Post Me" OnClick="btnPost_Click" />
<input type="button" value="Show my hidden value" onclick="ShowHdnVal();" />
</div>
</form>
</body>
</html>
---------------------------------------
protected void btnPost_Click(object sender, EventArgs e)
{
if (true)
{
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), Guid.NewGuid().ToString(), "document.getElementById('hdnAdi').value='Avaneesh Dash';", true);
}
}
get Hidden field data
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnPost_Click(object sender, EventArgs e)
{
if (true)
{
ScriptManager. RegisterStartupScript(this. Page, this.Page.GetType(), Guid.NewGuid().ToString(), "document.getElementById(' hdnAdi').value='Avaneesh Dash';", true);
}
}
---------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/ xhtml">
<head>
<title></title>
<script language="javascript">
function ShowHdnVal() {
alert(document.getElementById( 'hdnAdi').value);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="hidden" id="hdnAdi" />
<asp:Button runat="server" ID="btnPost" Text="Post Me" OnClick="btnPost_Click" />
<input type="button" value="Show my hidden value" onclick="ShowHdnVal();" />
</div>
</form>
</body>
</html>
Thursday, 4 December 2014
dataGridView to ListBox Drag Drop
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Drag_and_Drop
{
public partial class Form1 : Form
{
string sSelectedVal = "";
public Form1()
{
InitializeComponent();
}
private void lstBx_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Text))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
private void lstBx_DragDrop(object sender, DragEventArgs e)
{
lstBx.Items.Add(sSelectedVal);
}
private void Form1_Load(object sender, EventArgs e)
{
FillGrids();
}
private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
sSelectedVal = dataGridView1.SelectedCells[0].Value.ToString();
dataGridView1.DoDragDrop(sSelectedVal, DragDropEffects.Copy | DragDropEffects.Move);
}
private void dataGridView2_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
sSelectedVal = dataGridView2.SelectedCells[0].Value.ToString();
dataGridView2.DoDragDrop(sSelectedVal, DragDropEffects.Copy | DragDropEffects.Move);
}
private void FillGrids()
{
List<Student> students = new List<Student>()
{
new Student{Roll = 1,Name="Rakesh" },
new Student{Roll = 2,Name="Suresh" },
new Student{Roll = 3,Name="Rajesh"},
new Student{Roll = 4,Name="Harish" }
};
dataGridView1.DataSource = students;
List<Teacher> teachers = new List<Teacher>()
{
new Teacher{id = 1,Name="dgg" },
new Teacher{id = 2,Name="gfdgg" },
new Teacher{id = 3,Name="gfdgg"},
new Teacher{id = 4,Name="gfdg" }
};
dataGridView2.DataSource = teachers;
}
}
public class Student
{
public int Roll { get; set; }
public string Name { get; set; }
}
public class Teacher
{
public int id { get; set; }
public string Name { get; set; }
}
}
Tuesday, 11 November 2014
Javascript show file content
<head>
<script language="javascript">
function ShowMyFile(path) {
var sd = window.open(path);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<a href="#" onclick="ShowMyFile('Files/Hello.pdf'); return false;">Show file</a>
</div>
</form>
</body>
</html>
<script language="javascript">
function ShowMyFile(path) {
var sd = window.open(path);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<a href="#" onclick="ShowMyFile('Files/Hello.pdf'); return false;">Show file</a>
</div>
</form>
</body>
</html>
Thursday, 6 November 2014
CRUD asp.net
CREATE TABLE [dbo].[DocMaster](
[ID] [int] IDENTITY(1,1) NOT NULL,
[DocID] [varchar](12) NOT NULL,
[DocName] [varchar](30) NOT NULL,
[DocVersion] [varchar](50) NOT NULL,
[ModifiedBy] [varchar](12) NOT NULL,
[ModifiedDt] [datetime] NOT NULL,
CONSTRAINT [PK_DocMaster] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
Stored Proc:
ALTER PROCEDURE [dbo].[InsertDoc]
@DocID varchar(12),
@DocName varchar(30),
@DocVersion varchar(12),
@ModifiedBy varchar(12),
@ModifiedDt DateTime
AS
BEGIN
insert into DocMaster
values(@DocID,@DocName,@DocVersion,@ModifiedBy,@ModifiedDt)
END
**********************************************
ALTER PROCEDURE [dbo].[SelectDoc]
@DocID varchar(12)
AS
BEGIN
Select * from DocMaster
Where DocID = @DocID
END
********************************************
ALTER PROCEDURE [dbo].[UpdateDoc]
@DocID varchar(12),
@DocName varchar(30),
@DocVersion varchar(12),
@ModifiedBy varchar(12),
@ModifiedDt DateTime
AS
BEGIN
Update DocMaster
set DocName=@DocName,DocVersion = @DocVersion,ModifiedBy =@ModifiedBy,ModifiedDt=@ModifiedDt
Where DocID = @DocID
END
**********************
protected void btnSave_Click(object sender, EventArgs e)
{
string cnStr = ConfigurationManager.ConnectionStrings["ConStr"].ToString();
using (SqlConnection cn = new SqlConnection(cnStr))
{
try
{
if (cn.State == ConnectionState.Closed)
{
cn.Open();
}
SqlParameter prmDocID = new SqlParameter("@DocID", txtDocID.Text);
SqlParameter prmDocName = new SqlParameter("@DocName", txtDocName.Text);
SqlParameter prmDocVsn = new SqlParameter("@DocVersion", txtDocVsn.Text);
SqlParameter prmModBy = new SqlParameter("@ModifiedBy", txtModifiedBy.Text);
SqlParameter prmModDt = new SqlParameter("@ModifiedDt", DateTime.Parse(txtModifiedDt.Text));
SqlCommand cmd = new SqlCommand("InsertDoc", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(prmDocID);
cmd.Parameters.Add(prmDocName);
cmd.Parameters.Add(prmDocVsn);
cmd.Parameters.Add(prmModBy);
cmd.Parameters.Add(prmModDt);
cmd.ExecuteNonQuery();
}
catch(Exception ex)
{
}
finally
{
cn.Close();
}
}
}
*********************************************************************
protected void btnUpdate_Click(object sender, EventArgs e)
{
using (SqlConnection cn = new SqlConnection(cnStr))
{
try
{
if (cn.State == ConnectionState.Closed)
{
cn.Open();
}
SqlParameter prmDocID = new SqlParameter("@DocID", strDocID);
SqlParameter prmDocName = new SqlParameter("@DocName", lblDocName.Text);
SqlParameter prmDocVsn = new SqlParameter("@DocVersion", lblDocVsn.Text);
SqlParameter prmModBy = new SqlParameter("@ModifiedBy", lblModifiedBy.Text);
SqlParameter prmModDt = new SqlParameter("@ModifiedDt", DateTime.Parse(lblModifiedDt.Text));
SqlCommand cmd = new SqlCommand("UpdateDoc", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(prmDocID);
cmd.Parameters.Add(prmDocName);
cmd.Parameters.Add(prmDocVsn);
cmd.Parameters.Add(prmModBy);
cmd.Parameters.Add(prmModDt);
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
}
finally
{
cn.Close();
//Redirect to edit page
Response.Redirect("~/View.aspx?DocID=" + lblDocID.Text.Trim());
}
}
}
*********************************************************************
private void Show(string strDocID)
{
using (SqlConnection cn = new SqlConnection(cnStr))
{
try
{
if (cn.State == ConnectionState.Closed)
{
cn.Open();
}
SqlParameter prmDocID = new SqlParameter("@DocID", strDocID);
SqlCommand cmd = new SqlCommand("SelectDoc", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(prmDocID);
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
lblDocID.Text = sdr["DocID"].ToString();
lblDocName.Text = sdr["DocName"].ToString();
lblDocVsn.Text = sdr["DocVersion"].ToString();
lblModifiedBy.Text = sdr["ModifiedBy"].ToString();
lblModifiedDt.Text = sdr["ModifiedDt"].ToString();
}
}
catch (Exception ex)
{
}
finally
{
cn.Close();
}
}
}
*********************************
<configuration>
<connectionStrings>
<add name="ConStr"
connectionString="server=banka-pc;uid=sa;pwd=banka;database=accnt"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
Subscribe to:
Posts (Atom)


