Thursday, February 4, 2010

HOW TO USE USER CONTROL IN .ASPX PAGE

<%@ Register TagPrefix="UC" TagName ="RegUser" Src ="~/Include/UserControl/UserRegistration.ascx" %>









OR

<%@ Register TagPrefix ="SUBMENU" TagName ="PreferencesSubMenu" Src ="~/Include/UserControl/PreferencesSubMenu.ascx" %>




SEND MAIL CLASSSSSSSSSSSSSS

public bool sendMail(string fromEmail, string toEmail, string mailSubject, string mailBody, MailFormat mailFormat, string

fromName, string toName)
{

try
{
if (fromName == "")
{
fromName = ConfigurationSettings.AppSettings["siteName"];
}
if (toName == "")
{
toName = toEmail;
}

MailMessage mail = new MailMessage();


mail.From = fromEmail;
mail.To = toEmail;
mail.Subject = mailSubject;
mail.BodyFormat = mailFormat;
mailBody += "";
mail.Body = mailBody;

SmtpMail.SmtpServer = ConfigurationSettings.AppSettings["SmtpServer"];
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1);
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername",

ConfigurationSettings.AppSettings["sendemailusername"]);
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword",

ConfigurationSettings.AppSettings["sendemailpassword"]);
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", 2);
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver",

ConfigurationSettings.AppSettings["SmtpServer"]);
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout", 50);
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 25);

//SmtpMail.SmtpServer = ConfigurationSettings.AppSettings["SmtpServer"];
////SmtpMail.SmtpServer.Insert(0, ConfigurationSettings.AppSettings["SmtpServer"].ToString());
// mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1);
// mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername",

ConfigurationSettings.AppSettings["sendemailusername"]);
// mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword",

ConfigurationSettings.AppSettings["sendemailpassword"]);


SmtpMail.Send(mail);

return true;
}
catch (Exception ex)
{
return false;
}
}

CHANGE PASSWORD AND SEND MAIL FUNCTION

protected void btnsave_Click(object sender, EventArgs e)
{
try
{
DataTable dt = new DataTable();
int userid = Convert.ToInt16(Session["UserID"].ToString());
BLL_Users objuser = new BLL_Users();
objuser.UserID = userid;
objuser.Password = txtoldpwd.Text.Trim();
objuser.NewPwd = txtnewpwd.Text.Trim();

dt = objuser.ValidateUserByUserID();
if (dt.Rows.Count > 0)
{

if ((Convert.ToInt16(dt.Rows[0]["IsChange"])) > 0)
{
lblsuccess.Font.Bold = true;
lblsuccess.ForeColor = Color.Red;
lblsuccess.Text = "Please enter correct old password.";
txtoldpwd.Text = "";
}
else
{
lblsuccess.Font.Bold = true;
lblsuccess.ForeColor = Color.Green;
lblsuccess.Text = "Password has been changed successfully.";

DataTable dt1 = new DataTable();
BLL_Users objView = new BLL_Users();
objView.UserID = Convert.ToInt32(Session["UserID"].ToString());
dt1 = objView.GetUserInformation();
if (dt1.Rows.Count > 0)
{
String bodyText = "";
bodyText = "";
bodyText = bodyText + "Dear" + " " + dt1.Rows[0]["FirstName"].ToString() + " " +

dt1.Rows[0]["LastName"].ToString() + ",";
bodyText = bodyText + "
";
bodyText = bodyText + "
";
bodyText = bodyText + "Your password has been changed to :";
bodyText = bodyText + " " + txtnewpwd.Text + "";
bodyText = bodyText + "
";
bodyText = bodyText + "
";
bodyText = bodyText + "If you have any queries or want to send nominations, please mail the same to

aatish09@gmail.com " + "
";
bodyText = bodyText + "
";
bodyText = bodyText + "All the best!";
bodyText = bodyText + "
";
bodyText = bodyText + "
";
bodyText = bodyText + "Regards,";
bodyText = bodyText + "
";
bodyText = bodyText + "Learning Organization ";
bodyText = bodyText + "
";
bodyText = bodyText + "Three Sixty Degree Data Research Pvt.Ltd. Pune";
bodyText = bodyText + "
";
//bodyText = String.Empty;
bool check;

BLL_SendMail objmail = new BLL_SendMail();
check = objmail.sendMail(ConfigurationSettings.AppSettings["adminEmail"],

dt1.Rows[0]["EmailID"].ToString(), "Change Password", bodyText, MailFormat.Html, "", "");
if (check == true)
{
lblsendmail.Visible = true;
lblsendmail.Text = "Password has been Mailed to you, Please check mail.";

}
else
{
lblsendmail.Visible = true;
lblsendmail.Font.Bold = true;
lblsendmail.ForeColor = Color.Red;
lblsendmail.Text = "Problem in sending Email.";
}
}
}
}
}
catch(Exception ex)
{
String errorMessage;
errorMessage = "Error: ";
errorMessage = String.Concat(errorMessage, ex.Message);
errorMessage = String.Concat(errorMessage, " Line: ");
errorMessage = String.Concat(errorMessage, ex.Source);
LCE.Configuration.ErrorLog("Preferences/ChangePassword.cs", errorMessage);
}
}

