Pages

Wednesday 19 November 2014

Binding sql data to the gridview and filter the data based on dropdown selection and Cascading.

webform1

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<title></title>

</head>

<body>

<form id="form1" runat="server">

<div>




 
<br />

<asp:Label ID="Label2" runat="server" Text="Designation "></asp:Label>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">

</asp:DropDownList>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

<asp:Label ID="Label3" runat="server" Text="Employees "></asp:Label>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

<asp:DropDownList ID="DropDownList2" runat="server">

</asp:DropDownList>

<br />

<br />

<asp:GridView ID="GridView1" runat="server" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2">

<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />

<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />

<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />

<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />

<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />

<SortedAscendingCellStyle BackColor="#FFF1D4" />

<SortedAscendingHeaderStyle BackColor="#B95C30" />

<SortedDescendingCellStyle BackColor="#F1E5CE" />

<SortedDescendingHeaderStyle BackColor="#93451F" />

</asp:GridView>

<br />

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>




 
<br />

<br />

<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Add" />

<br />

<asp:Label ID="Label4" runat="server" Text="Users "></asp:Label>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

<asp:DropDownList ID="DropDownList3" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList3_SelectedIndexChanged">

</asp:DropDownList>

<asp:GridView ID="GridView2" runat="server" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2">

<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />

<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />

<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />

<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />

<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />

<SortedAscendingCellStyle BackColor="#FFF1D4" />

<SortedAscendingHeaderStyle BackColor="#B95C30" />

<SortedDescendingCellStyle BackColor="#F1E5CE" />

<SortedDescendingHeaderStyle BackColor="#93451F" />

</asp:GridView>




 
</div>

</form>

</body>

</html>

webform1.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;

using System.Xml;

using System.Xml.Linq;

using System.IO;

namespace WebApplication2



{
 
public partial class WebForm1 : System.Web.UI.Page



{
 
int desgid = 0;

SqlConnection con = new SqlConnection("Data Source=abc;Initial Catalog=master;Trusted_Connection=True;");

DataSet ds;

protected void Page_Load(object sender, EventArgs e)



{
 
if (!IsPostBack)



{

FillCombo();

FillComboConnectionless(desgid);

BindXmlDataCombo();

}

FillGrid(desgid);

BindXmlDataGrid();

}
 
public void XmlData()



{
 
XmlTextReader xmlreader = new XmlTextReader(Server.MapPath("XMLFile1.xml"));

ds = new DataSet();



ds.ReadXml(xmlreader);

xmlreader.Close();



}
 
public void BindXmlDataGrid()



{

XmlData();

GridView2.DataSource = ds;

GridView2.DataBind();



}
 
public void BindXmlDataCombo()



{

XmlData();

DropDownList3.DataSource = ds;
 
DropDownList3.DataTextField = "Job";

DropDownList3.DataValueField = "Job";



DropDownList3.DataBind();

}
 
public void FillCombo()



{

con.Open();
 
SqlCommand cmd1 = new SqlCommand("select 0 desgid,'All' desgname union select desgid,desgname from desg", con);

SqlDataReader dr = cmd1.ExecuteReader();



DropDownList1.DataSource = dr;
 
DropDownList1.DataTextField = "desgname";

DropDownList1.DataValueField = "desgid";



DropDownList1.DataBind();

con.Close();

}
 
public void FillComboConnectionless(int desgid)



{
 
DataSet ds = new DataSet();

string s = "";

if (desgid == 0)

s = "select 0 empid,'All' empname union select empid,empname from emp";

else

s = "select empid,empname from emp where desgid=" + desgid;

SqlDataAdapter da = new SqlDataAdapter(s, con);



da.Fill(ds);
 
//DropDownList2.DataSource = Enum.GetValues(typeof(Colors));// ds;



DropDownList2.DataSource = ds;
 
DropDownList2.DataTextField = "empname";

DropDownList2.DataValueField = "empid";



DropDownList2.DataBind();

}
 
public void FillGrid(int desgid)



{

con.Open();
 
string s = "";

if (desgid == 0)

s = "select empname,desgname from emp as e inner join desg as d on e.desgid=d.desgid";

else

s = "select empname,desgname from emp as e inner join desg as d on e.desgid=d.desgid where d.desgid=" + desgid;

SqlCommand cmd = new SqlCommand(s, con);

SqlDataReader dr = cmd.ExecuteReader();



GridView1.DataSource = dr;

GridView1.DataBind();
 
Label1.Text = "No of Records in the above table are <b>" + GridView1.Rows.Count + "</b>";



con.Close();

}
 
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)



{
 
FillGrid(Convert.ToInt32(DropDownList1.SelectedValue.ToString()));

FillComboConnectionless(Convert.ToInt32(DropDownList1.SelectedValue.ToString()));



}
 
protected void Button1_Click(object sender, EventArgs e)



{
 
Server.Transfer("WebForm2.aspx");



}
 
class Word



{
 
public string Job { get; set; }

public string FirstName { get; set; }

public string LastName { get; set; }

public string UserName { get; set; }



}
 
protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)



