Friday, July 11, 2008

Create Dinamic Image for Security signup

<%@ WebHandler Language="C#" Class="Image" %>

using System;

using System.Web;

using System.Drawing;

using System.Drawing.Imaging;


using System.Text;

using System.Web.SessionState;

class Image : IHttpHandler, IRequiresSessionState
{public void ProcessRequest(HttpContext context)

{context.Response.ContentType = "image/gif";

Bitmap b = new Bitmap(200, 60);

Graphics g = Graphics.FromImage(b);

g.FillRectangle(new SolidBrush(Color.White), 0, 0, 200, 60);

Font font = new Font(FontFamily.GenericSansSerif, 48, FontStyle.Bold, GraphicsUnit.Pixel);

Random r = new Random();

string letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";

string letter;

StringBuilder Lt = new StringBuilder();

for (int x = 0; x < 5; x++)

{letter = letters.Substring(r.Next(0, letters.Length - 1), 1);

Lt.Append(letter);

g.DrawString(letter, font, new SolidBrush(Color.Black), x * 38, r.Next(0, 15));

}

Pen linepen = new Pen(new SolidBrush(Color.Black), 2);

for (int x = 0; x < 6; x++)

g.DrawLine(linepen, new Point(r.Next(0, 199), r.Next(0, 59)), new Point(r.Next(0, 199), r.Next(0, 59)));

b.Save(context.Response.OutputStream, ImageFormat.Gif);

context.Session["image"] =Lt;

context.Response.End();

}

public bool IsReusable

{get{return true;}

}

}

Recursively find control from previous page

hi

i study more time asp.net starter kit and i found best code for finding previous page server control.

But from prevoius page you must use postbackurl on button or use server.transfer("go.aspx");

The code is follow........

public sealed class Util{

private Util(){}

public static Control FindControlRecursively(string controlID, ControlCollection controls){

if (controlID == null || controls == null)

return null;

foreach (Control c in controls){
if (c.ID == controlID)
return c;
if (c.HasControls()){
Control inner = FindControlRecursively(controlID, c.Controls);
if (inner != null)
return inner;}
}

return null;
}

Tuesday, July 8, 2008

Insert, Update, delete, paging in formview


        
        

ID:
Name:
City:
-

Name:
City:
-

Sample Database
ID:
Name:
City:
- -
Page :<%=fv.PageIndex+1%> « Prev Next »
public partial class formvieweditupdate : System.Web.UI.Page
{
SqlConnection dbConn = null;
SqlDataAdapter da = null;
DataTable dTable = null;
String strConnection = null;
String strSQL = null;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
bindData();
}
}
private void bindData()
{
try
{
strConnection = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
dbConn = new SqlConnection(strConnection);
dbConn.Open();
strSQL = "SELECT companyid, companyname,companycity  from companydetail order by companyname";
da = new SqlDataAdapter(strSQL, dbConn);
dTable = new DataTable();
da.Fill(dTable);
fv.DataSource = dTable;
fv.DataBind();
}
finally
{
if (dbConn != null)
{
dbConn.Close();
}
}
}