HOW TO USE SELECT ALL CHECK BOX FUNCTION



< type="text/javascript" language="javascript">
function CheckData()
{
GetSelected()
if(document.getElementById('<%=selected_qn_ids1.ClientID %>').value == "")
{
alert("No user is selected for enrollment.");
return false;
}
}
function GetSelected()
{
var grdInput;
var strSelectedIds;
var chkLength;
var intLoopCount;
var intarr = 0;
var arr = new Array(100);

grdInput = document.getElementById('<%= dgStudentEnroll.ClientID %>').getElementsByTagName("input");
chkLength = grdInput.length;

for(intLoopCount=0;intLoopCount<)
{
if(grdInput[intLoopCount].type == "checkbox")
{
if(grdInput[intLoopCount].checked == true)
{
if(document.getElementById("<%= selected_qn_ids1.ClientID %>").value == "")
{
var Qid= grdInput[intLoopCount].value;
document.getElementById("<%= selected_qn_ids1.ClientID %>").value = Qid;


}
else
{
var Qid= grdInput[intLoopCount].value;
document.getElementById("<%= selected_qn_ids1.ClientID %>").value =

document.getElementById("<%= selected_qn_ids1.ClientID %>").value + "," + Qid;
var str=document.getElementById("<%= selected_qn_ids1.ClientID %>").value;
}
intarr = intarr + 1;
}
else
{

}
}
}
}

HOW TO USE DATAGRID WITH DATAFILED FROM DATABASE


="#999999" ForeColor="#FFFFFF" HeaderStyle-HorizontalAlign ="Center" >


="#000000" ItemStyle-Width ="40px" ItemStyle-HorizontalAlign="Center" >


="#000000" ItemStyle-Width ="200px" ItemStyle-HorizontalAlign="Center" >


ItemStyle-ForeColor ="#000000" ItemStyle-Width ="300px" ItemStyle-HorizontalAlign="Center" >


ItemStyle-ForeColor ="#000000" ItemStyle-Width ="150px" ItemStyle-HorizontalAlign="Center" >


ItemStyle-ForeColor ="#000000" ItemStyle-Width ="100px" ItemStyle-HorizontalAlign="Center" >


ItemStyle-ForeColor ="#000000" ItemStyle-Width ="100px" ItemStyle-HorizontalAlign="Center" >


="#000000" ItemStyle-Width ="100px" ItemStyle-HorizontalAlign="Center" >


="#000000" ItemStyle-Width ="100px" ItemStyle-HorizontalAlign="Center" >


