Friday, October 30, 2009

HOw To Install IIS IN windows7 Or Vista

http://www.howtogeek.com/howto/windows-vista/how-to-install-iis-on-windows-vista/

Thursday, October 29, 2009

Login Lock After 3 times failur

void invalid()
{
if(session[i]!=null)
{
i=int.parse(session[i].tostring());

}
i++;
session[i]=i.tostring()
if(i>3)
{
responce.redirect ("aaa.aspx");
session.remove("i");
}
}

Clear All TextBoxes

clear all textboxes in Form

protected void Button1_Click(object sender, EventArgs e)
{

foreach (Control ctrl in Form.Controls)
{
if (ctrl.GetType().ToString() == "System.Web.UI.WebControls.TextBox")
{
((TextBox)ctrl).Text = string.Empty;
}
}
clear all textboxes in Form contain Master Page
//ContentPlaceHolder content1 = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
foreach (Control ctrl in content1.Controls)
{

if (ctrl.GetType().ToString() == "System.Web.UI.WebControls.Panel")
{
foreach (Control ctr in Panel1.Controls)
{
if (ctr.GetType().ToString() == "System.Web.UI.WebControls.TextBox")
((TextBox)ctr).Text = string.Empty;
}
}
}

foreach (Control c in this.Controls)
{
if (c.GetType().ToString () == "TextBox")
((TextBox)c).Text = string.Empty;
}
}

Wednesday, October 28, 2009

Crete Connection In Web .config File

in web.config file:




>

Whre We Required the COnnection:


SqlConnection con = new SqlConnection
(ConfigurationManager.ConnectionStrings["track"].ToString());

Code For Login

Step1:
To Drag The Two Lable s And two Textboxes
Step:2
using System.Data.SqlClient;
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Password=mygo;Persist Security Info=True;User ID=sa;Initial Catalog=eshwer;Data Source=.");
con.Open();
SqlCommand cmd=new SqlCommand("select * from login where username='"+TextBox1.Text+"' and password="+TextBox2.Text+"",con);
SqlDataReader dr=cmd.ExecuteReader();
if (dr.HasRows)
{
Response.Redirect("default2.aspx?username=" + TextBox1.Text);

}
else
{
lblmsg.Text = "invalid username and password";
}
}

Gridview

step:1
Drag A gridview on Form
...
using System.Data.SqlClient;
SqlConnection con = new SqlConnection("Password=mygo;Persist Security Info=True;User ID=sa;Initial Catalog=eshwer;Data Source=.");

protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
BindData();
}

}
protected void BindData()
{
SqlDataAdapter da = new SqlDataAdapter("select * from employee order by emp_no", con);
DataSet ds = new DataSet();
da.Fill(ds, "employee");
//attach dataset to databound
gv.DataSource = ds;
gv.DataMember = "employee";
gv.DataBind();
}
protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
gv.EditIndex = e.NewEditIndex;
BindData();
}
protected void gv_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gv.EditIndex = -1;
BindData();
}
protected void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Label l1=(Label)gv.Rows[e.RowIndex].FindControl("lblid1");
TextBox t2 = (TextBox)gv.Rows[e.RowIndex].FindControl("txtname");
TextBox t3 = (TextBox)gv.Rows[e.RowIndex].FindControl("txtsal");
TextBox t4 = (TextBox)gv.Rows[e.RowIndex].FindControl("txt_location");

con.Open();
SqlCommand cmd = new SqlCommand("update employee set emp_name='"+t2.Text+"',emp_sal="+t3.Text+",emp_location='"+t4.Text+"' where emp_no="+l1.Text,con);
cmd.ExecuteNonQuery();
con.Close();
gv.EditIndex = -1;
BindData();

}
protected void gv_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Insert")
{
TextBox t4 = (TextBox)gv.FooterRow.FindControl("ftid");
TextBox t1 = (TextBox)gv.FooterRow.FindControl("ftname");
TextBox t2 = (TextBox)gv.FooterRow.FindControl("ftsal");
TextBox t3 = (TextBox)gv.FooterRow.FindControl("ftlocation");

con.Open();
SqlCommand cmd = new SqlCommand("insert into employee values("+t4.Text+",'"+t1.Text+"',"+t2.Text+",'"+t3.Text+"')",con);
cmd.ExecuteNonQuery();
con.Close();
BindData();
}
}
protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
{

Label l1 = (Label)gv.Rows[e.RowIndex].FindControl("lblid");

con.Open();
SqlCommand cmd = new SqlCommand("delete from employee where emp_no="+l1.Text, con);
cmd.ExecuteNonQuery();
con.Close();
BindData();
}
protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gv.PageIndex = e.NewPageIndex;
BindData();
}

FIles Concept

Reading and Writing Files
To read data to and from files using the System.IO namespace, we primarily use two classes: FileInfo and FileStream. The FileInfo class exposes a number of methods that allow us access to stream-related functions based on a file. These methods simply use the FileStream and related classes to expose this functionality. However, they are useful as you often already have an instance of FileInfo that is specific to a given file. You can then call these methods to return and write to the contents of this file.


