Tuesday, December 18, 2007

reading excel file in asp.net

void readexcel(String filename)
{
string connection = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source= " + Server.MapPath("test\\" + filename) + ";" + "Extended Properties=Excel 8.0;";
oledbcon = new OleDbConnection(connection);
oledbcon.Open();
try
{
oledbcom = new OleDbCommand("select * from [qryDistributionExportXLSPDNewPl$]", oledbcon);
oledbreader = oledbcom.ExecuteReader();

while (oledbreader.Read())
if (oledbreader.GetName(18).ToString() == "BaseProduct" && oledbreader.GetName(0).ToString() == "ApprovalCode" && oledbreader.GetName(1).ToString() == "ShortDescription" && oledbreader.GetName(5).ToString() == "ValidityFirstOrder" && oledbreader.GetName(6).ToString() == "ValidityLastOrder" && oledbreader.GetName(11).ToString() == "EclipseId" && oledbreader.GetName(17).ToString() == "ProductId" && oledbreader.GetName(24).ToString() == "DealerCost" && oledbreader.GetName(27).ToString() == "OrderMinimumQuantity")
{
sdescription = oledbreader["BaseProduct"].ToString();
if (sdescription == "Y")
{
ApprovalCode = oledbreader["ApprovalCode"].ToString();
description = oledbreader["ShortDescription"].ToString();
firstorder = oledbreader["ValidityFirstOrder"].ToString();
lastorder = oledbreader["ValidityLastOrder"].ToString();
eclipid = oledbreader["EclipseId"].ToString();
Pdutid = oledbreader["ProductId"].ToString();
Dcost =Convert.ToDouble(oledbreader["DealerCost"].ToString());
//Dcost = Convert.ToDecimal(oledbreader["DealerCost"].ToString());
MinQty = oledbreader["OrderMinimumQuantity"].ToString();
appdate = DateTime.Now.ToString();
string selectMarkuppercentage = "Select * from Markuppercentage where ItemCode='" + Pdutid + "' ";
// Response.Write(selectMarkuppercentage);
// Response.End();
if (charp_class.aleradyexists(selectMarkuppercentage, "MyConn"))
{
mpercent = Convert.ToDouble(charp_class.DbSelSingleFeild(selectMarkuppercentage, "Markuppercentage", "MyConn"));
}
else
{
mpercent = 0;
}
string selectqty = "select * from spcmaster where spcnumber=" + description;
//int qty = Int16.Parse(charp_class.DbSelSingleFeild(selectqty, "quantity", "MyConn"));

Double total=Dcost*mpercent/100;
//Response.Write("percentsage" + total);
total = total + Dcost;
string selectSPCMaster = "update SPCMaster set EclipseId='" + eclipid + "',ApprovedDate='" + appdate + "',ApprovalCode='" + ApprovalCode + "',ShortDesc='" + description + "',ValidityFirstOrder='" + firstorder + "',ValidityLastOrder='" + lastorder + "',ApprovedPrice='" + total + "',SPCStatus='Approved', DealerCost='" + Dcost + "' where SPCNumber='" + description + "' and SPCStatus='Pending'";
charp_class.DbManip(selectSPCMaster, "MyConn", false);
}
errormessage.Text = "Uploaded Successfully";
//insertfunction(sdescription);
}
else
{
//message.Text = "";
errormessage.Text = "Field Name Is Not In Correct Format ";
//Response.Write("Field Name Is Not In Correct Format");
}
oledbreader.Close();
oledbcon.Close();
oledbcon.Dispose();
}


catch (OleDbException)
{
//message.Text = "";
errormessage.Text = " Pls Choose Correct file format";
//Response.Write("Enter Correct format");
}
}

mail sent-form as mail message



























style='BORDER-RIGHT: #d2f1ff 1px solid; BORDER-TOP: #d2f1ff; FONT-WEIGHT: normal; FONT-SIZE: 11px; BORDER-LEFT: #d2f1ff 1px solid; COLOR: #336699; BORDER-BOTTOM: #d2f1ff 1px solid; FONT-STYLE: normal; FONT-FAMILY: Arial, Helvetica, sans-serif; BACKGROUND-COLOR: #f9fdff; FONT-VARIANT: normal'>








style='BORDER-RIGHT: #003148 1px; BORDER-TOP: #003148 1px ridge; FONT-WEIGHT: bold; FONT-SIZE: 14px; BORDER-LEFT: #003148 1px; COLOR: #ffffff; BORDER-BOTTOM: #003148 1px ridge; FONT-STYLE: normal; FONT-FAMILY: Arial, Helvetica, sans-serif; BACKGROUND-COLOR: #006699; TEXT-DECORATION: none'
align=middle width='4%'>

