26 August 2011

GridView data filter with StoredProcedure

SqlCommand cmd=new SqlCommand();

SqlConnection con = new SqlConnection("..your connection string....");
SqlDataAdapter da=new SqlDataAdapter();
DataSet ds=new DataSet();
cmd.Parameters.Clear();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "sp_getall";
cmd.Connection = con;
cmd.Parameters.Add("@p", SqlDbType.VarChar, 20).Value = txtSearch.Text;
da = new SqlDataAdapter(cmd);
da.Fill(ds);
DataView dv = ds.Tables[0].DefaultView;
dv.RowFilter = "eName like '" + mr+"'";
gvsearch.DataSource = dv;
gvsearch.DataBind();

create  proc sp_getall
as
begin
select
* from tblemp
end

GridView data filter with StoredProcedure

SqlCommand cmd=new SqlCommand();

SqlConnection con = new SqlConnection("..your connection string....");
SqlDataAdapter da=new SqlDataAdapter();
DataSet ds=new DataSet();
cmd.Parameters.Clear();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "sp_getall";
cmd.Connection = con;
cmd.Parameters.Add("@p", SqlDbType.VarChar, 20).Value = txtSearch.Text;
da = new SqlDataAdapter(cmd);
da.Fill(ds);
DataView dv = ds.Tables[0].DefaultView;
dv.RowFilter = "eName like '" + mr+"'";
gvsearch.DataSource = dv;
gvsearch.DataBind();

create  proc sp_getall
as
begin
select
* from tblemp
end

sp for search



create proc sa_tstname(@p varchar(20))
as
begin
select lastname from geninfo where lastname like @p+'%'
end
exec sa_tstname c

25 August 2011

sp for search



create proc sa_tstname(@p varchar(20))
as
begin
select lastname from geninfo where lastname like @p+'%'
end
exec sa_tstname c

24 August 2011

checkbox check/uncheck all in gridview


static int kount=0;
protected void chkHead_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox chhead = (CheckBox)gv.HeaderRow.FindControl("chkHead");
        foreach (GridViewRow gr in gv.Rows)
        {
            CheckBox ch = (CheckBox)gr.Cells[1].FindControl("chkItem");
            if (chhead.Checked)
                ch.Checked = true;
            else
                ch.Checked = false;
        }
    }



protected void chkItem_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox chhead = (CheckBox)gv.HeaderRow.FindControl("chkhead");
        if (gv.Rows.Count == 1)
        {
            CheckBox sinch = (CheckBox)gv.Rows[0].FindControl("chkItem");
            if (sinch.Checked)
                chhead.Checked = true;
            else
                chhead.Checked = false;
        }
        else
        {
            foreach (GridViewRow gr in gv.Rows)
            {
                CheckBox ch = (CheckBox)gr.FindControl("chkItem");
                if (ch.Checked)
                    kount++;
            }
            if (kount == gv.Rows.Count)
                chhead.Checked = true;
            else
            {
                kount = 0;
                chhead.Checked = false;
            }
        }
    }


23 August 2011

checkbox check/uncheck all in gridview

protectedvoid chkHead_CheckedChanged(object sender, EventArgs e)
{
CheckBox chhead = (CheckBox)gv.HeaderRow.FindControl("chkHead");foreach (GridViewRow gr in gv.Rows)
{
CheckBox ch = (CheckBox)gr.Cells[1].FindControl("chkItem");if (chhead.Checked)
ch.Checked =true;
else
ch.Checked =false;
}
}
protectedvoid chkItem_CheckedChanged(object sender, EventArgs e)
{
CheckBox chhead = (CheckBox)gv.HeaderRow.FindControl("chkhead");if (gv.Rows.Count == 1)
{
CheckBox sinch = (CheckBox)gv.Rows[0].FindControl("chkItem");if (sinch.Checked)
chhead.Checked =true;
else
chhead.Checked =false;
}
else
{
foreach (GridViewRow gr in gv.Rows)
{
CheckBox ch = (CheckBox)gr.FindControl("chkItem");if (ch.Checked)
kount++;
}
if (kount == gv.Rows.Count)
chhead.Checked =true;
else
{
kount = 0;
chhead.Checked =false;
}
}
}