{
 
XDocument doc = XDocument.Load(Server.MapPath("XMLFile1.xml"));

XElement lex = doc.Element("users");

Word[] catWords = null;

if (lex != null)



{
 
IEnumerable<XElement> words = lex.Elements("user");

catWords = (from itm in words

where

itm.Element("FirstName") != null

&& itm.Element("LastName") != null

&& itm.Element("UserName") != null

&& itm.Element("Job").Value==DropDownList3.SelectedItem.ToString()

select new Word()



{
 
FirstName = itm.Element("FirstName").Value,

LastName = itm.Element("LastName").Value,

UserName = itm.Element("UserName").Value,

Job = itm.Element("Job").Value

}).ToArray<Word>();



}
 
//print it

if (catWords != null)



{
 
XmlDocument xdoc = new XmlDocument();

XmlElement mprntelmt = xdoc.CreateElement("users");

XmlElement rprntelmt = xdoc.CreateElement("users");

XmlElement prntelmt = xdoc.CreateElement("user"); ;

foreach (Word itm in catWords)



{
 
prntelmt = xdoc.CreateElement("user");

XmlElement fname = xdoc.CreateElement("FirstName");



fname.InnerText = itm.FirstName;
 
XmlElement lname = xdoc.CreateElement("LastName");



lname.InnerText = itm.LastName;
 
XmlElement uname = xdoc.CreateElement("UserName");



uname.InnerText = itm.UserName;
 
XmlElement job = xdoc.CreateElement("Job");



job.InnerText = itm.Job;

prntelmt.AppendChild(fname);

prntelmt.AppendChild(lname);

prntelmt.AppendChild(uname);

prntelmt.AppendChild(job);

rprntelmt.AppendChild(prntelmt);

}

mprntelmt.AppendChild(rprntelmt);
 
string msg = mprntelmt.InnerXml;

StringReader stringReader = new StringReader(msg);

ds = new DataSet();



ds.ReadXml(stringReader);

GridView2.DataSource = ds;

GridView2.DataBind();

}



}
 
//protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)

//{

// FillGrid(Convert.ToInt32(DropDownList2.SelectedValue.ToString()));

//}



}

}
 
webform2

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

<!DOCTYPE html>

<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="First Name"></asp:Label>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

</br>

<asp:Label ID="Label2" runat="server" Text="Last Name"></asp:Label>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>

</br>

<asp:Label ID="Label3" runat="server" Text="User Name"></asp:Label>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>

</br>

<asp:Label ID="Label4" runat="server" Text="Job"></asp:Label>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>

</br>

&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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" />

</div>

</form>

</body>

</html>

webform2.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.Xml;

namespace WebApplication2



{
 
public partial class WebForm2 : System.Web.UI.Page



{
 
protected void Page_Load(object sender, EventArgs e)



{

}
 
protected void Button1_Click(object sender, EventArgs e)



{
 
XmlDocument xdoc = new XmlDocument();

xdoc.Load(Server.MapPath("XMLFile1.xml"));

XmlElement prntelmt = xdoc.CreateElement("user");

XmlElement fname = xdoc.CreateElement("FirstName");



fname.InnerText = TextBox1.Text;
 
XmlElement lname = xdoc.CreateElement("LastName");



lname.InnerText = TextBox2.Text;
 
XmlElement uname = xdoc.CreateElement("UserName");



uname.InnerText = TextBox3.Text;
 
XmlElement job = xdoc.CreateElement("Job");



job.InnerText = TextBox4.Text;

prntelmt.AppendChild(fname);

prntelmt.AppendChild(lname);

prntelmt.AppendChild(uname);

prntelmt.AppendChild(job);

xdoc.DocumentElement.AppendChild(prntelmt);
 
xdoc.Save(Server.MapPath("XMLFile1.xml"));

Server.Transfer("WebForm1.aspx");



}

}

}

output