style='BORDER-RIGHT: #003148 1px; BORDER-TOP: #003148 1px ridge; FONT-WEIGHT: bold; FONT-SIZE: 14px; BORDER-LEFT: #003148 1px; COLOR: #ffffff; BORDER-BOTTOM: #003148 1px ridge; FONT-STYLE: normal; FONT-FAMILY: Arial, Helvetica, sans-serif; BACKGROUND-COLOR: #006699; TEXT-DECORATION: none'
align=middle width='96%'>
Auction details

style='BORDER-RIGHT: #003148 1px; BORDER-TOP: #003148 1px ridge; FONT-WEIGHT: bold; FONT-SIZE: 14px; BORDER-LEFT: #003148 1px; COLOR: #ffffff; BORDER-BOTTOM: #003148 1px ridge; FONT-STYLE: normal; FONT-FAMILY: Arial, Helvetica, sans-serif; BACKGROUND-COLOR: #006699; TEXT-DECORATION: none'
cellSpacing=0 cellPadding=0 width='96%' align=center border=0>

style='FONT-WEIGHT: normal; FONT-SIZE: 11px; COLOR: #ffffff; FONT-STYLE: normal; FONT-FAMILY: Arial, Helvetica, sans-serif; FONT-VARIANT: normal'
align=center>Item Name

style='FONT-WEIGHT: normal; FONT-SIZE: 11px; COLOR: #ffffff; FONT-STYLE: normal; FONT-FAMILY: Arial, Helvetica, sans-serif; FONT-VARIANT: normal'
align=center>Item Code

style='FONT-WEIGHT: normal; FONT-SIZE: 11px; COLOR: #ffffff; FONT-STYLE: normal; FONT-FAMILY: Arial, Helvetica, sans-serif; FONT-VARIANT: normal'
align=center>Item Price ($)

style='FONT-WEIGHT: normal; FONT-SIZE: 11px; COLOR: #ffffff; FONT-STYLE: normal; FONT-FAMILY: Arial, Helvetica, sans-serif; FONT-VARIANT: normal'
align=center>Total Price ($)

style='FONT-WEIGHT: normal; FONT-SIZE: 11px; COLOR: #ffffff; FONT-STYLE: normal; FONT-FAMILY: Arial, Helvetica, sans-serif; FONT-VARIANT: normal'
align=center>Sale Date

varitem

varitemcode

varprice

varprice

vardate
>We were unable to complete the transaction for your Auctioned Item
on Wine Societies due to your invalid credit card







using System.IO;
using System.Text;
using System.Net.Mail;
public partial class Test : System.Web.UI.Page
{
StringBuilder sb;
StringWriter sw;
Html32TextWriter htmltxtw;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
sb = new StringBuilder();
sw = new StringWriter(sb);
htmltxtw = new Html32TextWriter(sw);
holder.RenderControl(htmltxtw);
MailMessage mess = new MailMessage("karthick@webindia.com", "karthick@webindia.com", "Email Test", sb.ToString());
mess.IsBodyHtml = true;
SmtpClient obj = new SmtpClient("192.168.0.101");
obj.Send(mess);
}
}

sending datagrid as mail message

using System.Text;
using System.IO;
using System.Net.Mail;


public void bindgrid()
{
sqlcom = new SqlCommand("select * from viewcart", sqlcon);
ds = new DataSet();
ds.Load(sqlcom.ExecuteReader(), LoadOption.OverwriteChanges, "temp");
datagrid.DataSource = ds;
datagrid.DataBind();
sb = new StringBuilder();
sw = new StringWriter(sb);
htmlwr = new Html32TextWriter(sw);
datagrid.RenderControl(htmlwr);
ltlHTMLOutput.Text = sb.ToString();
MailMessage mess = new MailMessage("karthick@webindia.com", "karthick@webindia.com", "Email Test", sb.ToString());
mess.IsBodyHtml = true;
SmtpClient obj = new SmtpClient("192.168.0.101");
obj.Send(mess);

}

mail with attachment