checkbox check/uncheck all in gridview


static int kount=0;
protected void chkHead_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox chhead = (CheckBox)gv.HeaderRow.FindControl("chkHead");
        foreach (GridViewRow gr in gv.Rows)
        {
            CheckBox ch = (CheckBox)gr.Cells[1].FindControl("chkItem");
            if (chhead.Checked)
                ch.Checked = true;
            else
                ch.Checked = false;
        }
    }



protected void chkItem_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox chhead = (CheckBox)gv.HeaderRow.FindControl("chkhead");
        if (gv.Rows.Count == 1)
        {
            CheckBox sinch = (CheckBox)gv.Rows[0].FindControl("chkItem");
            if (sinch.Checked)
                chhead.Checked = true;
            else
                chhead.Checked = false;
        }
        else
        {
            foreach (GridViewRow gr in gv.Rows)
            {
                CheckBox ch = (CheckBox)gr.FindControl("chkItem");
                if (ch.Checked)
                    kount++;
            }
            if (kount == gv.Rows.Count)
                chhead.Checked = true;
            else
            {
                kount = 0;
                chhead.Checked = false;
            }
        }
    }


22 August 2011

how to know the current system name and ip address in asp.net

Response.Write(Request.ServerVariables["REMOTE_ADDR"].ToString()+"
"
);
Response.Write(
HttpContext.Current.Server.MachineName);

grid view sorting and paging in asp.net using C#.net code

protected void gv_Sorting(object sender, GridViewSortEventArgse)
{
DataSet ds = newDataSet();

ds = bal.getdata();

ds.Tables[0].DefaultView.Sort = e.SortExpression +
" " + GetSortDirection(e.SortExpression);
gv.DataSource = ds.Tables[0].DefaultView;

gv.DataBind();

}
privatestring GetSortDirection(string column)
{
string sortDirection = "ASC";
ViewState["SortExpression"] = column;
ViewState["SortDirection"] = sortDirection;string sortExpression = ViewState["SortExpression"] asstring;if (sortExpression != null)
{
string lastDirection = ViewState["SortDirection"] asstring;if ((lastDirection != null) && (lastDirection == "ASC"))
{

sortDirection ="DESC";
}

}
return sortDirection;
}
protectedvoid gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gv.PageIndex = e.NewPageIndex;
getdata();
}

how to know the current system name and ip address in asp.net

Response.Write(Request.ServerVariables["REMOTE_ADDR"].ToString()+"<br/>");
Response.Write(
HttpContext.Current.Server.MachineName);

grid view sorting and paging in asp.net using C#.net code

protected void gv_Sorting(object sender, GridViewSortEventArgse)
{
DataSet ds = newDataSet();

ds = bal.getdata();

ds.Tables[0].DefaultView.Sort = e.SortExpression +
" " + GetSortDirection(e.SortExpression);
gv.DataSource = ds.Tables[0].DefaultView;

gv.DataBind();

}
privatestring GetSortDirection(string column)
{
string sortDirection = "ASC";
ViewState["SortExpression"] = column;
ViewState["SortDirection"] = sortDirection;string sortExpression = ViewState["SortExpression"] asstring;if (sortExpression != null)
{
string lastDirection = ViewState["SortDirection"] asstring;if ((lastDirection != null) && (lastDirection == "ASC"))
{

sortDirection ="DESC";
}

}
return sortDirection;
}
protectedvoid gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gv.PageIndex = e.NewPageIndex;
getdata();
}

Grid view sorting in asp.net using code

SqlConnection con = new SqlConnection("Data Source=...;Initial Catalog=...;Persist Security Info=True;User ID=sa;Password=...");
SqlDataAdapter da;
DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
GetData();
}
void GetData()
{
DataTable dt = new DataTable();
da = new SqlDataAdapter("select * from sample",con);
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dt = new DataTable();
da = new SqlDataAdapter("select * from sample", con);
da.Fill(dt);
dt.DefaultView.Sort = e.SortExpression + direction();

GridView1.DataSource = dt;
GridView1.DataBind();
}
string direction()
{
if (ViewState["stat"] != null && ViewState["stat"].ToString().Trim() == "desc")
{
ViewState["stat"] = " asc";
return " asc";
}
else
{
ViewState["stat"] = " desc";
return " desc";
}
}

