Thursday, May 28, 2009

Restrict User to Copying Text

Just Ahead of Body tag paste the following Code oncontextmenu restrict to you to right click in to the textbox oncopy return false will not allow you to copy and cut and onpaste will not allow you to paste and text from outside the page

<body oncontextmenu="return false" oncopy="return false;" onpaste="return false;" oncut="return false;"/>

Friday, May 22, 2009

Javascript Timer Control

//Just Paste the following code in between the script tag

<script type="text/javascript" language="javascript"/>
var hour=11;

var minutes=59;

var seconds=59;

function timedCount()

{

//Textbox name is txt and time would be started in the textbox

document.getElementById('txt').value=hour+":"+minutes+":"+seconds;

if(hour==11 && minutes==58 && seconds==59)

{

//as soon as the time reach 11:58:59 it will transfer itself to another page Default.aspx

window.location.href='Default.aspx';

}

else if(seconds==0)

{

minutes-=1;

seconds=60;

}

if(minutes==01)

{

hour-=1;

minutes=59;

seconds=60;

}

seconds-=1;

//timedcount function decrase by one second which is mentioned here in Millisecond

t=setTimeout("timedCount()",1000);

}

</script>

Thursday, May 21, 2009

Transferring Data From Excel Sheet to Access

protected void Page_Load(object sender, EventArgs e)

{

//Calling Cotacts.mdb(Access Database) from the App_Data Folder

string Access = Server.MapPath("App_Data/contact.mdb");

//Calling Book.xls(Excel Sheet) from the Root Folder

string Excel = Server.MapPath("Book.xls");

//Connection string For Excel Sheet

string connect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Excel + ";Extended Properties=Excel 8.0;";

using (OleDbConnection conn = new OleDbConnection(connect))

{

using (OleDbCommand cmd = new OleDbCommand())

{

try

{

cmd.Connection = conn;

//Inserting Data into the Access Database fromthe Excel Sheet where Excel Sheet Name is Sheet1

cmd.CommandText = "INSERT INTO [MS Access;Database=" + Access + "].[Person] SELECT * FROM [Sheet1$]";

conn.Open();

cmd.ExecuteNonQuery();

}

catch (Exception ex)

{

Response.Write("Your error is " + ex.Message);

}

}

}

}

Gmail setting using Asp.Net

/*On design view Drop the Four Text Box And Button for sending Mail

1)From(For Name)

2)To (Recepient Email Address)

3)Subject

4)Body*/


protected void btnMail_Click(object sender, EventArgs e)

{

SmtpClient mailClient = new SmtpClient("smtp.gmail.com", 587);

mailClient.EnableSsl = true;

//In Network Credential give ur UserName and Password

NetworkCredential cred = new NetworkCredential("sheeban101pk@gmail.com", "xxxxxx");

mailClient.Credentials = cred;


try

{

mailClient.Send(tbxFrom.Text.Trim(), tbxTo.Text.Trim(), tbxSubject.Text.Trim(), tbxBody.Text.Trim());

Response.Write("Your Email has been sent sucessfully -Thank You");

}

catch (Exception exc)

{

Response.Write("Send failure: " + exc.ToString());

}

}

Total value of Gridview in Footer

//Get the two float values for two Gridview Columns and initialize it


float total1 = 0,total2=0;



//On page load Bind the Employee Salary Function with DataGrid


protected void Page_Load(object sender, EventArgs e)

{

DataGrid1.DataSource = GetEmployeeSalary();

DataGrid1.DataBind();

}

//Getting the data from Database


public DataSet GetEmployeeSalary()

{

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ConnectionString.ToString());

string sqlcmd = "SELECT Name, Bonus, SUM(Salary) as Salary"+

" FROM CustomerSalary"+

" group by Name, Bonus";

SqlCommand cmd = new SqlCommand(sqlcmd, conn);

SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();

da.Fill(ds);

return ds;

}

//Using Item Data Bound to get the total value in footer of Gridview


protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)

{

if (e.Item.ItemType!=ListItemType.Header && e.Item.ItemType!=ListItemType.Footer)

{

total1 += float.Parse(e.Item.Cells[1].Text);

total2 += float.Parse(e.Item.Cells[2].Text);

e.Item.ForeColor = System.Drawing.Color.Blue;

}

else if (e.Item.ItemType == ListItemType.Footer)

{

e.Item.Cells[0].Text = "Total";

e.Item.Cells[1].Text = total1.ToString();

e.Item.Cells[2].Text = total2.ToString();

//e.Item.Cells[3].Text = total.ToString();

}


}