using System.Net.Mail;
public partial class EmailSend : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void sendbut_Click(object sender, EventArgs e)
{
try
{
MailMessage mess = new MailMessage("karthick@webindia.com", "karthick@webindia.com", "Test for Sending Mail with Attachment", "Hi This is for Testing.....!");
mess.Attachments.Add(new Attachment(upload.PostedFile.InputStream, upload.FileName));
mess.Priority = MailPriority.High;
SmtpClient Mailsender = new SmtpClient("192.168.0.101");
// Receipt for Email
mess.Headers.Add("Disposition-Notification-To", "karthick@webindia.com");
Mailsender.Send(mess);
Response.Write("Email Sent Successfully....!");
}
catch (Exception exp)
{

Response.Write(exp.Message.ToString());
}
}
}

sending mails in asp.net-table format

test.htm
----------












username
varusername
Passwordvarpassword
Thank you for registering with us.
with regards,
Webindia India



**********************************


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Net.Mail;
public partial class EmailReader : System.Web.UI.Page
{
StreamReader reader;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void button1_Click(object sender, EventArgs e)
{
reader = File.OpenText(Server.MapPath("test.htm"));
string table = reader.ReadToEnd();
reader.Close();
reader.Dispose();
table = table.Replace("varusername", "karthik d.s");
table = table.Replace("varpassword", "karthik d.s");
//Response.Write(table);
MailMessage mess = new MailMessage("karthick@webindia.com", "karthick@webindia.com");
mess.Body = table;
mess.Priority = MailPriority.High;
SmtpClient Mailsender = new SmtpClient("192.168.0.101");
// Receipt for Email
Mailsender.Send(mess);
Response.Write("Email Sent Successfully....!");
}
}

Message box in asp.net

using System.Diagnostics;
using System.Runtime.InteropServices;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[DllImport("User32.dll")]
public static extern int MessageBox(int h, string m, string c, int type);
protected void MBox_Click(object sender, EventArgs e)
{
int ok = MessageBox(0, "API Message Box", "API Demo", 1);
MessageBox(0, ok.ToString(), "API Demo", 0);
}
}

Tuesday, December 11, 2007

class file for Database/datagrid to Excel sheet

Imports System.Data.OleDb
Imports System.Web.HttpResponse
Imports System.Data

Public Class DataGrid
Inherits System.Web.UI.Page

Public Sub Excel_View(ByVal Query As String, ByVal Connection_String As String)
Dim conn As New OleDbConnection(Connection_String)
conn.Open()
Dim dg As New System.Web.UI.WebControls.DataGrid()
Dim da As New OleDbDataAdapter(Query, conn)
Dim ds As DataSet
ds = New DataSet()

da.Fill(ds)
dg.DataSource = ds
dg.DataBind()

dg.Font.Name = "Verdana"
dg.BorderStyle = System.Web.UI.WebControls.BorderStyle.Solid
dg.HeaderStyle.BackColor = System.Drawing.Color.Blue
dg.HeaderStyle.ForeColor = System.Drawing.Color.White
dg.HeaderStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center
dg.HeaderStyle.Wrap = False
dg.HeaderStyle.Font.Bold = True

''context.Response.Buffer = True
''context.Response.ClearContent()
''context.Response.ClearHeaders()
''context.Response.ContentType = "application/vnd.ms-excel"
EnableViewState = True
Dim tw As New System.IO.StringWriter()
Dim hw As New System.Web.UI.HtmlTextWriter(tw)
dg.RenderControl(hw)
Dim content As String
content = tw.ToString().Trim()

System.IO.Directory.CreateDirectory(Server.MapPath("upload"))

Dim filestream As New System.IO.FileStream(Server.MapPath("upload\test.csv"), IO.FileMode.Create)
Dim binwriter As New System.IO.BinaryWriter(filestream, Encoding.GetEncoding("UTF-8"))
binwriter.Write(content)
binwriter.Close()
filestream.Close()


da.Dispose()
da = Nothing
ds.Dispose()
ds = Nothing

conn.Close()
conn.Dispose()
conn = Nothing
End Sub
End Class

Download file

public void downloadfile(int fileid)
{

sqlcom = new SqlCommand("select filename from NewsMaster where NewsID=" + fileid, sqlcon);
reader = sqlcom.ExecuteReader();
if (reader.Read())
{

string filename = Server.MapPath("NewsDownload" + @"\" + fileid + @"\" + reader["filename"].ToString());
downloadmethod(filename);

}
reader.Close();

}
public void downloadmethod(string filename)
{
if (filename != "")
{

string path = filename;

System.IO.FileInfo file = new System.IO.FileInfo(path);

if (file.Exists)
{

Response.Clear();

Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);

Response.AddHeader("Content-Length", file.Length.ToString());

Response.ContentType = "application/octet-stream";

Response.WriteFile(file.FullName);

Response.End();

}



}

}