18 August 2011

xml read, write, modify data in asp.net

wsex.aspx.cs:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;

public partial class wsex : System.Web.UI.Page
{
    string xmlfile = @"C:\vision\first\emp.xml";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            getdata();
    }
    void getdata()
    {
        wsxml ws = new wsxml();
        GridView1.DataSource = ws.getdata();
        GridView1.DataBind();
    }

    protected void Button1_Click(object sender, EventArgs e)//insert
    {
        XmlDocument xd = new XmlDocument();
        xd.Load(xmlfile);
        XmlElement xe = xd.CreateElement("emp");

        XmlElement id = xd.CreateElement("id");
        id.InnerText = TextBox1.Text;
        xe.AppendChild(id);

        XmlElement nam = xd.CreateElement("name");
        nam.InnerText = TextBox2.Text;
        xe.AppendChild(nam);

        XmlElement ad = xd.CreateElement("add");
        ad.InnerText = TextBox3.Text;
        xe.AppendChild(ad);

        xd.DocumentElement.AppendChild(xe);
        xd.Save(xmlfile);
        Response.Redirect("~/wsex.aspx");
     
    }
    protected void Button2_Click(object sender, EventArgs e)//update
    {
        DataSet ds = new DataSet();
        ds.ReadXml(Server.MapPath("emp.xml"));
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            if (TextBox1.Text == ds.Tables[0].Rows[i]["id"].ToString())
            {
                ds.Tables[0].Rows[i]["name"] = TextBox2.Text;
                ds.Tables[0].Rows[i]["add"] = TextBox3.Text;
                ds.WriteXml(Server.MapPath("emp.xml"));
            }
        }
        Response.Redirect("~/wsex.aspx");
    }
    protected void Button3_Click(object sender, EventArgs e)//delete
    {
        DataSet ds = new DataSet();
        ds.ReadXml(Server.MapPath("emp.xml"));
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            if (TextBox1.Text == ds.Tables[0].Rows[i]["id"].ToString())
            {
                ds.Tables[0].Rows[i].Delete();
                ds.WriteXml(Server.MapPath("emp.xml"));
            }
        }
        Response.Redirect("~/wsex.aspx");
    }
}

emp.xml:
 
    1
    s
    s
 
 
    2
    b
    che
 
 
    3
    c
    ban
 


wsxml:(webservice class)

 [WebMethod]
    public DataSet getdata()
    {
        DataSet ds = new DataSet();
        ds.ReadXml(Server.MapPath("emp.xml"));
        return ds;
    }

17 August 2011

xml read, write, modify data in asp.net

wsex.aspx.cs:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;

public partial class wsex : System.Web.UI.Page
{
string xmlfile = @"C:\vision\first\emp.xml";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
getdata();
}
void getdata()
{
wsxml ws = new wsxml();
GridView1.DataSource = ws.getdata();
GridView1.DataBind();
}

protected void Button1_Click(object sender, EventArgs e)//insert
{
XmlDocument xd = new XmlDocument();
xd.Load(xmlfile);
XmlElement xe = xd.CreateElement("emp");

XmlElement id = xd.CreateElement("id");
id.InnerText = TextBox1.Text;
xe.AppendChild(id);

XmlElement nam = xd.CreateElement("name");
nam.InnerText = TextBox2.Text;
xe.AppendChild(nam);

XmlElement ad = xd.CreateElement("add");
ad.InnerText = TextBox3.Text;
xe.AppendChild(ad);

xd.DocumentElement.AppendChild(xe);
xd.Save(xmlfile);
Response.Redirect("~/wsex.aspx");

}
protected void Button2_Click(object sender, EventArgs e)//update
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("emp.xml"));
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if (TextBox1.Text == ds.Tables[0].Rows[i]["id"].ToString())
{
ds.Tables[0].Rows[i]["name"] = TextBox2.Text;
ds.Tables[0].Rows[i]["add"] = TextBox3.Text;
ds.WriteXml(Server.MapPath("emp.xml"));
}
}
Response.Redirect("~/wsex.aspx");
}
protected void Button3_Click(object sender, EventArgs e)//delete
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("emp.xml"));
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if (TextBox1.Text == ds.Tables[0].Rows[i]["id"].ToString())
{
ds.Tables[0].Rows[i].Delete();
ds.WriteXml(Server.MapPath("emp.xml"));
}
}
Response.Redirect("~/wsex.aspx");
}
}
emp.xml:


<?xml version="1.0" standalone="yes"?>

<employee>

  <emp>

    <id>1</id>

    <name>s</name>

    <add>s</add>

  </emp>

  <emp>

    <id>2</id>

    <name>b</name>

    <add>che</add>

  </emp>

  <emp>

    <id>3</id>

    <name>c</name>

    <add>ban</add>

  </emp>

</employee>




wsxml:(webservice class)



[WebMethod]

    public DataSet getdata()

    {

        DataSet ds = new DataSet();

        ds.ReadXml(Server.MapPath("emp.xml"));

        return ds;

    }

xml read, write, modify data in asp.net

wsex.aspx.cs:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;

public partial class wsex : System.Web.UI.Page
{
    string xmlfile = @"C:\vision\first\emp.xml";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            getdata();
    }
    void getdata()
    {
        wsxml ws = new wsxml();
        GridView1.DataSource = ws.getdata();
        GridView1.DataBind();
    }

    protected void Button1_Click(object sender, EventArgs e)//insert
    {
        XmlDocument xd = new XmlDocument();
        xd.Load(xmlfile);
        XmlElement xe = xd.CreateElement("emp");

        XmlElement id = xd.CreateElement("id");
        id.InnerText = TextBox1.Text;
        xe.AppendChild(id);

        XmlElement nam = xd.CreateElement("name");
        nam.InnerText = TextBox2.Text;
        xe.AppendChild(nam);

        XmlElement ad = xd.CreateElement("add");
        ad.InnerText = TextBox3.Text;
        xe.AppendChild(ad);

        xd.DocumentElement.AppendChild(xe);
        xd.Save(xmlfile);
        Response.Redirect("~/wsex.aspx");
     
    }
    protected void Button2_Click(object sender, EventArgs e)//update
    {
        DataSet ds = new DataSet();
        ds.ReadXml(Server.MapPath("emp.xml"));
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            if (TextBox1.Text == ds.Tables[0].Rows[i]["id"].ToString())
            {
                ds.Tables[0].Rows[i]["name"] = TextBox2.Text;
                ds.Tables[0].Rows[i]["add"] = TextBox3.Text;
                ds.WriteXml(Server.MapPath("emp.xml"));
            }
        }
        Response.Redirect("~/wsex.aspx");
    }
    protected void Button3_Click(object sender, EventArgs e)//delete
    {
        DataSet ds = new DataSet();
        ds.ReadXml(Server.MapPath("emp.xml"));
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            if (TextBox1.Text == ds.Tables[0].Rows[i]["id"].ToString())
            {
                ds.Tables[0].Rows[i].Delete();
                ds.WriteXml(Server.MapPath("emp.xml"));
            }
        }
        Response.Redirect("~/wsex.aspx");
    }
}

emp.xml:
<?xml version="1.0" standalone="yes"?>
<employee>
  <emp>
    <id>1</id>
    <name>s</name>
    <add>s</add>
  </emp>
  <emp>
    <id>2</id>
    <name>b</name>
    <add>che</add>
  </emp>
  <emp>
    <id>3</id>
    <name>c</name>
    <add>ban</add>
  </emp>
</employee>


wsxml:(webservice class)

 [WebMethod]
    public DataSet getdata()
    {
        DataSet ds = new DataSet();
        ds.ReadXml(Server.MapPath("emp.xml"));
        return ds;
    }