Archive

Archive for the ‘ASP.Net’ Category

Get and Bind all Countries to DropdownList Using Globalization

July 17, 2017 Leave a comment

Hi all,

Try this example to Get and Bind all Countries to Dropdownlist Using Globalization

Select Country:

<asp:DropDownList ID="ddlcountry" runat="server"> </asp:DropDownList>

In Code behind :


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

public void GetAllCountry()
{
try
{
List<string> listcountry = new List<string>();
CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
foreach (CultureInfo cinfo in cultures)
{
RegionInfo Rinfo = new RegionInfo(cinfo.LCID);
if (!listcountry.Contains(Rinfo.EnglishName))
{
listcountry.Add(Rinfo.EnglishName);
}
}
listcountry.Sort();
ddlcountry.DataSource = listcountry;
ddlcountry.DataBind();
ddlcountry.Items.Insert(0, new ListItem("--Select Country--", "0"));
}
catch (Exception ex)
{
}
}

Hope this helps

Good Luck

Categories: ASP.Net

Send Selected Data from Gridview into Email in ASP.Net

June 29, 2017 Leave a comment

Dears,

Try this example to Send Selected Data from Gridview into Email


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gv_Category" runat="server" AutoGenerateColumns="False" CellPadding="4"
DataKeyNames="CategoryID" DataSourceID="sds_Category" EnableModelValidation="True"
ForeColor="#333333">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Category ID" InsertVisible="False" SortExpression="CategoryID">
<ItemTemplate>
<asp:Label ID="lblCategoryID" runat="server" Text='<%# Bind("CategoryID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Category Name" SortExpression="CategoryName">
<ItemTemplate>
<asp:Label ID="lblCategoryName" runat="server" Text='<%# Bind("CategoryName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description" SortExpression="Description">
<ItemTemplate>
<asp:Label ID="lblDescription" runat="server" Text='<%# Bind("Description") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
</asp:GridView>
<asp:SqlDataSource ID="sds_Category" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [CategoryID], [CategoryName], [Description] FROM [Categories]">
</asp:SqlDataSource>
</div>
<asp:Button ID="btn_SendEmail" runat="server" onclick="btn_SendEmail_Click"
Text="Send Email" />
</form>
</body>
</html>

In Code behind :


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Net;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.IO;
using System.Text;
using System.Net.Mime;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btn_SendEmail_Click(object sender, EventArgs e)
{
string sAttchent = string.Empty;
string attachmentPath = string.Empty;

MailMessage mm = new MailMessage();
mm.From = new MailAddress("From@gmail.com");
mm.To.Add("To@gmail.com");

string sContent = string.Empty;
sContent = "
<table border='1'>
<tr>
<td>Category Name</td>
<td>Category Description</td>
<tr>";
foreach (GridViewRow row in gv_Category.Rows)
{
CheckBox chkSelect = (CheckBox)row.FindControl("chkSelect");
if (chkSelect.Checked)
{
int rowIndex = row.RowIndex;
string CategoryID = Convert.ToString(gv_Category.DataKeys[rowIndex].Values["CategoryID"]);
Label lblCategoryName = (Label)row.FindControl("lblCategoryName");
Label lblDescription = (Label)row.FindControl("lblDescription");
sContent += "
<tr>
<td>" + lblCategoryName.Text + "</td>
<td>" + lblDescription.Text + "</td>
<tr>";
string sCategoryName = string.Empty;
string sCategoryImage = string.Empty;
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "SELECT * FROM Categories WHERE CategoryID = " + CategoryID;
cmd.Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
MemoryStream memoryStream = new MemoryStream();
byte[] contentAsBytes = (byte[])dt.Rows[i]["Picture"];
memoryStream.Write(contentAsBytes, 78, contentAsBytes.Length - 78);
// Set the position to the beginning of the stream.
memoryStream.Seek(0, SeekOrigin.Begin);
// Create attachment
ContentType contentType = new ContentType();
contentType.MediaType = "image/png";
contentType.Name = dt.Rows[i]["CategoryName"].ToString();
Attachment attachment = new Attachment(memoryStream, contentType);
// Add the attachment
mm.Attachments.Add(attachment);
}
}
}
}

mm.Subject = "Category Details ";
mm.IsBodyHtml = true;
mm.Body = "this content is in the body
" + sContent + "</table>
";
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
NetworkCredential credential = new NetworkCredential();
credential.UserName = "sender@gmail.com";
credential.Password = "xxxxxx";
smtp.UseDefaultCredentials = true;
smtp.Credentials = credential;
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.Send(mm);
}
}

In Web.Config add Connection String

 <connectionStrings>
<add name="NorthwindConnectionString" connectionString="Data Source=YASSER-PC;Initial Catalog=Northwind;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>

Hope this helps

Good Luck.

Categories: ASP.Net

JQuery calendar to display event data from Database

Hello,

Try this example to use JQuery calendar to display event data from Database


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
	<link href="http://code.jquery.com/ui/1.9.2/themes/smoothness/jquery-ui.css" rel="stylesheet" />
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
 <script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<style>
* {
margin: 0 auto;
padding: 0;
}

body {
background-color: #F2F2F2;
}

.container {
margin: 0 auto;
width: 920px;
padding: 50px 20px;
background-color: #fff;
}

h3 {
text-align: center;
}

#calendar {
margin-top: 40px;
}

.event a {
background-color: #42B373 !important;
background-image: none !important;
color: #ffffff !important;
}
</style>

<script type="text/javascript">
 var eventDates = {};
 $(function () {
 $.ajax({
 type: "POST",
 url: "Default2.aspx/GetEvents",
 data: '{}',
 contentType: "application/json; charset=utf-8",
 dataType: "json",
 success: OnSuccess,
 failure: function (response) {
 alert(response.d);
 },
 error: function (response) {
 alert(response.d);
 }
 });
 });