protected void Update(object sender, FormViewUpdateEventArgs e)
{
try
{
int cid = Convert.ToInt32((fv.Row.FindControl("theIDLabel1") as Label).Text);
string cname = (fv.Row.FindControl("theNameTextBox") as TextBox).Text;
string ccity = (fv.Row.FindControl("theCityTextBox") as TextBox).Text;
strConnection = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
dbConn = new SqlConnection(strConnection);
dbConn.Open();
SqlCommand   da = new SqlCommand( "Update companydetail set companyname=@comp,companycity=@comcity where companyid=@cid", dbConn);
da.Parameters.Add("@cid", SqlDbType.Int).Value = cid;
da.Parameters.Add("@comp", SqlDbType.VarChar).Value = cname;
da.Parameters.Add("@comcity", SqlDbType.VarChar).Value = ccity;
da.ExecuteNonQuery();
fv.ChangeMode(FormViewMode.ReadOnly);
}

finally
{
if (dbConn != null)
{
dbConn.Close();
bindData();
}
}

}
protected void Insert(object sender, FormViewInsertEventArgs e)
{
try
{

string cname = (fv.Row.FindControl("theNameTextBox") as TextBox).Text;
string ccity = (fv.Row.FindControl("theCityTextBox") as TextBox).Text;
strConnection = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
dbConn = new SqlConnection(strConnection);
dbConn.Open();
SqlCommand da = new SqlCommand("insert into companydetail values(@comp,@add,@comcity)", dbConn);
da.Parameters.Add("@add", SqlDbType.VarChar).Value ="";
da.Parameters.Add("@comp", SqlDbType.VarChar).Value = cname;
da.Parameters.Add("@comcity", SqlDbType.VarChar).Value = ccity;
da.ExecuteNonQuery();
fv.ChangeMode(FormViewMode.ReadOnly);
}

finally
{

if (dbConn != null)
{
dbConn.Close();
bindData();
}
}

}
protected void Delete(object sender, FormViewDeleteEventArgs e)
{
try
{
int cid = Convert.ToInt32((fv.Row.FindControl("theIDLabel") as Label).Text);
strConnection = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
dbConn = new SqlConnection(strConnection);
dbConn.Open();
SqlCommand da = new SqlCommand("delete from companydetail where companyid=@cid", dbConn);
da.Parameters.Add("@cid", SqlDbType.Int).Value = cid;
da.ExecuteNonQuery();
fv.ChangeMode(FormViewMode.ReadOnly);
}

finally
{

if (dbConn != null)
{
dbConn.Close();
bindData();
}
}

}
protected void ChangMode(object sender, FormViewModeEventArgs e)
{
fv.ChangeMode(e.NewMode);
if (e.NewMode.ToString() != "Edit" && e.NewMode.ToString()!="Insert")
{
bindData();
}

}

protected void pagechanged(object sender, FormViewPageEventArgs e)
{
fv.PageIndex = e.NewPageIndex;
bindData();
}
}

Monday, July 7, 2008

Paging in Gridview using PagerTemplate










« First

< Prev
[Records <%= gv.PageIndex * gv.PageSize%>-<%= gv.PageIndex * gv.PageSize + gv.PageSize - 1%>]

Next >

Last »


fix header in repeater and datalist


LocalityidLocalityname
<%#Eval("localityid") %><%#Eval("localityname") %>
code behind code
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
SqlDataAdapter cmd = new SqlDataAdapter("select top 100 * from locality", con);
cmd.SelectCommand.CommandType = CommandType.Text;
DataSet ds = new DataSet();
cmd.Fill(ds);
dl.DataSource = ds;
dl.DataBind();

}

Friday, July 4, 2008

register hidden field and its value in code file

ClientScript.RegisterHiddenField(

"hd",pageState.ToString());

where hd is hidden field name and pagestate in numeric variable which contian value

Edit,Update,delete in gridview using Xml file





TemplateField HeaderText="empId" >




TextBox ID="txtid" runat =server Text='<%#Eval("empid") %>'>
EditItemTemplate>
TemplateField>
TemplateField HeaderText="empname" >




TextBox ID="txtname" runat =server Text='<%#Eval("empname") %>'>
EditItemTemplate>
TemplateField>
empcity" >




TextBox ID="txtcity" runat =server Text='<%#Eval("empcity") %>'>


empsalary" >




TextBox ID="txtsalary" runat =server Text='<%#Eval("empsalary") %>'>


CommandField ShowEditButton="True" />
CommandField ShowDeleteButton="True" />















