Sie sind auf Seite 1von 5

using System;

using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
public partial class books_booklist : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
List<int> list = new List<int>();
Session["gridSave"] = list;
}
}
protected void btnCalculatePrice_Click(object sender, EventArgs e)
{
string source = "Data Source=SQL5.WEBHOSTADDRESS.NET;Initial Catalog=seq
uence;User ID=HIME;Password=HELLOWORLD;";
String selectCmd = "SELECT BOOK_CODE, NAME, PRICE FROM [BOOKS] WHERE BOO
K_CODE IN (";
List<int> list = (List<int>)Session["gridSave"];
string Enquiry = "";
if (list != null)
{
///
/// Update list for selected/deselected books on the current page
///
foreach (GridViewRow row in BooksGridView.Rows)
{
// Is a checkbox on this box checked?
int id = (int)BooksGridView.DataKeys[row.RowIndex].Value;
bool chk = ((CheckBox)row.FindControl("BookSelector")).Checked;
// A checkbox is found checked
if (chk)
{
// Is this not listed in the list
if (list.Contains(id) == false)
{
// Add it in the list
list.Add(id);
}
}
// A checkbox is not found checked
else
{
// Is this listed in the list
if (list.Contains(id))
{
// Remove it from the list
list.Remove(id);
}
}
}
///
/// For total list
///
if (list.Count > 0)
{
string bookCodesToFind = "";
char[] aComma = { ',' };
foreach (int i in list)
{
bookCodesToFind = bookCodesToFind + "'" + i + "'" + ",";
}
string trimmed = bookCodesToFind.TrimEnd(aComma);
selectCmd = selectCmd + trimmed + ")";
SqlDataReader reader;
using (SqlConnection conn = new SqlConnection(source))
{
conn.Open();
using (SqlCommand command = new SqlCommand(selectCmd, conn))
{
try
{
reader = command.ExecuteReader();
while (reader.Read())
{
Enquiry = Enquiry + "Code: " + reader[0] + " "
+ "Name: " + reader[1] + " " + "Price: " + reader[2] + " " + "Qty: " + "\n";
}
}
catch (Exception ex)
{
txtBoxEMail.Text = "Exception in ExecuteReader: " +
ex.Message;
conn.Close();
return;
}
conn.Close();
}
}
}
}
txtBoxEMail.Text = Enquiry;
}
protected void btnSendEmail_Click(object sender, EventArgs e)
{
if(txtBoxEMail.Text.Length < 20)
{
lblStatus.ForeColor = System.Drawing.Color.Red;
lblStatus.Font.Bold = true;
lblStatus.Text = "First select books and create enquiry to send as E
mail";
return;
}
if (txtBoxName.Text.Length < 1)
{
lblStatus.ForeColor = System.Drawing.Color.Red;
lblStatus.Font.Bold = true;
lblStatus.Text = "Name is required";
return;
}
if (txtBoxAddress.Text.Length < 1)
{
lblStatus.ForeColor = System.Drawing.Color.Red;
lblStatus.Font.Bold = true;
lblStatus.Text = "Address is required";
return;
}
if (txtBoxEM.Text.Length < 1)
{
lblStatus.ForeColor = System.Drawing.Color.Red;
lblStatus.Font.Bold = true;
lblStatus.Text = "Your Email Address is required";
return;
}
StringBuilder sb = new StringBuilder("Enquiry from:");
sb.AppendFormat("{0} <br />", " ");
sb.AppendFormat("{0}", "Name: ");
sb.AppendFormat("{0} <br />", txtBoxName.Text);
sb.AppendFormat("{0}", "Address: ");
sb.AppendFormat("{0} <br />", txtBoxAddress.Text);
sb.AppendFormat("{0}", "Email ID: ");
sb.AppendFormat("{0} <br />", txtBoxEM.Text);
sb.AppendFormat("{0} <br />", "Enquiry: ");
sb.AppendFormat("{0} <br />", txtBoxEMail.Text);
try
{
MailAddress frmAddress = new MailAddress("messageform@webhostclient.
in", "Message from Enquiry");
MailAddress toAddress = new MailAddress("contact@webhostclient.in",
"Contact Sadhan");
MailMessage mailMsg = new MailMessage(frmAddress, toAddress);
mailMsg.Subject = "Enquiry";
mailMsg.IsBodyHtml = true;
mailMsg.Body = sb.ToString();
SmtpClient smtpClient = new SmtpClient("mail.webhostclient.in");
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential("messageform@webhostc
lient.in", "wonder@thunder");
smtpClient.Port = 25;
smtpClient.Send(mailMsg);
Response.Redirect("~\\Thanks.aspx");
}
catch (Exception ex)
{
lblStatus.ForeColor = System.Drawing.Color.Red;
lblStatus.Font.Bold = true;
lblStatus.Text = "Send Email Failed: " + ex.Message;
}
}
protected void BooksGridView_OnPageIndexChanging(object sender, GridViewPage
EventArgs e)
{
saveValues();
BooksGridView.PageIndex = e.NewPageIndex;
}
protected void BooksGridView_OnDataBound(object sender, EventArgs e)
{
loadValues();
}
private void saveValues()
{
List<int> list = (List<int>)Session["gridSave"];
if (list != null)
{
foreach (GridViewRow row in BooksGridView.Rows)
{
int id = (int)BooksGridView.DataKeys[row.RowIndex].Value;
bool chk = ((CheckBox)row.FindControl("BookSelector")).Checked;
if (chk)
{
//see if we have a id added already
if (list.Contains(id) == false)
{
list.Add(id);
}
}
else
{
if (list.Contains(id))
{
list.Remove(id);
}
}
}
Session["gridSave"] = list;
}
}
private void loadValues()
{
List<int> list = (List<int>)Session["gridSave"];
if (list != null)
{
foreach (GridViewRow row in BooksGridView.Rows)
{
int id = (int)BooksGridView.DataKeys[row.RowIndex].Value;
if (list.Contains(id))
{
CheckBox chkBook = (CheckBox)row.FindControl("BookSelector")
;
chkBook.Checked = true;
}
}
}
}
protected void btnCalculatePrice_Click_ORIGINAL(object sender, EventArgs e)
{
int totalPrice = 0;
// Iterate through the BooksGridView.Rows property
foreach (GridViewRow row in BooksGridView.Rows)
{
// Access the CheckBox
CheckBox cb = (CheckBox)row.FindControl("BookSelector");
if (cb != null && cb.Checked)
{
// First, get the Price for the selected book
int bookPrice =
//Convert.ToInt32(BooksGridView.DataKeys[row.RowIndex].Value
);
Convert.ToInt32(row.Cells[4].Text);
totalPrice = totalPrice + bookPrice;
}
}
if (totalPrice > 0)
{
lblTotal.Text = "Rs. " + totalPrice + ".00";
}
else
{
lblTotal.Text = "Select books then click button";
}
}
}

Das könnte Ihnen auch gefallen