function OnSuccess(response) {
 $(response.d).each(function (i, e) {
 eventDates[convertDate(e.EventDate)] = convertDate(e.EventDate);
 });

$('#calendar').datepicker({
 beforeShowDay: function (date) {
 var highlight = eventDates[convertDate(date)];
 // alert(highlight);
 if (highlight) {
 return [true, "event", highlight];
 } else {
 return [true, '', ''];
 }
 }
 });
 }

function convertDate(inputFormat) {
 function pad(s) { return (s < 10) ? '0' + s : s; }
 var d = new Date(inputFormat);
 return [pad(d.getDate()), pad(d.getMonth() + 1), d.getFullYear()].join('/');
 }

</script>
</head>
<body>
<form id="form1" runat="server">
<div class="container">
<h3>Highlight Particular Dates in JQuery UI Datepicker</h3>
<div id="calendar"></div>
</div>
</form>
</body>
</html>

In Code behind :


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;

public partial class Default2 : System.Web.UI.Page
{
[WebMethod]
public static List<Event> GetEvents()
{
List<Event> events = new List<Event>();
events.Add(new Event()
{
EventID = 1,
EventName = "EventName 1",
EventDate = DateTime.Now.ToString("MM-dd-yyyy"),
EventDetail = string.Empty
});
events.Add(new Event()
{
EventID = 2,
EventName = "EventName 2",
EventDate = DateTime.Now.AddDays(4).ToString("MM-dd-yyyy"),
EventDetail = string.Empty
});
events.Add(new Event()
{
EventID = 3,
EventName = "EventName 3",
EventDate = DateTime.Now.AddDays(10).ToString("MM-dd-yyyy"),
EventDetail = string.Empty
});
events.Add(new Event()
{
EventID = 4,
EventName = "EventName 4",
EventDate = DateTime.Now.AddDays(22).ToString("MM-dd-yyyy"),
EventDetail = string.Empty
});
return events;
}

protected void Page_Load(object sender, EventArgs e)
{

}
}

Hope this helps

Good Luck.

Categories: ASP.Net, Jquery

Display Sum (Total) of Checked GridView Rows on CheckBox check using jQuery in ASP.Net

March 27, 2017 Leave a comment

Dears,

Try this example to Display Sum (Total) of Checked GridView Rows on CheckBox check using jQuery in ASP.Net

 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
 <script type="text/javascript">
 $(function () {
 $('[id*=chk]').on('change', function () {
 var value = 0;
 $('[id*=chk]:checked').each(function () {
 var row = $(this).closest('tr');
 value = value + parseInt(row.find('[id*=lblValue]').html());
 });
 $('[id*=lblTotal]').html(value);
 });
 });
 </script>
<asp:GridView ID="gvData" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="chk" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Values">
<ItemTemplate>
<asp:Label ID="lblValue" runat="server" Text='<%# Eval("Value") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Label ID="lblTotal" runat="server" />

In Code behind :

protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[1] { new DataColumn("Value", typeof(int)) });
dt.Rows.Add(10);
dt.Rows.Add(20);
dt.Rows.Add(30);
dt.Rows.Add(40);
gvData.DataSource = dt;
gvData.DataBind();
}
}

Hope this helps

Good Luck.

Categories: ASP.Net, Jquery

Convert Text in TextBox to Upper Using JavaScript

February 20, 2017 Leave a comment

Hi all,

Try this example to Convert Text in TextBox to Upper using Javascript

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
 function Capital(txt) {
 txt.value = txt.value.replace(/^\s+/, '').toUpperCase();
 }
 </script>
</head>
<body>
Enter Name:
<input type="text" value=" " onkeyup="Capital(this)" />
</body>
</html>

Hope this helps

Good Luck.

Categories: ASP.Net

Validate IBAN Using C#

January 5, 2017 Leave a comment

Hi all,

Try this example to Validate IBAN Using C#

public static bool ValidateBankAccount(string bankAccount)
{
bankAccount = bankAccount.ToUpper(); //IN ORDER TO COPE WITH THE REGEX BELOW
if (String.IsNullOrEmpty(bankAccount))
return false;
else if (System.Text.RegularExpressions.Regex.IsMatch(bankAccount, "^[A-Z0-9]"))
{
bankAccount = bankAccount.Replace(" ", String.Empty);
string bank =
bankAccount.Substring(4, bankAccount.Length - 4) + bankAccount.Substring(0, 4);
int asciiShift = 55;
StringBuilder sb = new StringBuilder();
foreach (char c in bank)
{
int v;
if (Char.IsLetter(c)) v = c - asciiShift;
else v = int.Parse(c.ToString());
sb.Append(v);
}
string checkSumString = sb.ToString();
int checksum = int.Parse(checkSumString.Substring(0, 1));
for (int i = 1; i < checkSumString.Length; i++)
{
int v = int.Parse(checkSumString.Substring(i, 1));
checksum *= 10;
checksum += v;
checksum %= 97;
}
return checksum == 1;
}
else
return false;
} 

Hope this helps

Good Luck

Categories: ASP.Net

Get Current Page Name

December 6, 2016 Leave a comment

Hi all,

Try this example to Get Current Page Name

public static string GetCurrentPageName()
{
string sPath = HttpContext.Current.Request.Url.AbsolutePath;
System.IO.FileInfo oInfo = new System.IO.FileInfo(sPath);
string sRet = oInfo.Name;
return sRet;
}

Hope this helps

Good Luck.

Categories: ASP.Net