codefile

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
binddata();
}
}
void binddata()
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("empdata.xml"));
if (ds != null && ds.HasChanges())
{
gv.DataSource = ds;
gv.DataBind();
}
else
{
gv.DataBind();
}
}
protected void Editdata(object s, GridViewEditEventArgs e)
{
gv.EditIndex = e.NewEditIndex;
binddata();
}
protected void Deletedata(object s, GridViewDeleteEventArgs e)
{
binddata();
DataSet ds= gv.DataSource as DataSet;
ds.Tables[0].Rows[gv.Rows[e.RowIndex].DataItemIndex].Delete();
ds.WriteXml(Server.MapPath("empdata.xml"));
binddata ();
}
protected void Canceldata(object s, GridViewCancelEditEventArgs e)
{
gv.EditIndex = -1;
binddata();
}
protected void Updatedata(object s, GridViewUpdateEventArgs e)
{

int i =gv.Rows[e.RowIndex].DataItemIndex;
string id=(gv.Rows[e.RowIndex].FindControl("txtid") as TextBox).Text;
string name=(gv.Rows[e.RowIndex].FindControl("txtname") as TextBox).Text;
string city=(gv.Rows[e.RowIndex].FindControl("txtcity") as TextBox).Text;
string salary=(gv.Rows[e.RowIndex].FindControl("txtsalary") as TextBox).Text;
gv.EditIndex = -1;
binddata();
DataSet ds =(DataSet) gv.DataSource;
ds.Tables[0].Rows[i]["empid"] = id;
ds.Tables[0].Rows[i]["empname"] = name;
ds.Tables[0].Rows[i]["empcity"] = city;
ds.Tables[0].Rows[i]["empsalary"] = salary;
ds.WriteXml(Server.MapPath("empdata.xml"));
binddata();
}
protected void pageddata(object s, GridViewPageEventArgs e)
{
gv.PageIndex = e.NewPageIndex;
binddata();
}

protected void insert(object sender, EventArgs e)
{
binddata();
DataSet ds = gv.DataSource as DataSet;
DataRow dr = ds.Tables[0].NewRow();
dr[0] = empId.Text;
dr[1] = empName.Text;
dr[2] = empcity.Text;
dr[3] = empsalary.Text;
ds.Tables[0].Rows.Add(dr);
ds.AcceptChanges();
ds.WriteXml(Server.MapPath("empdata.xml"));
binddata();
empId.Text = string.Empty;
empcity.Text = string.Empty;
empName.Text = string.Empty;
empsalary.Text = string.Empty;
}
xml file









Thursday, July 3, 2008

Set dropdownlist value from querystring in javascript



    
delhi mumbai channai kolkota

Tuesday, July 1, 2008

Display Rss data througth Javascript

try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}function displayRSS(url)
{xmlhttp=GetXmlHttpObject();
if(xmlHttp==null)
{return;}
xmlHttp.open("GET",url,true);
xmlHttp.onreadystatechange=getRssFormat;
xmlHttp.send(null);
}
function getRssFormat()

{if(xmlHttp.readyState==4)
{if(xmlHttp.status != 200)
{
document.getElementById("rss").innerHTML= "Sorry....The Server did not return any results (Result code: 200). Please try again Sorry for the Inconvenience";
}
else{
var items_count=3;
link=new Array(), title=new  Array(), pubDate=new Array()
for(var  i=0; i0)?title[i].firstChild.nodeValue:"Untitled";
link_w=(link.length>0)?link[i].firstChild.nodeValue:"";
pubDate_w=pubDate[i];
litag = document.createElement("li");
linktag =document.createElement("a");
linktag.setAttribute("href",link_w);
linktag.appendChild(document.createTextNode(title_w + " (" + pubDate_w + ")"));
litag.appendChild(linktag);
ul.appendChild(litag);
}}}
if(xmlHttp.readyState==1 || xmlHttp.readyState==2 || xmlHttp.readyState==3)
{document.getElementById("rss").innerHTML='Please Wait....';
}}
AJAX, asp, Asp.net, asp.net and sql server security, Asp.net IntemIndex, C#, Css, DataBinder.Eval, DataKeyNames, Datalist, Datapager, DataSet, DataTable, DropDownList, FindControl, gridview, JavaScript, jquery, Listview, Paging, Regex, RegularExpression, Repeater, Server side validation, Sql Server, timer, timercallback, Validation, XML, xmlnode, XPath