ItemStyle-ForeColor ="#000000" ItemStyle-Width ="100px" ItemStyle-HorizontalAlign="Center" >


ItemStyle-ForeColor ="#000000" ItemStyle-Width ="150px" ItemStyle-HorizontalAlign="Center" >


ItemStyle-ForeColor ="#000000" ItemStyle-Width ="100px" ItemStyle-HorizontalAlign="Center" >


="#000000" ItemStyle-Width ="150px" ItemStyle-HorizontalAlign="Center" >


HOW TO RETURN RECORDS FROM EXCEL SHEET aND HOW TO USE FINALLY BLOCK

DataSet ds = GetExcel(path + "\\" + strFile);
string chk_reg = "";
int rowCount = ds.Tables[0].Rows.Count;
if (rowCount > 0)
{
for (int z = 0; z < 1; z++)
{
}

public DataSet GetExcel(string fileName)
{
Application oXL;
Workbook oWB;
Worksheet oSheet;
Range oRng;
try
{
// creat a Application object
oXL = new ApplicationClass();
// get WorkBook object
oWB = oXL.Workbooks.Open(fileName, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value);

// get WorkSheet object
oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oWB.Sheets[1];
System.Data.DataTable dt = new System.Data.DataTable("dtExcel");
DataSet ds = new DataSet();
ds.Tables.Add(dt);
DataRow dr;

StringBuilder sb = new StringBuilder();
int jValue = oSheet.UsedRange.Cells.Columns.Count;
int iValue = oSheet.UsedRange.Cells.Rows.Count;
// get data columns
for (int j = 1; j <= jValue; j++)
{
dt.Columns.Add("column" + j, System.Type.GetType("System.String"));
}

// get data in cell
for (int i = 1; i <= iValue; i++)
{
dr = ds.Tables["dtExcel"].NewRow();
for (int j = 1; j <= jValue; j++)
{
oRng = (Microsoft.Office.Interop.Excel.Range)oSheet.Cells[i, j];
string strValue = oRng.Text.ToString();
dr["column" + j] = strValue;
}
ds.Tables["dtExcel"].Rows.Add(dr);
}
return ds;
}
catch (Exception ex)
{
return null;
}
finally
{
Dispose();
System.Diagnostics.Process[] prs = System.Diagnostics.Process.GetProcesses();
foreach (System.Diagnostics.Process proces in prs)
{
if(proces.ProcessName.ToUpper()=="EXCEL")
{
proces.Refresh();
if (!proces.HasExited)
proces.Kill();
}
}
}
}

HOW TO DOWNLOAD EXCEL SHEET

protected void linkBtnDownLoad_Click(object sender, EventArgs e)
{
DownLoadExcel();
}

private void DownLoadExcel()//function for download excel sheet format.
{
try
{
//b4 download file make sure that file access is not denied ,so set file attribute IsReadonly to false.
string download_filename = Server.MapPath("../Reports") + "\\StudentRegistration.xls";
System.IO.FileInfo fileInfo = new System.IO.FileInfo(download_filename);
fileInfo.IsReadOnly = false;
FileStream fs;
fs = File.Open(Server.MapPath("../Reports") + "\\StudentRegistration.xls", FileMode.Open);
Byte[] bytBytes = new Byte[fs.Length];
fs.Read(bytBytes, 0, Convert.ToInt32(fs.Length));
fs.Close();
Response.AddHeader("Content-disposition", "attachment; filename=StudentRegistration.xls");
Response.ContentType = "application/octet-stream";
Response.BinaryWrite(bytBytes);
Response.End();
}
catch (Exception ex)
{
String errorMessage;
errorMessage = "Error: ";
errorMessage = String.Concat(errorMessage, ex.Message);
errorMessage = String.Concat(errorMessage, " Line: ");
errorMessage = String.Concat(errorMessage, ex.Source);
LCE.Configuration.ErrorLog("ManageUsers/ImportFromExcel.cs", errorMessage);
}
}