Creating file:

using System.IO;



string filename = txtDocumntname.Text;
string str=Txtenterdata .Text ;
FileStream fs = File.Create(Server.MapPath("." + @"\files\"+filename +".txt"));
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(str);
sw.Close();

Downloading File:


protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
retrivefiles();
}
public void retrivefiles()
{
ListBox1 .Items.Clear();
DirectoryInfo dinfo = new DirectoryInfo(Server.MapPath("." + @"\uploadresumes\"));
FileInfo[]fileslist = dinfo.GetFiles();
foreach (FileInfo fi in fileslist)
{
ListBox1.Items.Add (fi.Name);
}
}

string filename = Server.MapPath("." + @"\uploadresumes\" + ListBox1.SelectedItem.Text);
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=" + filename + "");
Response.AddHeader("Content-Length", filename.Length.ToString());
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.word";
Response.WriteFile(filename.ToString());
Response.End();









Viewing file:

protected void btnview_Click(object sender, EventArgs e)
{
ListBox1.Items.Clear();
ListBox1.Visible = true;
string filename =txtfilename .Text ;
StreamReader sr = new StreamReader(Server.MapPath("." + @"\files\" + filename+".txt"));
do
{
ListBox1.Items.Add(sr.ReadLine());
}
while (sr.Peek() != -1);
sr.Close ();
}


Inserting Values Into Table

step:1
Drag Two Lables . And 2 Textboxes And Give Name For Lables Username And Password ,As Same give Textbox Id 's TxtUsername,TxtPassword.
Step2:
.cs File (Cose Window)

using System.Data.SqlClient;
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=chandhu;Integrated Security=True");
protected void Button1_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("insert into Table_1 values('"+TxtUsername .Text +"','"+TxtPassword .Text +"')", con);
cmd.ExecuteNonQuery();
con.Close();

lblmsg.Text = "SuccessFull inserted";
}

Repeter Data Control





Custer Detailes


l

Customer ID:
<%#DataBinder .Eval (Container .DataItem ,"billid") %>

Name:
<%#DataBinder .Eval (Container .DataItem ,"Cname") %>




















using System.Data.SqlClient;
public partial class Default2 : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=music;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{
load();
}
public void load()
{
SqlDataAdapter da = new SqlDataAdapter("select * from account", con);
DataSet ds = new DataSet();
da.Fill (ds);
Repeater1.DataSource = ds;
//Repeater1.DataMember = "account";
Repeater1.DataBind();

}
}

File Upload Control Using

protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{

string s = FileUpload1.FileName;
FileUpload1.SaveAs(Server.MapPath("." + @"\x\" + s));
//FileUpload1.SaveAs(@"D:\raju\"+s);
string t = FileUpload1.PostedFile.ContentLength.ToString();
string u = FileUpload1.PostedFile.ContentType.ToString();

Label1.Text = "filename is:" + s + "
file length:" + t + "
content type:" + u+"
file uploaded Sucessfully";
}
else {
Label1.Text = "plz select file";
}
}

Sending Mail Using SMTP Mail Server


step:1
Design WebForm Following Manner:
protected void btnSend_Click(object sender, EventArgs e)
{
try
{
/* Create a new blank MailMessage with the from and to adreesses*/
MailMessage mailMessage = new MailMessage(txtSender.Text,txtReceiver.Text);
/*Checking the condition that the cc is empty or not if not then
* include them
*/
if (txtCc.Text != null && txtCc.Text != string.Empty)
{
mailMessage.CC.Add(txtCc.Text);
}
/*Checking the condition that the Bcc is empty or not if not then
* include them
*/
if (txtBcc.Text != null && txtBcc.Text != string.Empty)
{
mailMessage.Bcc.Add(txtBcc.Text);
}
//Ading Subject to the Mail
mailMessage.Subject = txtSubject.Text;
//Adding the Mail Body
mailMessage.Body = txtBody.Text;

/* Set the properties of the MailMessage to the
values on the form as per the mail is HTML formatted or plain text */
if (rblMailFormat.SelectedItem.Text == "Text")
mailMessage.IsBodyHtml = false;
else
mailMessage.IsBodyHtml = true;

/* We use the following variables to keep track of
attachments and after we can delete them */
string attach1 = null;
string attach2 = null;
string attach3 = null;

/*strFileName has a attachment file name for
attachment process. */
string strFileName = null;

/* Bigining of Attachment1 process &
Check the first open file dialog for a attachment */
if (inpAttachment1.PostedFile != null)
{
/* Get a reference to PostedFile object */
HttpPostedFile attFile = inpAttachment1.PostedFile;
/* Get size of the file */
int attachFileLength = attFile.ContentLength;
/* Make sure the size of the file is > 0 */
if (attachFileLength > 0)
{
/* Get the file name */
strFileName = Path.GetFileName(inpAttachment1.PostedFile.FileName);
/* Save the file on the server */
inpAttachment1.PostedFile.SaveAs(Server.MapPath(strFileName));
/* Create the email attachment with the uploaded file */
Attachment attach = new Attachment(Server.MapPath(strFileName));
/* Attach the newly created email attachment */
mailMessage.Attachments.Add(attach);
/* Store the attach filename so we can delete it later */
attach1 = strFileName;
}
}
/* Attachment-2 Repeat previous step defiend above*/
if (inpAttachment2.PostedFile != null)
{
HttpPostedFile attFile = inpAttachment2.PostedFile;
int attachFileLength = attFile.ContentLength;
if (attachFileLength > 0)
{
strFileName = Path.GetFileName(inpAttachment2.PostedFile.FileName);
inpAttachment2.PostedFile.SaveAs(Server.MapPath(strFileName));
Attachment attach = new Attachment(Server.MapPath(strFileName));
mailMessage.Attachments.Add(attach);
attach2 = strFileName;
}
}
/* Attachment-3 Repeat previous steps step defiend above*/
if (inpAttachment3.PostedFile != null)
{
HttpPostedFile attFile = inpAttachment3.PostedFile;
int attachFileLength = attFile.ContentLength;
if (attachFileLength > 0)
{
strFileName = Path.GetFileName(inpAttachment3.PostedFile.FileName);
inpAttachment3.PostedFile.SaveAs(Server.MapPath(strFileName));
Attachment attach = new Attachment(Server.MapPath(strFileName));
mailMessage.Attachments.Add(attach);
attach3 = strFileName;
}
}

/* Set the SMTP server and send the email with attachment */
SmtpClient smtpClient = new SmtpClient();
// smtpClient.Host = emailServerInfo.MailServerIP;
//this will be the host in case of gamil and it varies from the service provider
smtpClient.Host = "smtp.gmail.com";
//smtpClient.Port = Convert.ToInt32(emailServerInfo.MailServerPortNumber);
//this will be the port in case of gamil for dotnet and it varies from the service provider
smtpClient.Port = 587;
smtpClient.UseDefaultCredentials = true;
//smtpClient.Credentials = new System.Net.NetworkCredential(emailServerInfo.MailServerUserName, emailServerInfo.MailServerPassword);
smtpClient.Credentials = new System.Net.NetworkCredential(txtUserName.Text, txtPassword.Text);
//this will be the true in case of gamil and it varies from the service provider
smtpClient.EnableSsl = true;
smtpClient.Send(mailMessage);



/* Delete the attachements if any */
try
{
if (attach1 != null)
File.Delete(Server.MapPath(attach1));
if (attach2 != null)
File.Delete(Server.MapPath(attach2));
if (attach3 != null)
File.Delete(Server.MapPath(attach3));
}
catch { }

/* clear the controls */
txtSender.Text = string.Empty;
txtReceiver.Text = string.Empty;
txtCc.Text = string.Empty;
txtBcc.Text = string.Empty;
txtSubject.Text = string.Empty;
txtBody.Text = string.Empty;
txtUserName.Text = string.Empty;

/* Dispaly a confirmation message to the user. */
lblMessage.Visible = true;
lblMessage.ForeColor = Color.Black;
lblMessage.Text = "Message sent.";
}
catch (Exception ex)
{
/* Print a message informing the
user about the exception that was risen */
lblMessage.Visible = true;
lblMessage.ForeColor = Color.Red;
lblMessage.Text = ex.ToString();
}

}
}

Google Maps using Asp.net



Untitled Page













File Upload Control to Store the Server Path In Database

protected void BtnSubmit_Click(object sender, EventArgs e)
{
string savepath="";
string savedir = @"~\photes\";
if (FileUpload1.HasFile)
{
string filename = Server.HtmlEncode(FileUpload1.
FileName);
string extention = System.IO.Path.GetExtension(filename);
if ((extention == ".jpg") || extention == ".gif")
{
savepath = savedir + filename;
FileUpload1.SaveAs(Server.MapPath(savepath));

con.Open();
SqlCommand cmd1 = new SqlCommand("insert into registration(eid,ename,desg,email,mobile,photo,username,password) values ('"+txteid.Text+"','"+txtEname.Text+"','"+DropDownList1.SelectedItem.Text + "','" + txtEmail.Text + "','" + txtmobile.Text + "','" + savepath + "','"+txtUsername .Text +"','"+txtpassword .Text +"')", con);
cmd1.ExecuteNonQuery();
con.Close();
Label7.Visible = true;
Label7.Text = "Uploaded";
Response.Write("");
}
else
{
Response.Write("images only");
}
}

get alternate id

public void eid()
//{

// con.Open();
// SqlCommand scmd = new SqlCommand("select 'MYGO00'+cast(max(substring(
eid,6,3)+1) as varchar(25)) from registration", con);
// SqlDataReader dr = scmd.ExecuteReader();
// dr.Read();
// if(dr.IsDBNull(0))
// {
// txteid.Text = "MYGO001";
// }
// else
// {
// txteid.Text = dr[0].ToString();
// }
// con.Close();
//}